How to Capture Java -jar command's OS Output in Linux? - java

I am running java -jar xyz.jar command on the linux terminal. I have both system.out.println statements and System.exit statements in the code.
how to capture the system.exit or output to the OS on the linux? do i need to prepare a linux script for this?

This may be off-topic for SO.
You just redirect it:
java -jar xyz.jar > the_file_to_output
If you used System.err output as well as System.out output and wanted to redirect both, it's a tiny bit more complicated because you need to redirect out to the file and then redirect error to out:
java -jar xyz.jar > the_file_to_output 2>&1
The 2>&1 thing is what redirects error (standard stream #2) to out (standard stream #1). Note that the order matters, you have to do that after redirecting out to the file.

Java works as any other command.
If you want standard output (or error output) you can work with usual I/O operators: > 2> >> |
java -jar xyz.jar > output.file
java -jar xyz.jar | sort | less
For getting System.exit value you have special variable $?
java -jar xyz.jar
echo $?
To get more info Exit status and I/O redirection

Related

how to run a jar file on the ubuntu 14.04 without stopping when i stopped the putty?

I am using ubuntu 14.04
I am running a jar file which should be collection a large amount of data for a few days.
I am running the jar file thought this command and it works fine.
java -jar xxx.jar
However when i close the putty, the process stopped. Is there a way for a jar file to run even when i close the putty?
You can use nohup to run the jar(any process) in background.
Use the following command in the putty session :
nohup java -jar xxx.jar &
You need the nohup command. This command makes processes keep running despite closing terminal.
Run your jar with (in case you are in the right folder):
nohup java -jar xxx.jar &
I would suggest to you use
nohup java -jar xxx.jar > /dev/null 2>&1 &
which redirects standard error & output of the command to /dev/null which means it's discarded. If you need the console output of this command then you can redirect it to any file as follows
nohup java -jar xxx.jar > output.log 2>&1 &

take stdin from file to control background java process

I would like to run a Minecraft server in the background with Ubuntu, but every time I do it stops running.I am also running bash as my shell.
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &
When I run jobs I get this at first.
[1]+ Running java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &
Then this a few seconds later.
[1]+ Stopped java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui
Later I found out that it will stay "Running" if you redirect stdin/stdout/stderr to some files.
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui > out.txt 2> err.txt < in.txt &
It works great, but I want to use the in.txt file as input for the running java process. When I first run this it executes the in.txt once. So if I have /stop in in.txt it will stop the server. But ONLY once. I would like to put commands in the text file and have that passed to the minecraft server. The file would become my keyboard input while the process is running. I know I can use screen, but I want to see if I can run it with a file as input. Again the in.txt would be my input.
So I could do something like this.
echo -e "/stop\r" > in.txt
OR
echo -e "/time set 0\r" > in.txt
Another rabbit hole.
I even tried making my own pipe. This makes minecraft run in the background.
mkfifo pipe;
java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui &> out.txt < pipe &;
cat > pipe
If I cat > pipe my prompt is taken again, but I can enter commands and it executes them as stdin. So if I do /say hi it will run it. Awesome! One problem. I need my bash prompt back. If I close it by using Ctrl-C, I'll lose my stdin forever! I can keep it around by Ctrl-Z. Later If I need it I'll fg. But that is very crude. I would like to run a script that would stick something into that at a later time. Something like echo "/say hi" > pipe. I lose stdin if I don't keep something running. echo runs once. That's the problem, it collapses stdin.
I am a novice at bash scripting so a little help will be greatly appreciated.
You already got the idea of using a FIFO. You need to run it with the following command:
while true; do cat pipe ; done | java -Xmx512M -Xms512M -jar minecraft_server.1.8.jar nogui > out.txt &
The while loop will make sure that even if the process you use for writing to the FIFO closes the file, the JVM standard input will not be closed. It will just start catenating pipe to it again and the while`s standard output is considered one long stream.

PHP shell_exec() won't execute screen command to run .jar file

I am working on a minecraft control panel in Ubuntu and thus I need to start/stop a .jar filewith shell_exec();
When I try commands like "whoami", the output is normal. But when I try this:
shell_exec("screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui");
It does not do anything, I have checked the permissions too and www-data is the owner of the files
Try to redirect the standard error stream to stdout (by appending 2>&1 to the command), fetch that output and print it to check whether there was a meaningful error message
$cmd = "screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui";
$redirect = '2>&1';
// using variable substitution only for readability here
shell_exec("$cmd $redirect", $output);
var_dump($output);

using rc.local to start a jar file at startup is not working

I have a .jar file I want to run whenever the system reboots/starts, so I put the line
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
in my /etc/rc.local file. The program is validated as working, and if I run the above command at the command line the program works as expected.
Other versions I have tried without success:
nohup /usr/bin/java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar > /dev/null &
and:
nohup java -jar /mnt/fioa/fusion/nfs/labStats/LabInfoAutoLog.jar 2> /dev/null \ .. &
I am running centos 6.4.
Check that your jar file is accesible roots, NFS mounted volumes may impose special restrictions for root.
Instead of discarding your error messages, you might want to route them to syslog, something like 2> /sbin/logger -t FOO 1> /sbin/logger -t BAR
Maybe the path isn't set yet at startup time and you need the full path to the java executable or, possibly, nohup.

Pipe Java to Grep: Why not working?

I am trying to run this dreadfully simple command in Bash
java -cp nasa-top-secret.jar gov.nasa.RocketToMoon | grep -v codehaus
but grep is not working (it does not filter out my string). How can I filter my java output using grep?
The output could be on STDERR, Try this instead:
java -cp nasa-top-secret.jar gov.nasa.RocketToMoon 2>&1 | grep -v codehaus
possible scenario
you actually have all the lines with
"codehaus", so grep -v gives you
nothing. I assume you know what -v
stands for.
your java program did not print
anything to stdout. check your
source and make sure your program spits out to stdout. Otherwise, check if its stderr that your program is spitting out.
possible troubleshooting step:
remove the pipe to grep, run only the java program and make
sure your program has output.
put 2>&1 at the end of the command
and try again with grep

Categories

Resources