0

When i call VirtualMachine.list(), it works fine when i'm on eclipse and with jdk. But as soon as i change to jre, or the i try my app out of eclipse, this method seems to return an empty list each time. I need to be able to run my application anywhere wih just a JRE.

for(VirtualMachineDescriptor jvm :  VirtualMachine.list()){
        System.out.println("jvm: " + jvm.displayName());
}
hugo4715
  • 43
  • 7
  • possible duplicated https://stackoverflow.com/questions/31493980/how-can-i-able-to-compile-and-run-java-programs-without-jdk-with-just-jre – AbouLkhair Jul 12 '17 at 09:16
  • I don't think it is, i can compile my code fine. The problem is that the i can't find the VirtualMachineDescriptor's list correctly, even including the tools.jar in my jar. – hugo4715 Jul 12 '17 at 09:23

2 Answers2

2

I need to be able to run my application anywhere wih just a JRE.

In this case you should not use com.sun.tools.attach.VirtualMachine class, since VirtualMachine is only a part of JDK, not JRE.

  • Oracle license agreement does not allow to distribute tools.jar with your application without distributing the entire JDK.
  • Even if you include tools.jar in your application, you still won't be able to run it on any JRE, because tools.jar has platform-specific parts, and it depends on a native library (libattach.so) which is also missing in JRE.

So, there are the following options:

  1. Require installing JDK before running your application.
  2. Bundle the entire JDK along with your application.
  3. Implement the missing functionality from scratch. If you are looking how to list running JVMs without using tools.jar, this answer might give you an idea.
apangin
  • 92,924
  • 10
  • 193
  • 247
0

I'm somewhat confused. In my opinion VirtualMachine class is non part of any JRE as it's included in tools.jar, so it's only available from JDK. Possibly, you may want to jar the content of tools.jar into your application jar file, or distribute tools.jar and have it added to classpath at runtime. Your code will run as expected and will list the jvm your program is running, and any program running in the same jvm.

ugo
  • 284
  • 1
  • 2
  • 10