Getting Java program running in Eclipse to show up

2019-07-05 09:16发布

问题:

I'm trying to use Java VisualVM on Windows 7 64-bit together with Eclipse Kepler and am experiencing the following issue:

I have a Java application running inside Eclipse, but when I open jVisualVM, the only item showing up in the Applications under Local is VisualVM itself.

Is it possible to have my Java application show up as well? Do I need to launch it with some command line parameter to allow VisualVM to connect? Do I need to run it outside of Eclipse?

回答1:

Due to a design peculiarity in the Windows JVM's you need to run JVisualVM in the same kind of JVM as the program you want to connect to (and as the same user)

This mean, use either 32-bit or 64-bit, not one of each.

A simple way to ensure this, is to add the JVM in the JDK installation directory containing the jvisualvm executable you want to use to the JVM's known to Eclipse (in Preferences), and then change the JVM used by your project to the one in the JDK you just added. Now your application should show up in the VisualVM window when launched.



回答2:

Have you tried using the VisualVM eclipse launcher ? This will automatically launch visual VM for you.

The only change is when you run your main class in eclipse, is to change the configuration to use the VisualVM launcher like this.



回答3:

Another possible solution, in case it helps others: Restart Eclipse.

This was the first time I'd tried JVisualVM on a new computer. Eclipse was showing up in JVisualVM, just not any child processes it launched.

I was tearing my hair out, trying all sorts of other things (firewall, different JDKs...) Then this silly restart just fixed it!


Anyway if all is working well, the rules seem to be:

  • JVisualVM can attach to and monitor applications using any JDK / JRE version and the same "bit-ness".
  • For full functionality (like profiling) JVisualVM should be running on an equal or higher version of the JDK than the application

Random tip (while we're discussing bitness):

  • Browsing memory dumps is inexplicably a zillion times fastor in 64-bit. (Shame, as I still like the 32-bit's -client mode for GUI apps to get the lowest memory usage. So I'd like to also profile them that way, but unfortunately it's intolerably slow.)


回答4:

Just found another, quite obscure thing that can prevent your application from showing up in JVisualVM under Windows:

JVisualVM uses your system's temp-folder (as specified by the environment variable TMP) to communicate with running applications. For security reasons, if this folder is not located on an NTFS partition where Windows can provide strict file access controls, debugging is disabled and applications won't show up in JVisualVM.

If reformatting that partition or moving your entire temp-folder is not something you want or can do, you can simply create a secondary temp-folder just for JVisualVM on an NTFS partition somewhere else and set your launch configurations in Eclipse to set the environment variable TMP to that folder. Then, simply use the following batch file to run JVisualVM and everything should work:

@Echo off
set TMP=F:\temp
start "JVisualVM" /b "C:\Program Files\Java\jdk1.7.0_76\bin\jvisualvm"

Here, you need to replace F:\temp with the location of your new temp-folder just like specified in the launch configurations and C:\Program Files\Java\jdk1.7.0_76 with your JDK install directory.