Problem with Runtime.getRuntime().exec when running java in .bat

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shayco
    New Member
    • Nov 2009
    • 2

    Problem with Runtime.getRuntime().exec when running java in .bat

    hey,
    in my code i'm using Runtime.getRunt ime().exec() in order to run a .bat file that calls another java program (they communicate with each other using RMI).
    when i call:
    Process process = Runtime.getRunt ime().exec("cmd /c start C:\\MyFolder\\J avaApp.bat");
    the seperate process runs perfectly, but when i add a space to the path:
    Process process = Runtime.getRunt ime().exec("cmd /c start \"C:\\My Folder\\JavaApp .bat\"");
    then the java.exe refuses to load and all i get is an empty cmd with only "C:\"...

    i also tried running this line with a ProcessBuilder, but that didn't work as well.
    the .bat file has to be under C:\Program Files, so moving it under a folder without a space in its path is not an option.

    does anyone know how can i pass through this problem?
    thnx.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    The "C:\\MyFolder\\ JavaApp.bat" is one of the arguments of the command "cmd", so if you put a space there inside the name it is parsed as 2 arguments instead of 1. I am not 100% sure but in this case if I remember right you can use method "Process exec(String[] cmdarray)" of class Runtime, and not "Process exec(String command)"! Just put the command and the arguments all as separate strings into a String array and pass that.

    Comment

    • Shayco
      New Member
      • Nov 2009
      • 2

      #3
      i tried this method already and it didn't work.
      notice i wrote \"C:\\My Folder\\JavaApp .bat\" which means it's supposed to be read by java as one argument.
      by the way if i have a .bat file that contains:
      "start /b notpad
      start /b calc
      start /b java -cp args..."
      and i run it like this:
      Process process = Runtime.getRunt ime().exec("\"C :\\My Folder\\JavaApp .bat\"");
      then the notpad and calc run (so it means that the .bat is being launched) but the java doesn't...

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        So if the .bat is being launched, which you can verify by the calculator pop-up, then you have solved your Java problem with Runtime.exec().
        Now you need to solve a second problem, ms-dos related: how to start a java program from a batch file.
        Can you please list full command instead of only ellipis (...) and "args", I mean, the real command contained inside your *.bat file instead of an informative "java -cp args...".
        Did you already try this line exactly from the command line directly?
        if yes, have you used environment variables before that, which are not set if the *.bat is invoked from your Java application? I mean something like CLASSPATH or PATH?
        If yes, then set them inside your *.bat file or pass them along with the Runtime.exec() method.

        By the way,
        starting a cmd inside a cmd, that means a second shell (from your command)inside a first shell (from Java), which you tried in the first version of your posting, is unnecessary and a bad idea. I remember a little that you must use option K, that means "cmd /K ..." to pass arguments to the second cmd.
        Also I remember some differences between "command" and "cmd". Just try "command" to see if they matter.

        Comment

        Working...