How can I from within Java code find out which and how the running JVM was launched? My code shall spawn another Java process hence I'd be especially interested in the first parameter - which usually (in C, Pascal, Bash, Python, ...) points to the currently running executable.
Edit: I am not sure why someone would downvote - but maybe I was not clear enough for others.
So when a java application is run like
d:\openjdk\bin\java -Xmx500g -Dprop=name -jar my.jar param1 param2
I can access command line parameters in my main method like
public class Main {
public static void main(String[] args) {
System.out.println("main called with " args);
}
}
but that will deliver access to param1
and param2
only. How would I get the full command line?
CodePudding user response:
Following Determining location of JVM executable during runtime I found the real answer to be:
ProcessHandle.current().info().commandLine().orElseThrow();
Thank you, @XtremeBaumer
CodePudding user response:
Use the Runtime Management Bean RuntimeMXBean
to obtain the VM arguments, like in:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
List<String> inputArgs = mxBean.getInputArguments();
This will return all arguments passed to the virtual machine, not the parameters passed to the main method.
To get the command, use ProcessHandle
as answered here by Hiran Chaudhuri.