1

There is batch file that contains a jar file. I want to run a batch file in a java program in netbeans. I used this code:

Process p=    Runtime.getRuntime().exec("cmd /c  start  \"C:\\Users\\123\\Desktop\\New folder (6)\\Browser.bat\"");

But by running this code, a cmd windows in open and below address is shown and batch file doesn't run.

C:\Users\123\Documents\NetBeansProjects\JavaApplication19

That is different from the address of batch file.

batch file contains:

java -jar -Xms32m -Xmx512m Browser.jar

using ProcessBuilder: the jar file runs.

ProcessBuilder pb = new ProcessBuilder("java.exe", "-jar","-Xms32m", "-Xmx512m", "C:\\Users\\123\\Desktop\\New folder (6)\\Browser.jar");
  pb.directory(new File("C:\\Windows\\System32"));
  Process p = pb.start();

using ProcessBuilder for batch file: program runs successfully but jar file not execute.

ProcessBuilder pb = new ProcessBuilder("java.exe", "-jar","-Xms32m", "-Xmx512m", "C:\\Users\\Saeedeh\\Desktop\\New folder (6)\\FarsNetBrowser.jar");
pb.directory(new File("C:\\Windows\\System32"));
Process p = pb.start();
6
  • What is printed seems just the cmd prompt advertising the current directory Commented Jul 5, 2014 at 9:15
  • use a process builder and set the initial path of the process to the folder where the batch file is at. Does the batch file work from command prompt directly? Commented Jul 5, 2014 at 9:19
  • yes, it is printed the current directory of netbeans. Commented Jul 5, 2014 at 9:20
  • search google for process builder. change the current folder of the Process Builder to the folder where the batch file is 'C:\\Users\\123\\Desktop\\New folder (6)\' in your case. read up the process builder javadoc Commented Jul 5, 2014 at 9:25
  • 1
    @tgkprog I added a code with ProcessBuilder. it runs the jar file. Commented Jul 5, 2014 at 9:26

2 Answers 2

1

I think there's an extra start in your command line. It should work by just using

cmd /c "C:\Users\123\Desktop\New folder (6)\Browser.bat"

and I would let Java escape argument instead of hardcoding the string:

new ProcessBuilder("cmd", "/c", PATH_OF_THE_BAT_FILE);
Sign up to request clarification or add additional context in comments.

2 Comments

thanks. I used first line in my code but unable to access jar file browser.jar , and by second code the java program runs successfully but jar file dose not runs.
That's just because you use relative pathnames but don't set the right working directory for the child process
0
Process p = Runtime.getRuntime().exec("cmd /c start \"C:/Users/123/Desktop/New folder (6)/Browser.bat\"");

2 Comments

The slashes are the difference. / is not the same as \.
ok, but I think the path is correct. I tryed with your code but the result is same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.