2

If I go to the folder using dos prompt and run:

python.exe DocumentConverter.py a.odt  b.pdf

it successfully converts odt to pdf... but if I do this in Java:

p = Runtime.getRuntime().exec(new String[]{
    "cmd",
    "C:/OpenOffice 4/program/python.exe",
    "C:/OpenOffice 4/program/DocumentConverter.py",
    "C:/OpenOffice 4/program/a.odt C:/OpenOffice 4/program/b.pdf"});

then nothing happens, why?

yshavit
  • 42,327
  • 7
  • 87
  • 124
user2582318
  • 1,607
  • 5
  • 29
  • 47
  • 1
    You probably need to enclose the paths in `"` because they contain spaces. – assylias Jan 23 '14 at 18:15
  • Shouldn't that last String argument be separated into two Strings - one for the a.odt and the other for the b.pdf? – Martin Dinov Jan 23 '14 at 18:17
  • @MartinDinov i tried it too – user2582318 Jan 23 '14 at 18:17
  • Can you elaborate on "Nothing happens"? You're executing a program, *something* happens. Have you verified, for instance, that your code even reaches this point? Is it perhaps throwing an exception that you swallow and therefore don't see? Providing an [SSCCE](http://sscce.org/) will help us see what's happening. – dimo414 Jan 23 '14 at 18:26
  • Use a utility method to escape your paths, add quotes around paths with spaces, etc. ProcessBuilder has a nicer API than the raw Runtime class. Also be sure to consume the contents of `p.getInputStream()` and `p.getErrorStream()` or your program will hang when the process' STDOUT and STDERR buffers are full. – rob Jan 23 '14 at 18:29

4 Answers4

1

Instead of passing a String[] as parameter, try passing just a String.

Runtime.getRuntime().exec("cmd /c C:/OpenOffice 4/program/python.exe C:/OpenOffice 4/program/DocumentConverter.py C:/OpenOffice 4/program/a.odt C:/OpenOffice 4/program/b.pdf");

I believe the thread How do I execute Windows commands in Java? and this page can help you with what you need if you have any questions or wants to know alternative approaches.

Community
  • 1
  • 1
Bruno Toffolo
  • 1,504
  • 19
  • 24
  • when i run this, nothing happens :( – user2582318 Jan 23 '14 at 18:20
  • Did you try using `\\ ` instead of `/`? For instance, `C:\\OpenOffice 4\\program\\python.exe` instead of `C:/OpenOffice 4/program/python.exe`. – Bruno Toffolo Jan 23 '14 at 18:21
  • yes and the button freezes in the pressed state and nothing more happens – user2582318 Jan 23 '14 at 18:25
  • 2
    This has been deprecated as far as I know. It used it work well for Windows 7 and below, however since I upgraded to Windows 8 directly passing stopped working, and with `String[]` it works just fine on both systems. – skiwi Jan 23 '14 at 18:25
  • @skiwi im using windows 8 and i tried using new String[], and same result, can you post the code you used – user2582318 Jan 23 '14 at 18:26
  • Did you try inserting the string `/c` between the first and second elements of your array? Note that the command I presented in my answer has this slight difference when compared to the one yshavit was trying to execute in the question. – Bruno Toffolo Jan 23 '14 at 18:40
0

I would suggest you try the ProcessBuilder

 Process p = new ProcessBuilder("myCommand", "myArg").start();

ProcessBuilder pb =
   new ProcessBuilder("myCommand", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory(new File("myDir"));
 File log = new File("log");
 pb.redirectErrorStream(true);
 pb.redirectOutput(Redirect.appendTo(log));
 Process p = pb.start();
 assert pb.redirectInput() == Redirect.PIPE;
 assert pb.redirectOutput().file() == log;
 assert p.getInputStream().read() == -1;
Njax3SmmM2x2a0Zf7Hpd
  • 1,354
  • 4
  • 22
  • 44
0
  • I would add a sleep command at the beginning of DocumentConverter.py, then from cmd, use the command 'tasklist' to see the list of currently running tasks. That should tell you if the way Java is kicking off your program looks the same as how it gets run manually from the cmd window. This is probably the most important thing you can do because it's difficult to get Java to run external programs and pass parameters the same way that you do from the command prompt.
  • I don't think you should need the 'cmd' to start with.
  • You are most likely going to have to escape the space in "C:/OpenOffice 4/program/python.exe", so it should be "C:/OpenOffice\\ 4/program/python.exe", otherwise that will probably be viewed as two separate parameters, which is not what you're going for. Better yet, avoid specifying the full path, at least initially. Once you get it working with a relative path, then you can tackle the absolute path. One step at a time.

Also, I would start here: http://commons.apache.org/proper/commons-exec/

The apache commons-exec makes calling external programs from Java better, but it's still painful.

kevinmrohr
  • 821
  • 7
  • 11
0

You should do three things differently compared to working code that I have, this may or may not solve your actual problem:

1) You should not pass cmd, with Runtime.getRuntime().exec() you are essentially in the commandline interface already.

2) You should enclose every argument with spaces within quotes, so in Java it looks like the following: "\"argument with spaces\"".

3) There may only be one argument in each element, in this case in String[].

In your code you may do it like this:

p = Runtime.getRuntime().exec(new String[]{
    "\"C:/OpenOffice 4/program/python.exe\"",
    "\"C:/OpenOffice 4/program/DocumentConverter.py\"",
    "\"C:/OpenOffice 4/program/a.odt\"",
    "\"C:/OpenOffice 4/program/b.pdf\""});

Then at a later point you can call p.waitFor() if you want your thread (program) to waiit until the execution has finished.

skiwi
  • 66,971
  • 31
  • 131
  • 216