473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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

2
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.
Nov 9 '09 #1
3 6948
chaarmann
785 Expert 512MB
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.
Nov 9 '09 #2
Shayco
2
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.getRuntime().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...
Nov 9 '09 #3
chaarmann
785 Expert 512MB
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.
Nov 10 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: aa | last post by:
Anybody know the solution to trailing space in argument of Runtime.getRuntime().exec(arg), Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:\\Documents and...
4
by: Hal Vaughan | last post by:
I never noticed this before, but when I run an external program through Runtime, like this: Runtime rt = Runtime.getRuntime(); System.out.println("Running external command: " + sCommand); try...
5
by: JW | last post by:
Hi, I don't seem to get any results from the following use of Runtime.getRuntime().exec(cmd) using Java 1.4 on Redhat linux. Suppose that in the same directory as my java file below, I have...
5
by: Ayesha Ahsan | last post by:
Hi, I use Runtime.getRuntime().exec(command) to make my system call. For Windows based Dos, i add "cmd /c" before I type in my system call. So for example make the system call "dir": String...
2
by: uwnewsgroup | last post by:
When I was using Runtime.exec(String cmd) to run a unix utility (join), and try to get its standard output by using Process.getOutputStream(), it blocks forever. I tried it using Java 1.4 and...
1
by: maya2000 | last post by:
Hello, I searched this java newsgroup and tried all suggestions, but still failed. I keep watch a task manager, but cmd.exe never running. All I want to do is run and pop up the cmd.exe using...
5
by: chenthil | last post by:
In my Java program I need to call two DOS batch programs namely call.bat and start.bat. First I need to start the batch program call.bat and once that program is completed, I need to call the other...
2
by: yaha1982 | last post by:
Hi, Friends I am trying to one firefox at runtime through java code but it gives error. I don't get why such type of error occures. My code is : try {...
2
by: jerry chapman | last post by:
I am trying to send a command to windows from my java program, and I get an error. The pertinent (?) part of my code follows: public boolean action(Event evt, Object arg) { if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.