Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

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 :)

question from:https://stackoverflow.com/questions/13958318/is-it-possible-to-get-the-command-used-to-launch-the-jvm-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...