Jack
Jack

Reputation: 1076

Is it possible to get the command used to launch the jvm in java?

I would like to know if it is possible to get from code the command used to launch a java program.

E.g. if I launch a java program with:

 java -cp lib1:lib2:... -jar mylib.jar com.foo.Bar

I would like to get the exact string (jvm parameters included).

Is it possible?


Comment on the bounty and the question

Thank you all for your responses. Unfortunately, I did not get the answer I was initally looking for. I was hoping there was some portable solution to get the complete java command from within the program itself (including classpath etc.). As it seems there are no portable solution and since I am using Linux I am using the responses of agodinhost and Luigi R. Viggiano to solve my problem. However I give the bounty to rahulroc for the most complete (portable) response. For the rest an upvote for all :)

Upvotes: 42

Views: 22017

Answers (5)

Rahul
Rahul

Reputation: 16355

The below mentioned code should show all JVM parameters, arguments passed to the main method as well as the main class name.

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

import java.util.List;

public static void main(String[] args) {
  RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
  List<String> jvmArgs = bean.getInputArguments();

  for (int i = 0; i < jvmArgs.size(); i++) {
    System.out.println( jvmArgs.get( i ) );
  }
  System.out.println(" -classpath " + System.getProperty("java.class.path"));
  // print the non-JVM command line arguments
  // print name of the main class with its arguments, like org.ClassName param1 param2
  System.out.println(" " + System.getProperty("sun.java.command"));
}

javadoc for getInputArguments

Returns the input arguments passed to the Java virtual machine which does not include the arguments to the main method. This method returns an empty list if there is no input argument to the Java virtual machine.

Some Java virtual machine implementations may take input arguments from multiple different sources: for examples, arguments passed from the application that launches the Java virtual machine such as the 'java' command, environment variables, configuration files, etc.

Typically, not all command-line options to the 'java' command are passed to the Java virtual machine. Thus, the returned input arguments may not include all command-line options.

You can also take a look at : jps

It's a Java program that is able to get the full command line for all Java processes, including full class name of main class and JVM options.

You can find a good summary of various JVM tools, including Java Application Launcher links to :

Upvotes: 27

assylias
assylias

Reputation: 328913

You can use this to retrieve the VM parameters :

public static void main(String args[]) {
    List<String> inputArguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
    System.out.println("input arguments = " + inputArguments);
}

However it won't give you all the command line (only gives the JVM arguments, no main class nor parameters). Sample output:

input arguments = [-Dfile.encoding=UTF-8, -XX:-UseTLAB, -Xms2000m, -Xmx2000m, -XX:+PrintCompilation, -XX:+PrintGC]

Upvotes: 19

djangofan
djangofan

Reputation: 29689

In the task manager on Win2003 you can enable the display of a column that displays the command like it does on linux. Or, you can do it from the command line like so:

wmic.exe PROCESS where "name like '%java%'" get Processid,Caption,Commandline

Upvotes: 6

agodinhost
agodinhost

Reputation: 391

in a linux machine would be easier to run:

ps -ef | grep java

this command will list all java programs running with it's used parameters.

Not sure about what can be used in a windows environment.

Upvotes: 9

Luigi
Luigi

Reputation: 8847

It only works on  Sun  Oracle JVM: System.getProperty("sun.java.command")

Additionally, you can have a look at JavaSysMon, it can report command line of active processes. To check which is the current JVM Process check here: How can a Java program get its own process ID?

Upvotes: 8

Related Questions