Solved

read batch file run statements

Posted on 2014-03-08
9
743 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 54

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 54

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 54

Accepted Solution

by:
Bill Prew earned 500 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 54

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 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