Batch File to run Java Program

Hi,
I have a Java program I want to run it using a batch file. I created a batch file and the java program is running too.
Now I want to do error handling to see if there was any error.  So I want the bat script to capture non zero return codes & display if the run was successful or not.
Can you let me know how this can be achieved? Do I need to do this handling in Java program or the Batch file?
michaelm702Asked:
Who is Participating?
 
VenabiliConnect With a Mentor Commented:
System.out.ptintln("3:Some message") for example :) when you do this, the text get sent to the out which in this case is the batch file so you can get it and do something based on it.

System.err will also do the trick - depends on which one you prefer and use for something else.

Now being fully awaken, you actually have a better way:
System.exit(3)

Then in the batch file:
java YourClass
set exitcode = %ERRORLEVEL%
echo %exitcode%

keep in mind that System.exit exits the Java code. And depending on the OS, if the commands extensions are not installed, this won't work either. In which case you are back to using System.err/System.out
0
 
mdshortCommented:
Here is an example of batch error handling.
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Windows/Q_23868880.html
cd c:\doesnetexist || goto ERR
 
echo Never have to see this
goto :EOF
 
:ERR
echo Error!

Open in new window

0
 
VenabiliCommented:
Now - as we are talking about Java...
The main method of your program always returns void... which means that you effectively have no exit code from there.

What kind of errors are you trying to catch? In most cases you will need to write the error handling in the Java code and then to eventually handle it in the batch file (for example the Java code writing to the System.out a message and the batch file handling it). Another option is just to redirect the out to a file when you start the code.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
michaelm702Author Commented:
Hi Venabili,

My prgram has various catch blocks for the errors it might encounter. So I want to catch these errors.

You said:
I most cases you will need to write the error handling in the Java code and then to eventually handle it in the batch file (for example the Java code writing to the System.out a message and the batch file handling it).
Can you provide me an example if possible?

0
 
CEHJCommented:
>>
Now being fully awaken, you actually have a better way:
System.exit(3)
>>

;) That's the way
0
 
VenabiliCommented:
>CEHJ

Yep - but my sleeping self was refusing to remember this before the first coffee... Ooops. At least I did remember at the end. Note to self: No answering before coffee.

michaelm702,

Any troubles? Any aditional information needed? :)
0
 
michaelm702Author Commented:
partially resolved
0
All Courses

From novice to tech pro — start learning today.