File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes problem with Runtime.getRuntime().exec when running java in .bat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem with Runtime.getRuntime().exec when running java in .bat" Watch "problem with Runtime.getRuntime().exec when running java in .bat" New topic
Author

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

Shay Yac
Greenhorn

Joined: Nov 08, 2009
Posts: 1
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.
Chris Merritt
Greenhorn

Joined: Dec 02, 2009
Posts: 3
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42912
    
  68
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with Runtime.getRuntime().exec when running java in .bat