tags:

views:

43

answers:

2

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?

+1  A: 

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

stacker
how do you do it?
Tony
The value C:\Program Files\Java\jdk1.6.0_18\bin; is included in the PATH variable
Tony
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.
stacker
"A cleaner way could be to use Runtime.getRuntime().exec() with two parameters to specified environment", can you give me an example please?
Tony
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.
stacker
+2  A: 

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.

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