1

I have a .bat file called galen.bat. I included the path to this file in the PATH environment variable.

When I run:

galen.bat --version 

in cmd, I get the following output:

Galen Framework Version: 1.6.3 JavaScript executor: Rhino 1.7 release 5 2015 01 29 

I have the following java code through which I'm trying to run the same command through my application -

 public static void main(String[] args) throws Exception { ProcessBuilder builder = new ProcessBuilder( "cmd.exe", "/c", "galen.bat --version"); builder.redirectErrorStream(true); Process p = builder.start(); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while (true) { line = r.readLine(); if (line == null) { break; } System.out.println(line); } } 

When I run this I get the following error -

'galen.bat' is not recognized as an internal or external command, operable program or batch file. 

Please note that if I use

ProcessBuilder builder = new ProcessBuilder( "cmd.exe", "/c", "java -version"); 

instead of

ProcessBuilder builder = new ProcessBuilder( "cmd.exe", "/c", "galen.bat --version"); 

then I do get the correct output in my eclipse console window -

java version "1.7.0_03" Java(TM) SE Runtime Environment (build 1.7.0_03-b05) Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode) 

Why is galen.bat --version not working? How do I fix this? Thanks!

3
  • 2
    Did you tried giving full path of .bat file? Commented May 7, 2015 at 7:49
  • 2
    Try providing the full path to the bat file, or (maybe less usefully) place it in the working directory of the java app. Commented May 7, 2015 at 7:49
  • 1
    your whole code is a copy of this answer stackoverflow.com/questions/15464111/… where it has been clearly mentioned what it has done .. that is missing in your code.. read that answer carefully Commented May 7, 2015 at 7:55

1 Answer 1

1

According the docs:

When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application's virtual machine process. The application can change this set using ProcessBuilder.environment.

So you must include the new path of galen.bat in the path of your JVM or change ProcessBuilder.environment to your system PATH, but according this answer, seems last option is not possible.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.