• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shay Yac
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Chris Merritt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic