• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey,
in my code i'm using Runtime.getRuntime().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.getRuntime().exec("cmd /c start C:\\MyFolder\\JavaApp.bat");
the seperate process runs perfectly, but when i add a space to the path:
Process process = Runtime.getRuntime().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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a workaround.

Create a file called go.bat where your java app executes (i.e., where your class files are). The contents of go.bat should call the bat file that you're trying to execute that has spaces in it.



Then, in your Java code, simly call your go.bat file.


Your go.bat file will execute the real batch file you're trying to run. Problem solved.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch-

For starters, you should use Runtime.exec(String[]) instead of Runtime.exec(String). But even before that, you need to read this article.
reply
    Bookmark Topic Watch Topic
  • New Topic