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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

VenabiliCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.