How to capture Java Exceptions in batch file
I have a batch file that calls java class. When the java class throws exception, how do I capture the exception as an error message in batch file.
This is the java program
import java.io.*;
publicclass Test{
publicstaticvoid main(String []args){
String wantThisToFail=null;
try{
if (wantThisToFail.equals("SomeString") ){
System.out.println("Testing");
}
}catch (Exception e){
System.out.println("Exception " + e);
}
}
}
This is the batch file
@echo off
echo Testing
REM execute the javaclass
java Test
set deployError=%ERRORLEVEL%
echo %deployError%
IF NOT %deployError% == 0 (
echo failed
)else (
echo succeeded
)
Since the java class throws NullPointerException, i want the batch file to show "Failed" Message.
Please Help!!
[1657 byte] By [
Frascatia] at [2007-11-27 4:21:15]

Try capturing the return value of the command "java Test". If it succeeds, you should get a '0', otherwise you'll get something else. I admit it's been years since I had to do that, but it rings a bell. If it works, and you're on Java 5 or later, you can use the method Thread.setDefaultUncaughtExeptionHandler ( look at the docs ) and from there use System.exit(<your integer value return code here>) for more control over the return value
> Thank you george for the suggestions. But my hands
> are tide here and i cannot make any change to the
> java program, the only change i could do is at the
> batch file level.
> ERROLEVEL is always displaying "0" here.
If you can't change the java code, how can we help you? This doesn't seem to be a java question. Perhaps you can find a batch forum somewhere?