Solved

read batch file run statements

Posted on 2014-03-08
9
740 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 53

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 53

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 53

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

770 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