I am attempting to programmatically retrieve the VM arguments from within a Java application.
After doing some research, I came across what seemed to be a perfect solution:
ManagementFactory.getRuntimeMXBean().getInputArguments();
However, when the content of an argument contains a space character, the argument is split and only up to that space character is present in the list.
With the example VM arguments -Xmx1024mb -javaagent:"C:/Users/Thompson Main/Documents/app.jar", the following list is returned:
[0] -> -Xmx1024mb
[1] -> -javaagent:C:/Users/Thompson
[2] -> Main/Documents/app.jar
[3] -> -Dfile.encoding=Cp1252
The second VM argument is cut off due to containing a unavoidable space, and I cannot assume that all of my users lack a space character in file paths. This bug renders ManagementFactory and RuntimeMXBean useless in my opinion.
I have done more research, but I haven’t been able to locate an alternate, successful way to retrieve VM arguments in their entirety in Java 6 (thanks Jean-Philippe for letting me know it works in Java 7).
What is another way to acquire VM arguments from within an application?
This is a JDK bug, corrected in JDK 7 : RuntimeMXBean.getInputArguments() doesn’t handle arguments with spaces properly.
Good solution : upgrade to Java 7.
Ugly solution : reconstruct the parameters, but it’s going to bed awful 🙁