File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Make it so: Java DB Connections & Transactions this week in the JDBC 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

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

Shay Yac

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

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

Joined: Mar 22, 2005
Posts: 42959
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:
subject: problem with Runtime.getRuntime().exec when running java in .bat
It's not a secret anymore!