public class Test200 {
public static void main (String args []) {
System.out.println("David");
System.out.println("Peter");
}
}
output=$(java Test200)
echo $output
I get the both value which are David Peter. Let say I only want "David" to be returned in shell script ? Any clue ? Thanks.
You don't return "David" and "Peter", you print them to STDOUT. So if you only want to print one of these, just remove the other println call.
You can only return integer values to the shell. This is done by System.exit(status).
Try using grep command so that it will return the expected value alone for multiple values use egrep
output=$(java Test200)
instead use
output=$(java Test200 | grep 'David')
But i didnt tested this code, it should work.
or try this
java Test200 >> outfile
all the SYSOUT will redirect to outfile base on that you can do te operation with external script.
Related
I am making simple console app. My main loop looks like this.
var s = new Scanner(System.in);
while (appShouldRun) {
var action = s.nextLine();
// proccess action
}
It works fine when I run app with empty stdin.
java -jar app.jar
But I want to have some prepared scenarios in file. Run them and then wait for next user input.
Expected behaviour:
java -jar app.jar < scenario.txt
output of commands from file
| <- cursor, waiting for user input
// scenario.txt
cmd1
cmd2
cmd3
.
.
.
Problem is that when I run program with something od std in, it process commands correctly, but then it throws
java.util.NoSuchElementException: No line found
I think it's becouse stdin is closed after all lines from file is processed.
Am I wrong?
How to fix the code to get expected behaviour?
Thanks
When you redirect standard input with <, it is connected to the input file, and not connected at all to the console.
If you want your program to receive input on the console, then you can't redirect stdin. You should take the filename as a command-line argument instead. Open it, read it, close it, and then process stdin.
OR, if you really want to, and you're on a unix/linux/macOS system, you can run it like this, using the cat command to concatenate standard input to something else:
cat scenario.txt - | java -jar app.jar
I have tried do change as little code as possible. This reads the whole file like you wanted and ends without Exception
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean appShouldRun = true;
while (appShouldRun) {
String action = s.nextLine();
System.out.println("got: " + action);
appShouldRun = s.hasNext();
}
}
}
I have written a shell script(.sh) which calls a java program. After some computation the java code has an array. I want to return this array back to the shell script. Is there any method by which it is possible? Thank you for all the help
Inside the java use System.out.println("a b c"); with the variables of your array.
Call it from .sh something like this:
#!/bin/bash
output=$(java -jar myexecutable.jar)
array=($output)
for i in "${array[#]}"
do :
echo $i
done
I am running java program from shell script. I need to execute next step in same shell script based on if any exception occur in java program or it ran successful.I am aware that java doesn’t run anything. How can we do it in shell script ?
I know that we can print some string in log and grep it to check the status. is there any better way to do this ?
It would be good even if I can get some elegant way to do it using grep.
If a Java program was terminated because of an exception, the JVM will exit with an error status 1, instead of the usual success status 0. For example, this program exits because of a NullPointerException:
shell$ cat Throw.java
public class Throw {
public static void main(String[] args) {
String s = null;
System.out.println(s.length());
}
}
shell$ java Throw 2>/dev/null; echo $?
1
You can use the exit status in a shell if-statement for example. This will print boo:
if java Throw 2>/dev/null ; then
echo woo
else
echo boo
fi
If you want to set an exit code other than 1, use the System.exit(int status) function.
consider the following java program:
class PrintName{
public static void main(String[] args){
System.out.println("Hi " + args[0]);
}
}
Now I just compile and execute it from console (I'm on Ubuntu server):
~$:javac PrintName.java
~$:java PrintName "Fernando"
I get the next output:
Hi Fernando
I know there are commands like 'yes' in Linux, with which I can get infinite stream of data. My idea is to do something like this:
yes "Fernando" | java PrintName >> my_file.txt
I want to be able to pass "infinite" Fernando's to my program and have it run infinitely many times, then be able to manipulate the STDO to redirect to some file.
I don't know if I explained it clearly, sorry for my poor handling of the English language. Thank you very much for your time.
This is where you want the xargs command:
yes Fernando | xargs java PrintName
xargs takes each line of stdin, and passes it as command line arguments to the given command.
To redirect it to a file, you can wrap that in a grouping construct:
{ yes Fernando | xargs java PrintName; } >> my_file.txt
I have a Java program which has a static method
private static int checkURL(String currentURL)
From my perl script, I want to call this method and get return value of this method.
I have a constraint that I can't use any inbuilt Perl modules offered by CPAN.
I am thinking to call Java through system() command in Perl but issue is how to call this method and get the return code?
call external command and get back result (straight from http://www.perlhowto.com/executing_external_commands)
#-- scalar context
$result = `command arg1 arg2`;
Using Java module
use Java;
my $java = new Java;
my $obj = $java->create_object("com.my.Class","constructor parameter");
$obj->checkURL("http://www.google.com/");
$obj->setId(5);
Iniline::Java is also a famous module for Java/Perl integration.
Edit: I have a constraint that I can't use any inbuilt perl modules offered by CPAN.
Oh sorry I didn't see that. I had tried something like below some time ago.
Hello.java
class Hello {
// Your program begins with a call to main().
public static void main(String args[]) {
System.out.println("This is a simple Java program to test return code.\n");
System.exit(100);
}
}
test.pl
#!/usr/bin/perl
use strict;
use warnings;
my $command = "java Hello";
print "Command is $command:\n";
system($command);
my $retval = $? >> 8;
print "The return code is $?\n";
print "retval is $retval\n";
Run it
perl test.pl
Output
Command is java Hello:
This is a simple Java program to test return code.
The return code is 25600
retval is 100