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

前端 未结 5 971
长情又很酷
长情又很酷 2020-12-09 01:15

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 li         


        
相关标签:
5条回答
  • 2020-12-09 01:39

    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?

    0 讨论(0)
  • 2020-12-09 01:52

    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 :

    • ManagementFactory.getRuntimeMXBean() - Returns the managed bean for the runtime system of the Java virtual machine.
    • getInputArguments() javadoc
    • determine if JVM is running in debug mode
    0 讨论(0)
  • 2020-12-09 01:52

    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
    
    0 讨论(0)
  • 2020-12-09 01:53

    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]

    0 讨论(0)
  • 2020-12-09 01:54

    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.

    0 讨论(0)
提交回复
热议问题