Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

read batch file run statements

Posted on 2014-03-08
9
Medium Priority
?
767 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 59

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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 59

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 59

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 59

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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