i have googled my heart out! I am trying to figure out how to output any errors a java class might give when executing java from the Windows command line.
For instance
java -jar class.jar <someFile.file>
if that line throws any errors, i want them to be stored into a text file for later reviewing.
I tried
java -jar class.jar <someFile.file> >> log.txt
But despite throwing errors the log.txt file is empty.
Thanks all!
Use:
java -jar class.jar <someFile.file> 2>> log.txt
The 2 redirects the error stream.
or you can use bat file to easily direct all output to a text file
in command prompt
C:> something.bat > console_output.txt
Related
I have a python code that launches jar file
subprocess.call(['java', '-jar', 'jarfile.jar']
The jar file prints some errors to the console.
I can't modify jar file.
How can I block those warnings?
Still, I want my python output to be printed in console. Just those jar warnings.
This could be seen as more of a shell question.
You could throw the output into /dev/null
java -jar jarfile.jar > /dev/null
I am running a command line program as follows:
java -jar myjar.jar -host localhost MonkeyProject/BAT_Login_Online_V1.mt
Which prints the following output:
result: OK
I am running the above command in a loop and want to read the output printed on the console in my script to make some decisions.
So how do i read it in the script?
If using *nix:
var=`java -jar myjar.jar`
The output of the java program will be stored in var
If using Batch, you can do it by outputting into a file, then reading it back into a variable like this:
java -jar myjar.jar > file.txt
set /p var=<file.txt
I normally compile things through the command line using:
javac -classpath . Test.java
Similarly, I run them through:
java -classpath . Test
I'm now attempting to save myself the trouble of typing these out every time through batch files. I have attempted to do so through another question from here:
Creating a batch file, for simple javac and java command execution
I've also tried my own way:
cmd.exe
#echo off
javac -classpath . Test.java
Still no luck, however. I have checked that my PATH environment variable is correctly pointing to the latest version of jdk and as I've said, I can compile just fine directly through command line. Upon running the batch file, I just get the command prompt with no error; as if there was nothing under cmd.exe. Could anyone lend a helping hand and slap some sense into me?
When you write cmd.exe, that will start a new command prompt. You don't want that.
When you write #echo off, that means nothing will be printed on the screen after that point. That's what it means. That's what it does. That is why it looks like nothing is happening.
Something would be printed to the screen if you had a compilation error, but probably you don't.
If you want the command prompt window to stay around instead of disappearing, I believe there is an option in Windows to configure that, at least there was when I last used Windows, back in the mists of time.
this worked for me. I think it does what you were looking to do.
This is the code I suggest for the .bat file:
cd C:\Users\John\JavaApps\folderThatContains.java //points terminal to folder
javac Main.java //This compiles .java in said folder
cmd /K "java Main" //cmd /K prevents terminal from quitting after "java Main"
I have test.jar which just contain a single class and it has a main method. That just prints out some string to std out. I need to run this jar using .bat file. So I used
#echo off
start java -jar E:\FYP\jartest\out\artifacts\jartest_jar\jartest.jar %1
in my script.
When I run the bat file the output does not print to the same console. Instead it is opened in another window.
( What I understand is I should put some 'echo variable' where the output of the java program should be assigned to 'variable'. Or There might be some other ways ).
What should I do?
Thank You !
The start is what is causing the new window to pop up and the output is going there. Change your bat file to say this instead:
#echo off
java -jar E:\FYP\jartest\out\artifacts\jartest_jar\jartest.jar %1
You can also remove the #echo off if you want. If you remove that line, the windows shell will print out the lines it's executing in your bat file. I'd personally leave #echo off there except when you're trying to debug the bat file.
is it possible to launch a program in my shell which I wrote with eclipse ?
For some reasons, I don't want to use the eclipse console so is it possible ?
Absolutely. Export your program to a jar file (your-project.jar) and run the main function of the desired class by calling the command:
java -cp your-project.jar packagename.classname
You can even redirect the outputs (stdout, stderr) to a file by:
java -cp your-project.jar packagename.classname > outputFile 2>&1