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
Related
I've been trying to do this simple script that I wrote where it runs an executable jar file that I made. The command of the script are as follows:
#!/bin/bash
msisdn=$1
java -cp /home/support/phuzca/Migration/PostpaidXMigration_lib/ -jar /home/support/phuzca/Migration/PostpaidXMigration.jar $msisdn /home/support/phuzca/Migration/config.properties /opt/tomcat9/webapps/axis2/WEB-INF/classes/META-INF/PlanID.xml
The jar file works as expected and I receive the expected results:
The idea that I've been trying to figure out is how to prevent those texts from appearing when I run my script, and instead, print them in a file so it can be reviewed later. I hope you could open up some ideas for me. Thank you very much.
Redirect the output to a file:
migrateToPstopaidX.sh > output.log
if you want to redirect stderr use this
migrateToPstopaidX.sh &> output.log
you can use this >> to append the log instead of >
Redirect both stdout and stderr to the output file.
migrateToPstopaidX.sh > output.log 2>&1
You can use >> to append instead of overwriting your file.
Bash executes the redirects from left to right as follows:
>: Open output.log in overwrite mode and redirect stdout there.
2>&1: Redirect stderr to "where stdout is currently going". In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout currently uses.
I'm rank new to bash thus the question.
I've a java program that I've exported as a .jar.
This file when run as
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
How do I write a bash script to do automated testing of this project.
I need the scrip to do the following -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
This should do it.
#!/bin/bash
files="$#"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename it first. Then call it like this:
./filename firstfile secondfile thirdfile etc.
Other:
As sjsam pointed out, the use of <<< is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
1st using executable jar
2nd by running java class from terminal
create a new text file and name it hello.sh
In hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
1 navigate to directory where your HelloWorld.jar is present
2 give permission to terminal to run the bash script by using the following command
sudo chmod 754 hello.sh
3 run you script by running the following command
./hello.sh
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
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