2

I would like to identify the current JVM which is run. In the best case with a function described in the JVMTI Documentation, but I cannot find anything.


What I meant, is something like this: VirtualMachine.list() delivers:

[sun.tools.attach.WindowsAttachProvider@46ae506e: 2440 de.fu.profiler.view.MainFrame...

But it displays all JVMs, not the current one being run.

Konrad Reiche
  • 27,743
  • 15
  • 106
  • 143

5 Answers5

3

Use the Java system properties, for example

java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition
java.runtime.version=1.5.0_01-b08
java.specification.name=Java Platform API Specification
java.specification.vendor=Sun Microsystems Inc.
java.specification.version=1.5
java.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
java.version=1.5.0_01
java.vm.info=mixed mode, sharing
java.vm.name=Java HotSpot(TM) Client VM
java.vm.specification.name=Java Virtual Machine Specification
java.vm.specification.vendor=Sun Microsystems Inc.
java.vm.specification.version=1.0
java.vm.vendor=Sun Microsystems Inc.
java.vm.version=1.5.0_01-b08

Use java.lang.System.getProperties() or getProperty(String name)

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • This looks fine, however I cannot find any unique identifier. I am looking for something like process id or name of the application. Name would be for instance: com.fun.MyApplication – Konrad Reiche Apr 07 '11 at 16:03
  • So you're not trying to identify the JVM, but the machine it's running on? Please clarify what you mean by "unique id". – Jim Garrison Apr 07 '11 at 16:07
  • @Jim-Garrison: I am trying to identify the JVM which is running from other JVMs which are also run on the OS. For instance currently I am running the JVM which runs Eclipse. – Konrad Reiche Apr 07 '11 at 16:10
  • I don't think there's a builtin JVM-unique ID. See this question http://stackoverflow.com/questions/2834749 for other suggestions. – Jim Garrison Apr 07 '11 at 16:15
3

You can get a unique name from the RuntimeMXBean. on most platforms, this includes the processid of the current process.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

jps (the command line tool bundled with the JDK) will list the currently running Java processes.

You could use JMX to find and attach to different running Java processes.

http://download.oracle.com/javase/1.5.0/docs/guide/management/agent.html

You will need to enable monitoring.

Finding your current PID is very operating system dependent. Here is a blog post with some more suggestions:

http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html

Here are links with some more info:

How can a Java program get its own process ID? http://www.google.com/search?q=find+current+java+process+id

Community
  • 1
  • 1
Will Iverson
  • 2,009
  • 12
  • 22
-1

Will System.getProperty("java.runtime.name") do?

subsub
  • 1,857
  • 10
  • 21
-1

A JVM does not have a single identifying class that is linked to it's invocation, at least not one that you can easily query.

If you're using JVMTI, you can identify the threads that are running in the JVM that you have connected to. There may be one or more threads, depending on whether the JVM is running a graphical interface, or (for instance) an enterprise container. For the given threads you can examine the stack and identify the current method being called. If you iterate up through the stack you could decide if the parent class is part of the JVM (eg. java.lang.) or an application specific class (eg. com.fun.). This could allow you to uniquely identify the 'target' of the JVM being examined.

AndyT
  • 1,413
  • 9
  • 11