tags:

views:

893

answers:

5

Can I run more than one JVM? If yes then how can I find a particular class is loaded on which JVM?

+3  A: 

It's not entirely clear what you mean, but:

  • You can have more than one VM (version / brand etc) installed on the same machine
  • You can run more than one java process, whether of the same JVM version or different ones

Unless you're running a debugging agent or something similar, I don't know of any way to ask a JVM process whether it's loaded a particular class. It seems a bit of an odd requirement - why do you want to do this?

Jon Skeet
+3  A: 

You can run as many JVMs as you can fit on your disk and in memory :)

Whenever you start a Java application, you're first starting the JVM and then telling it which application to run. The answer to "which JVM" is simply: The JVM that you loaded the application with!

It's possible to do some esoteric fiddling with classloaders which would prove an exception to what I've just said. But it's true in the general case and the majority of all applications.

Carl Smotricz
A: 

Multiple JRE (Java Runtime Enviroment) is very possible. I do so. The thing is JVM does not always run on your system. It is like any other software. When you run a jar file, it starts running.

The default JRE is set in Environment Variables as JAVA_HOME (right click my computer -> properties -> advanced tab -> Environment Variables)

To run a jar file you simply run this command:

C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe" -jar Myfile.jar

You can use any other jre javaw to run a jar file.

Please note that j2re1.4.2_04 may not be your jre version.

Edit:

All classes in a jar file run on a single JVM. As you may guess. See your JAVA_HOME, it is the default.

JCasso
+1  A: 

Can I run more than one JVM?

Yes - just run the 'java' process

If yes then how can I find a particular class is loaded on which JVM?

The 'jps' program that is distributed with the JAVA SDK will list all java processes (JVM's) ruinning on your machine, the main class that is being executed by each JVM and the classpath. You'll have to see which jars or classes are on each classpath to figure out if a class is loaded or not.

try running

jps -mlvV

and see what you get

emeraldjava
+2  A: 

Yes, you can run multiple JVM's on a single machine.

Sun packages the tools to run the jvm in a few different ways. Usually, you either have a java development kit (jdk) or java standard edition (jse) installed as the default. Those packages include a java program that gets invoked to start a jvm. In addition, the jdk also contains some additional commands (like javac) for developers.

You can have multiple jdk's and or jse's available on a single machine. On windows, jdk and jse packages are usually installed under Program Files/java (this is from memory as I don't have a pc handy at the moment)

On Mac look under /System/Library/Frameworks/JavaVM.framework/Versions.

On Linux, I would use which java command to determine where the default java command is installed (usually /usr/bin). Then do a ls -al | grep java (inside /usr/bin, for example) and note where the symlinks point in order to figure out where there might be other versions installed.

Once you've figured out where each of the various jdk's and jse's are located on your system, then you can start to figure out which version of java is used to start each of the programs.

Each java program will startup using the default jvm. Open a command window or terminal and try java -version to determine which version is the current default.

Rather than using the default java version, programs can also be started to use a specific java version. For example, sometimes I create a custom windows shortcut to open Eclipse using a specific version of the jdk.

As of java 5, there's a tool named jconsole that might also help you determine which programs are running in which verions of jvms. Simply open a console, and type jconsole and you should get a nice GUI that shows all the programs running in the default jvm. I think you might even be able to inspect programs classpaths.

Hope that helps, good luck!

Dave Paroulek