Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

read batch file run statements

Posted on 2014-03-08
9
Medium Priority
?
765 Views
Last Modified: 2014-03-21
I have a batch file which runs java program and while program is running I see a lot of messages on command prompt coming from my java code. I want to know if there is any way I can get those messages in a batch variable?
e.g. messages ffrom my java program are:
Started to read database
processing employee table
found user
Finished successfuly


How can I capture these while my program is running in batch mode.
0
Comment
Question by:michaelm702
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39914618
Since these are multiple line type messages, how do you want them in the variable?  There is no easy way to store a line feed in a variable between lines, but you could place some other delimiter like "|".

Actually, it would be helpful to understand what you intend to do with the variable after the java program runs?  If you are going to search it for particular strings or messages then I would actually recommend sending the output to a temporary text file first, and then you can use FIND or FINDSTR against that file to look for errors, etc.

You can send all output from the java run to a file like this:

java myprog.java >c:\temp\log.txt 2>&1

Open in new window

The "2>&1" captures error messages in addition to standard information messages.

If you really did want to place all output in a single variable you can do the following, but I think it will be very hard to work with:

setlocal EnableDelayedExpansion
for /f "tokens=*" %%A in ('java myprog.java') do set Output=!Output!;%%A

Open in new window

~bp
0
 

Author Comment

by:michaelm702
ID: 39914715
basically my intent is find if there were any issues during the program run. So I would be interest in looking for the string successfully finished or program failed.
If I write to file can I look for these message right after the program finishes in my same batch file?
OR can I just store the last message recived from java program in a variable?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39914849
if errorlevel 0

Open in new window

will tell you if java.exe exited cleanly. You can capture the output to a log file with


java Foo >java.log 2>&1

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 57

Expert Comment

by:Bill Prew
ID: 39915360
ERRORLEVEL is often the way a BAT file checks the result of a called program.  But it is dependent on the called program playing by the rules, and returning a non zero exit code if an error is detected.

Do you have control of the java code, if so you could make sure it returns non zero exit codes on errors.

Yes, you can search the output log file in the same BAT script right after the java program finishes.  But you would need to identify what text you would look for to indicate success or failure.

~bp
0
 

Author Comment

by:michaelm702
ID: 39915394
i dont have access to the code but I know specifically what message the program will give if success or failure and will be one line at end.
can you provide a sample of batch file which will call java program and look for the specific success or failure message?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39915545
But it is dependent on the called program playing by the rules, and returning a non zero exit code if an error is detected.
That's a good point. I'm afraid to say that Java authors often lose sight of the importance in some cases of returning a proper exit code.

You could probably pipe the output (redirecting stderr to stdout first) through the 'find' utility, but not being hugely au fait with the intricacies of batch files (and not wanting to be ;)) i think people like Bill could probably tell you more
0
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 39919376
Here is a simple technique you could use.  You can check for either strings that indicate failure, or ones that indicate success.  The logic in the && block would be executed if one or more strings are found, where as the || block is executed if no strings are found.  And of course you can add the /V switch to the FINDSTR to reverse the logic...

java myprog.java | findstr /i /c:"string 1" /c:"string 2" && (
  echo String found
) || (
  echo No string found
)

Open in new window

~bp
0
 

Author Comment

by:michaelm702
ID: 39944657
Instead of echo "No string found" can I print the output from program here?

java myprog.java | findstr /i /c:"string 1" /c:"string 2" && (
  echo String found
) || (
  echo No string found
)
0
 
LVL 57

Expert Comment

by:Bill Prew
ID: 39944924
Yes, this should do that.  We need to save the output from the java program to a temporary file, and then can display it with the TYPE command.

java myprog.java > "%TEMP%\myprog.txt"
findstr /i /c:"string 1" /c:"string 2" "%TEMP%\myprog.txt" && (
  echo String found
) || (
  type "%TEMP%\myprog.txt"
)

Open in new window

~bp
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question