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 :)
The way to start a jvm is by invoking the main, either by invoking a jar using java -jar MyJar or by simply running main class from an IDE. Yes, Multiple jvm instances can be run on a single machine, they all will have their own memory allocated. There will be that many jvms as many main programs you run.
The JVM is the thing that can execute java bytecode. java is a command line tool that is part of a java runtime environment (JRE) that knows how to start a java virtual machine, load and execute your class file.
Each java invocation starts its own JVM.
Go to Control Panel, and look for the Java control panel applet. In there click on the Java tab, and then the "View..." button. The window that pops up should have the paths to the Java VM executables.
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()
javadocYou 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]
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With