5

I have batch file, named run.bat which inlcudes the following code:

@echo off
REM bat windows script
set CXF_HOME=.\lib\apache-cxf-2.2.7
java -Djava.util.logging.config.file=%CXF_HOME%\logging.properties -jar archiveServer-0.1.jar

When I execute this file on a command line it works perfectly. However when I try to execute within a java file with the following statement:

File path = new File("C:/Documents and Settings/Zatko/My Documents/Project-workspace/IUG/external/application/archive");    

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", "start", "run.bat"}, new String[]{}, path);

I get the following error in the terminal window:

'java' is not recognized as internal or external command, operable program or batch file.

Where the error may be?

2 Answers 2

2

Java.exe is not found in your PATH.

If you can assume that the JAVA_HOME variable is defined, you can modify your batch file:

%JAVA_HOME%\bin\java -Djava.util.logging.config.file=%CXF_HOME%\logging.properties -jar archiveServer-0.1.jar

A better way to do it would be, as staker suggested, to set the PATH environment variable to contain %JDK_HOME%\bin

File workingDirectory = new File("C:/Documents and Settings/Zatko/My Documents/Project-workspace/IUG/external/application/archive");    
String javaProgram = System.getProperty("java.home") + "\bin";
String[] command = {"cmd.exe", "/C", "start", "run.bat"};
String[] environment = {"PATH=" + javaProgram};
Process process = Runtime.getRuntime().exec(command, environment, workingDirectory);

As a third option, you can also avoid to have the batch file by invoking the main-class of the jar directly. You archiveServer would run in the same process, however. Maybe that's not want you want.

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

2 Comments

+1 I fixed some minor typos, please rollback if you don't agree.
@stacker Thanks. I've just added back the workingDirectory as specified in the code of the question.
1

I suppose you didn't add JAVA_HOME/bin to your PATH environment variable.

4 Comments

The value C:\Program Files\Java\jdk1.6.0_18\bin; is included in the PATH variable
Try to add C:\Program Files\Java\jdk1.6.0_18\bin before java in your batch file. A cleaner way could be to use Runtime.getRuntime().exec() with two parameters to specified environment.
"A cleaner way could be to use Runtime.getRuntime().exec() with two parameters to specified environment", can you give me an example please?
Gawi already posted an example, see javadoc of Runtime.exec for alternatives which include the environment. I'm sorry but can't post code from my iPad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.