1

I want to run a command through java session. The command contains spaces. as "C:\With Space\sample.exe" -command_option "C:\Source File\test.c" This works if

C:\WithoutSpace\sample.exe -command_option "C:\Source File\test.c"

if we keep the quotes in C:\With Space\sample.exe we get error as :'The filename, directory name, or volume label syntax is incorrect.' and if we remove the quotes then the exe do not run... please guide.

Thanks,

2
  • If you are using Runtime#exec(), you should try ProcessBuilder instead. Commented Apr 25, 2013 at 12:23
  • Could you show the code you use to run the command ? Commented Apr 25, 2013 at 12:24

2 Answers 2

3

Try this:

String[] arg = {"cmd","/c","C:/Source File/test.c"};
ProcessBuilder pb = new ProcessBuilder(arg);
Process pr = pb.start();   

Also, you can use Runtime.exec(String[]) version

Example:

Runtime rt = Runtime.getRuntime();
String[] args = { "cmd", "/c", "C:/Source File/test.c"};
try
{
    Process proc = rt.exec(processCommand);
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 For using ProcessBuilder. Note that the code sample doesn't work because \S isn't a valid escape sequence :-)
"c:/source file/test.c" or "c:\\source file\\test.c"
@user93353 c:/source file/test.c
I was not asking a question - i was saying that your original paths won't work - and you can use either of the paths in my comment - both should work.
0

You can try this:

Process process = Runtime.getRuntime().exec("'C:\WithoutSpace\sample.exe' -command_option 'C:\Source File\test.c'");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));

while ((line = in.readLine()) != null) {
    //line contain command output
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.