Solved

read batch file run statements

Posted on 2014-03-08
9
734 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 51

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
 
LVL 51

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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 51

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 51

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now