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
Related
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 m using Linux.
I want to call a small executable application from my java command line which is called "wmic". It needs an input query. Output are stored in text file in the specific directory.
When I use the command in Linux Terminal
echo "Hello World" >> /home/kannan/hello.txt
the output is stored in hello.txt file.
but when i call this command from java
Process p = Runtime.getRuntime().exec("echo \"Hello World\" >> /home/kannan/hello1.txt");
the output is not created any hello1.txt file
Please any one help me.
Thanks in Advance.
Use a ProcessBuilder. It makes it easy to redirect output of a command to file as shown below:
new ProcessBuilder("echo", "hello").redirectOutput(new File("output.txt")).start();
If you want to append to the output file:
new ProcessBuilder("echo", "hello").redirectOutput(Redirect.appendTo(new File("output.txt"))).start();
What you are executing is bash command (echo). Your java program do not work as bash interpreter
To execute any script which requires bash or shell scripting features, your need to execute that interpreter
To solve your problem you can follow below steps
1. Write your string into temp .sh file. Lets call it temp.sh
2. execute below using Runtime.getRuntime().exec
Process p = Runtime.getRuntime().exec("bash temp.sh");
bash will try to execute any command in temp.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.
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
I wanted to know if there is a way, from a sh script, to input value in a Java program. For example I have a program.jar. I do java -jar program.jar and it outputs:
Enter your name:
Would it be possible to write an sh script like that:
java -jar program
echo name
where name is gonna be the input for the program?
To echo data into your java program from the shell do something like this:
java -jar program<< EOF
<your-data>
EOF
In addition to #tcb 's suggestion, you can also use a standard input redirection pipe < in order to specify a file that will be the input of the program:
input.txt
name
And in the sh:
java -jar program < input.txt
Use pipes:
echo name | java -jar program
If you mean interactive input , expect is always one of the answers.