0

Possible Duplicate:
Java execute a command with a space in the pathname

I am having following command

i_view32.exe C:\*.bmp /import_pal=C:\default.pal /convert=D:\temp\*.bmp Which when i run through command prompt works fine. I am trying to run the same command with the help of java.

 Process p = Runtime.getRuntime().exec(System.getenv("ProgramFiles")+"\\IrfanView\\i_view32.exe  c:\\*.bmp /import_pal= 1.pal /convert=d:\\temp\\*.bmp");

But i am not able to get Output in d:\\temp\\ Folder. Can any one suggest me where i am wrong.

Thanks in Advance..

Is there any other way to give "/" as i am using slash /import_pal=

5
  • did you check if System.getenv("ProgramFiles") actually returns what you need ? Commented Nov 8, 2011 at 8:22
  • 1
    have you tried using the exec with String[] parameter? Im quite sure using Process.exec(String command) doesnt work if you have additional params. Commented Nov 8, 2011 at 8:27
  • does the command gives some output.. you can try reading with p.getInputStream() and p.getErrorStream() to get the outputs from the process Commented Nov 8, 2011 at 8:28
  • 1
    I have found this one quite useful for executing some complex commands through java.. devdaily.com/java/java-exec-processbuilder-process-1 Commented Nov 8, 2011 at 8:29
  • Try using: ProcessBuilder builder = new ProcessBuilder(String[] commands).start(); Commented Nov 8, 2011 at 8:52

2 Answers 2

0

2 your attempts are not exactly the same. I think that you executed command from command prompt when you were in c:\Program Files\IrfanView. When you are trying to run the same command from java you mention the full path. Since some programs are sensitive to current working directory I'd recommend you first to try to run the command from other directory (e.g. from c:) but specify full path.

If it works manually but does not work from java try to use ProcessBuilder instead of Runtime.exec(). Actually it is almost the same but it more Object Oriented and allows to specify working directory separately. I hope this will work for you.

If not try to play with quotes. Directory path 'c:\Program Files' contains space, so the path should be quoted.

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to execute CMD

Example:

proc = Runtime.getRuntime().exec("cmd.exe /c dir");

It should work something like this, for your example it's a bit more complicated, but try it this way.

2 Comments

Its Runtime.getRuntime().exec
You are right, just mistyped character :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.