问题
When attempting to attach an agent jar file onto another process running in java, I have came across the exception:
com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
I was running linux, with java Oracle JDK 8_101, however after answering this question I've realized the O.S does not matter for the cause of this problem.
Edit: Answer:
If you encounter this problem, the reason it occured for me is because I was launching a program from a different JVM, other than the default JVM specified for the system.
i.e)
Program A (The launcher), is running on JVM-1 (JDK_8_1 for example, or JDK_8_1/jdk/jre).
Program A (The launcher), creates a process with java -jar programB.jar
Program B (The target), is running on the system's default JVM, JVM-2 (JDK_8_2 for example, or JDK_8_2/jre).
Program A (The launcher) CANNOT attatch to Program B (The target), because the JVM Program A (The launcher) is running on, does not match the JVM of which Program B (The target) is running on, thus throwing the com.sun.tools.attach.AttachNotSupportedException:
回答1:
Common reasons for this problem:
- Attach socket
/tmp/.java_pid1234
has been removed (e.g. by a scheduled job that periodically cleans up /tmp). - Target JVM is started with
-XX:+DisableAttachMechanism
option. - Garbage Collection or other long VM operation (e.g. Heap Dump) is in progress.
- JVM cannot reach safepoint within attach timeout. This happens rarely, and the problem is typically intermittent.
回答2:
Problem: different user executing jcmd
It might happen, that the user calling jcmd is different, than the user running the proccess.
Example:
- user invoking jcmd as root
- user running JVM as fancyUser
Solution:
On Linux try to run jcmd with the same user, as the process is running.
When you have such a scenario, you will get given error.
Problem: AppArmor
When AppArmor is enabled for running JVM instance, which restricts sys-calls, it might be the case, that opening a socket-connection is restricted.
Solution:
Change AppArmor-Profile for process
来源:https://stackoverflow.com/questions/48379878/java-attachnotsupportedexception-unable-to-open-socket-file-hotspot-vm-not-l