W
wex
I have used this kind of functionality many times:
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(somecmd);
However I have run into a problem where it will not work when the
command contains a path with spaces in it on a linux platform. For
instance a string that executes any random exec.
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("/path with space/somecmd.bat");
If you executed this command exactly you would get-
java.io.IOException "/path: not found.
Now in windows you would get a similar problem. But the solution is to
pass the command surrounded with quotes embedded into the cmd string
like so:
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("\"/path with space/somecmd.bat\"");
Above works on windows, but on linux you get the same error as before.
I have also tried putting the command in a string array like so:
String[] cmds ={"\"/path with space/somecmd.bat\""};
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(cmds);
This results in the same error with or without the quotes. I am using
this in a java app that runs on both windows and linux so I am trying
to find a solution that works on both platforms. The quote solution
works with the windows paths but not the linux. If you do however run
the same linux command in a shell using the quotes it works fine so I
can't explain the problem. Does anyone have a solution?
thanks, ryan
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(somecmd);
However I have run into a problem where it will not work when the
command contains a path with spaces in it on a linux platform. For
instance a string that executes any random exec.
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("/path with space/somecmd.bat");
If you executed this command exactly you would get-
java.io.IOException "/path: not found.
Now in windows you would get a similar problem. But the solution is to
pass the command surrounded with quotes embedded into the cmd string
like so:
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("\"/path with space/somecmd.bat\"");
Above works on windows, but on linux you get the same error as before.
I have also tried putting the command in a string array like so:
String[] cmds ={"\"/path with space/somecmd.bat\""};
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(cmds);
This results in the same error with or without the quotes. I am using
this in a java app that runs on both windows and linux so I am trying
to find a solution that works on both platforms. The quote solution
works with the windows paths but not the linux. If you do however run
the same linux command in a shell using the quotes it works fine so I
can't explain the problem. Does anyone have a solution?
thanks, ryan