Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem executing a batch file in a Java application

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?

like image 266
Anto Avatar asked Oct 25 '22 07:10

Anto


2 Answers

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.

like image 86
gawi Avatar answered Nov 10 '22 00:11

gawi


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

like image 33
stacker Avatar answered Nov 10 '22 00:11

stacker