command line - How to start a process from Java with arguments which contain double-quotes -


i need start second java process java program, , 1 of arguments forked process need contain literal double-quote characters ("). how do in portable way, i.e. @ least works on linux , windows?


i tried use processbuilder (which introduced overcome problems of runtime class, right?) array of strings command line, still there problem parameter containing quote:

list<string> commandline = new arraylist<string>(); commandline.add(new file(system.getproperty("java.home") + "/bin/java").getabsolutepath()); commandline.addall(arrays.aslist("-jar", "plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar", "-application", "org.eclipse.equinox.p2.director", "-repository", "http://download.eclipse.org/releases/juno")); commandline.add("-list"); commandline.add("q:select(x | x.id == \"org.eclipse.sdk.ide\")");  new processbuilder().command(commandline).directory(eclipseinstallationdir).start().waitfor(); 

the code above doesn't work (on windows) because processbuilder (or else) eats double quotes in last argument:

instead of q:select(x | x.id == "org.eclipse.sdk.ide"), process receives argument q:select(x | x.id == org.eclipse.sdk.ide) , fails.


i figured out can make work on windows adding \" in argument need " i.e.

commandline.add("q:select(x | x.id == \"org.eclipse.sdk.ide\")".replace("\"", "\\\"")); 

but breaks invocation on linux (as morgano confirmed). i'm forced detect os again. there no simple, portable way start process in java arguments content of string array?

normally, command argument string split around spaces form array of arguments. however, if want single argument contain space, can wrap argument in quotes. later, each pair of quotes removed string. it's not java removing these quotes--it's operating system this.

processbuilder wrapping each of arguments supply in quotes if of arguments contains space, not split multiple arguments further down road operating system (you can hack supplying 1234\" \"5678; end being 2 arguments: 1234, , 5678). processbuilder concatenates arguments 1 command, passes operating system. later, operating system splits command string whitespace , quotes (as described above), , removes pairs of quotes each string argument before begins new process. escape quote (i.e. treat regular character not handled differently os parser), use backslash character before it. command line escape character both linux , windows. like: \\\".


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -