Search code examples
javajvmjvm-argumentssystem-properties

java getInputArguments() returns awkward results if system property value contains spaces


Consider a main method:

public static void main(String[] args) throws Exception {
    System.out.println("property='" +  System.getProperty("property") + "'");
    List<String> inputArgs = ManagementFactory.getRuntimeMXBean().getInputArguments();
    System.out.println("jvm input args size: " + inputArgs.size());
    System.out.println("jvm input args: " + inputArgs);
} 

Results from running the program:

>java -Dproperty=hey!
property='hey!'
jvm input args size: 1
jvm input args: [-Dproperty=hey!]

>java -Dproperty="one two three"
property='one two three'
jvm input args size: 3 //but there's only one input property!
jvm input args: [-Dproperty=one, two, three] //!!!

At least that's the behavior on Oracle/Sun's jvm 6 on mac) & on win (haven't tested elsewhere).

Does anyone know a way of getting input args that is reliable when system properties contain spaces?


Solution

  • Apparently it's a known issue which may have been fixed in JDK7: https://bugs.java.com/bugdatabase/view_bug;jsessionid=f84d44729bd8affffffffeb9b87963e2d752?bug_id=6459832