I am using below script(.sh file) to run a java code in an UNIX system,but even if Java is giving exception or terminated successfully in both cases, exit code coming as 0, I want to return non zero exit codes from script, if Java run throwing an exception, so that I can add if-then check in script,to print success or failure messages.
#!/bin/sh
echo 'processing started -->>'
LOC=/opt/appl/Mapping/
export JAVA_HOME=/usr/java6
export PATH=/usr/java6/bin
LD_LIBRARY_PATH=/opt/appl/JARS/
export LD_LIBRARY_PATH
java -classpath $LOC/ojdbc6-11.2.0.3.0.jar:$LOC/ds35-02.00.11.jar:$LOC/log4j-1.2.17.jar:$LOC/TestClasses.jar:$LOC/db2jcc_license_cisuz-3.0.0.jar:$LOC/db2jcc_license_cu-3.0.0.jar:$LOC/db2jcc-3.0.0.jar -Xms256M -Xmx512M com.home.backfill.TestRun
I can use Try-catch in Java file and use System.exit(1) at catch block, but I am looking for any good generic approach, as My code could be very long, not sure if it would be good idea to put System.exit(1) in every catch block.
System.exit(1)in thetry-catchwithin themainmethod only. In all other places, you either remove thecatch-blocks (if they would otherwise doSystem.exit(1), or re-throw the exception from thecatch-block, so that it's finally caught inmainmethod..fatalError()) in every catch block that callsSystem.exit(1). That way you'll have more maintainability.System.exit(...)with different values depending on the type of exception thrown and the method that throws it. Each code is associated with a unique error label. For example: 1 forbad config file, 2 for somepersistence error, etc.