Input value in Java program from shell script - java

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.

Related

How to handle "<" in args from terminal input in java [duplicate]

I am trying to read the filename by the command line,
This is command that our professor wants us to type:
java MultiBinaryClient xxxxxx.edu 6001 < files.txt
I was trying to use args[3] to get the file name, but args only contains "xxxxxx.edu" and "6001". why not "<" and "files.txt" in the args[]? Can anyone help me out?
BTW, I am using MAC terminal to test my code, I believe my professor uses win CMD, will it make differences?
Thank you!
Let's see what each fragment means. This is how we execute a Java class containing a main method:
java MultiBinaryClient
The only command-line arguments that are being passed to your program are these ones:
xxxxxx.edu 6001
And this snippet is not part of the expected arguments to the Java program:
< files.txt
It's just Unix shell syntax to specify that the contents of files.txt must be read into your program via the standard input.
I know it's an old question, but I've had this problem recently. Here's what I've done to handle it:
Well, as the others said, the "<" redirects the file contents to stdin. If you want to use the file contents as program arguments, you can use xargs:
xargs -a FILE java JAVA_ARGS
or, more specifically:
xargs -a FILE java -cp CLASSPATH CLASS_WITH_MAIN_METHOD
< is a redirection. The file will be streaming over stdin.
You should escape the '<'
java MultiBinaryClient xxxxxx.edu 6001 \< files.txt

reading java jar command console output

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

How to execute shell script and save in text file command through java in linux

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

Shell script call command line application

I have a Java program which I'd like to call inside a linux shell script. The Java program takes a user input from the command line.
I read somewhere that I can use echo to mimic user input as follows:
java myProgram
echo 1000
echo
However this doesn't work for me, the program is still waiting for the user input. Is there something I'm doing wrong? I can't imagine this is a difficult task.
You can use echo, but in a pipeline.
echo 1000 | java myProgram
If you want to send a file, you can use cat:
cat file.txt | java myProgram
Why not just pass in the value as an argument
java myProgram 1000
I think you should find the process id of your java process and then write to its /proc directory.
Say the id of the Java process is 4321, then output to
/proc/4321/fd/0

PHP command shell_exec() not working for my custom Java app5

I have made a custom java program to output a license and am trying to run it in php.
$deviceid="12345";
$command_app = 'java -jar /home/myname/secure/mycommand.jar ';
$privateKey = 'QEFAASCAmEwggJdAgE';
$command_app_args = "\"$privateKey\" deviceid=$deviceid";
$command=$command_app.$command_app_args;
$license = shell_exec($command);
The problem is that $license is empty every time, I tried to print out the $command using
echo $command;
and then ran that command directly in the linux terminal and the xml output was correct.
I am using
System.out.println()
in the java app to print all the xml output. I tried something simple like
shell_exec('ls -l') ;
and sure enough if worked.
What might I be doing wrong?
My first instinct is that the command java is not in PHP's shell path. Try something like this:
$command_app = '`which java` -jar /home/myname/secure/mycommand.jar ';
The command which java will return the full path to the java executable...

Categories

Resources