1

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
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Pranjut
  • 1,747
  • 5
  • 18
  • 31
  • What are you using the "start" for in this case? IIRC it's also a Windows command. Are you trying to run a start.exe from the directory you've just cd'ed to? – Kage Sep 28 '09 at 07:31
  • Not at all, I tried to run other batch files also, but the main problem is that it runs only the first line of the batch file. I don't need to run any exe file, I just need to run the whole batch file, thats it. – Pranjut Sep 28 '09 at 07:37
  • Yes, but why is the "start" in your last line there then? – Kage Sep 28 '09 at 09:52
  • what? to start a new window. Man do you really have knowledge about this stuffs? – Pranjut Sep 28 '09 at 11:02
  • Remember that running a batch from another batch file like this is going to ignore anything that came after the batch file call. You will need `call` for that. And please don't insult people randomly here. – Joey Sep 28 '09 at 19:06

6 Answers6

1

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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. – Pranjut Sep 28 '09 at 07:17
  • 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 Sep 28 '09 at 07:28
  • 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 – Pranjut Sep 28 '09 at 07:33
  • Am I interpreting this correctly? You have inserted ECHO commands before each line to check which line is reached an which isn't? – Kage Sep 28 '09 at 07:50
  • What brought you to the conclusion that it only runs until the second line? – Kage Sep 28 '09 at 09:53
  • 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. – Pranjut Sep 28 '09 at 10:49
1

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.
  • 1,286
  • 12
  • 12
  • Where do I supposed to place my batch file anyway? I don't get the code at all, would you please explain? – Pranjut Sep 28 '09 at 08:02
0

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
  • 33,172
  • 3
  • 63
  • 88
0

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
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
MRG
  • 3,219
  • 1
  • 26
  • 35
  • 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. – Pranjut Sep 28 '09 at 07:20
  • cd /d C:\Tomcat 5.5\webapps\mail_testing\james-2.3.2\bin this line should be equivalent to cd\ c: cd C:\Tomcat 5.5\webapps\mail_testing\james-2.3.2\bin – MRG Sep 28 '09 at 07:39
  • Hey guys, please give me some java code, which can run the whole batch file, not any batch code please. – Pranjut Sep 28 '09 at 07:41
0

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
  • 395
  • 1
  • 3
  • 9
0

This works like a charm:

 Runtime run = Runtime.getRuntime();
    try
    {
    System.out.println("Start Running the batch file");
    Process p = run.exec(new String[]{"cmd.exe","/c", "start", "C:/Users/sony/Documents/NetBeansProjects/CodeReview/src/codereview/install.bat",i,j,m,l});
    System.out.println("Completed");
    }
    catch (Exception e)
    {
    } 

here i,j,k,l are parameter passing to batch file

user3065757
  • 475
  • 1
  • 5
  • 14