views:

516

answers:

5

Hi experts, I am trying run a batch file from my java codes, but unfortunately I could not run it properly. Actually the batch file is running but only the first line of the batch file is running, so please give solution to it, here are the codes and the batch file.

 class bata
{
public static void main(String [] args)
{
try
{
 Runtime.getRuntime().exec("start_james.bat");


  }
  catch(Exception e) {}
}
}

and the batch file is

cd\
c:
cd C:\Tomcat 5.5\webapps\mail_testing\james-2.3.2\bin
run.bat

start
A: 

What do you expect cd: to do? That doesn't look right to me...

If your batch file is only going to run another batch file, why not run that target batch file to start with? If you're worried about the initial working directory, use the overload which takes a File argument to say which directory to use. For example:

File dir = new File("C:\\Tomcat 5.5\\webapps\\mail_testing\\james-2.3.2\\bin");
Runtime.getRuntime().exec("start_james.bat", null, dir);
Jon Skeet
cd: was a mistake, it should be c: only. I'll try this, but the main problem was, it runs only first line of the batch file.
Ryan
I tried your code, but its not worthed, either.
Ryan
Have you tried to run your batch file from a commandline? Try Start-Run-cmd and execute the batch file. See if it throws any error messages at you.
Kage
I have tried it before I write the java code, I think there are some other ways, which can run the whole batch file, I mean my or your code, runs only the first line of the batch file, if you edit the batch file with writting start in the first line, a new cmd is opened, but after that nothing goes on. please help
Ryan
Am I interpreting this correctly? You have inserted ECHO commands before each line to check which line is reached an which isn't?
Kage
Oh no man, I tried whatever I had write.
Ryan
Oh no man, I have tried whatever I have wrote.
Ryan
What brought you to the conclusion that it only runs until the second line?
Kage
I am practically doing this, and its results it. I'm editing the batch file batch file with different commands, and it result the same, the it runs only the first line.
Ryan
A: 

Isn't there something in java, whereby you can invoke the batch file directly with full path?

I mean, why do you need to change directories?
Also, what is the use of cd:? It is not a valid command in DOS, unless you are using *nix.

shahkalpesh
Hi! "cd:" in bash is new to me. Does it work in other shells?
Kage
I am sorry, actually its c:, cd: was by mistake.
Ryan
A: 

I think he wants to change to a directory and then run the batch file. Can you try this ?

cd /d C:\Tomcat 5.5\webapps\mail_testing\james-2.3.2\bin
run.bat

start
Mahin
@Jon : thanks for the edit.. missed it altogether :)
Mahin
It does not matter actually, because, my code only able to run first line of the batch file, I need some java code which can run the whole batch file.
Ryan
cd /d C:\Tomcat 5.5\webapps\mail_testing\james-2.3.2\binthis line should be equivalent to cd\c:cd C:\Tomcat 5.5\webapps\mail_testing\james-2.3.2\bin
Mahin
Hey guys, please give me some java code, which can run the whole batch file, not any batch code please.
Ryan
A: 

Was "cd:" supposed to be a label you can jump to using the GOTO command? However labels are declared using ":labelname". This should be the reason why your batch execution stops after the first line.

Kage
+1  A: 

If all the other answers (with valid batch file) didn't work try executing cmd.exe directly like this:

File dir = new File("D:\\tools\\bin");
Runtime.getRuntime().exec("c:\\windows\\system32\\cmd.exe /c start_james.bat", null, dir);

You might also use the %SystemRoot% environment variable to get the absolute path to cmd.exe.

dz
Where do I supposed to place my batch file anyway? I don't get the code at all, would you please explain?
Ryan