I've developed a Java console application that when start, open a console window and remain in foreground, i want to start that application in background .
Now i launch the application by this command line :
java -jar myapp.jar
Is there a way to achieve this behaviour ?
It's enough change the command line parameter or i need to do some change on my code ?
The answer is operating system dependent.
*nix: <your command> &
Windows: (opens a new console): start <your command>
Windows: (doesn't open a new console): start /b <your command>
If you are doing this in anything unix based then you can append & to the end which will spawn a new thread and keept it running in the background.
java -jar myapp.jar &
If you really just want it to run in the background, java -jar myapp.jar & will do the job. That way, it'll still die when the shell closes, but you can keep using your shell.
If you really want it run as a daemon, nohup java -jar myapp.jar & will do the job. That way, it'll continue to live when the shell closes.
If you want this to be reliable, you can prepare an init script or upstart job definition, or run it via Vixie cron(8) #reboot specifier to make it start at boot.
Given that you're using Windows, you might consider Java Service Wrapper. I have used it on a project in the past.
Related
There is windows process which is actually a java application (java.exe). I need to close it from within other java application (other running .jar) which is probably will also be named as java.exe in windows taskbar.
To do the task we would normally do it this way
Runtime.getRuntime().exec(KILL + serviceName); // where serviceName is a `java.exe`
But there would be some java.exe processes in windows taskbar. So how to find the one I need? In this case it is forum.jar.
Actually this jar was started from .bat file (if it makes any difference) - it is process.bat as it seen in the screenshot.
You could run
wmic process where caption="java.exe" get commandline,description,processid
to get the list of processes and their associated command line like in your screenshot. Then you get the ID of the command you want to kill and kill it with taskkill /pid xxx.
For example, I was trying to start a java process
in background and ignoring SIGHUP.
The first way I tried is to write a shell script like
start.sh:
java -jar *.jar
and execute
nohup ./start.sh &
the process starts, but does not work as expected.
Another way I try is to direct execute nohup java -jar *.jar & in command line, which works well.
So I want to know what's the real difference that makes the first way not work as expected.
I am running a bash script that, among other things, runs a java program that can be used via GUI or via command line (depending on a parameter).
splitstree --commandLineMode --commandFile comm.txt --version --verbose
EDIT:
When I run it via normal command line or via GUI, it works perfectly. If I echo this command into a file and $(cat file) it also works, and it works as well when I integrate it into a bash script and run the bash script.
If I qsub it to the cluster where I am doing the work, I get an error about a missing display:
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed
an operation which requires it.
I tried to export DISPLAY=:0.0 within the bash script but the error didn't change.
EDIT 2:
If I pass the DISPLAY variable to the qsub command, the error goes away but the program terminates with an exit status of 1. Like:
qsub -v DISPLAY <job_file>
It also says Picked up _JAVA_OPTIONS: -Xmx4096M but performing unset on this variable didn't change the exit status (so probably is not harming the process).
Re-running the same command outside of qsub (that is: simply copy-paste the cmd into the shell) work perfectly again.
Any suggestion on how to make it so that the qsub command correctly passes the display information to the cluster node?
If you don't need the display run Java with -Djava.awt.headless=true property, as explained in Using Headless Mode in the Java SE Platform.
Alternatively, if your program can't run headless, you can try using Xvfb (X virtual framebuffer). It comes with xvfb-run command, take a look at Running without a Display wiki:
xvfb-run java MainClass
or by configuring DISPLAY environment variable:
sudo Xvfb :1 -screen 0 1024x768x24 </dev/null &
export DISPLAY=":1"
java MainClass
When in a headless environment, you need to use GraphicsEnvironment.isHeadless() in your code to avoid doing anything that requires AWT components. That means you can't do any input/output, of course.
I want to make job on Jenkins that starts server (MockServer on WireMock).
Server is launched from *.jar file, from terminal like that.
java -jar serverLaunch.jar
It takes over my console. To avoid that I modify this and do:
java -jar serverLaunch.jar &>/dev/null &
And that works for me on my local PC. Now I want to move it to Jenkins.
If I try to do this from "Shell command" block in Jenkins Job then:
a) java -jar serverLaunch.jar
I have task locked in queue in my Jenkins and I don't want that but server starts and works.
b) java -jar serverLaunch.jar &>/dev/null &
Job ends with success but my server is not alive.
I have wrapped this command also in .sh script and .rb script. Any idea how to make it work?
I've tried this:
https://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build
And then in Jenkins "Shell script":
daemonize -E BUILD_ID=dontKillMe /bin/bash launch.sh
But it also passes but server is not alive.
I had to check "Inject environment variables to the build process" and add:
BUILD_ID=dontKillMe
Now it is working.
Try using nohup e.g.:
nohup java -jar serverLaunch.jar &
That should prevent the process being terminated when the parent shell process exits (which I suspect is your problem).
Another effective approach would be to add a post-build action that executes a shell spawning the server.
What's the best way to restart a java app in ubuntu? I know u can run commands like this one from the terminal, but it doesnt seem to be working...
String restartArgs = "java -jar \'/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar\' --Terminal=true";
Process restart = Runtime.getRuntime().exec(restartArgs);
System.exit(1);
You are killing the parent process with System.exit(1), so its child process is destroyed as well.
To restart you would typically provide a shell script wrapper to launch the actual Java app.
#!/bin/sh
restartCode="1"; # predefined restart signal code
java -jar '/home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar' --Terminal=true; # run java program
if [ $? -eq restartCode ] # if exit code is equal to predefined restart signal code
then
$0; # restart script
fi
exit $?;
Note the above code is a rough, crude outline. Typical wrappers are far more complex to deal with commandline arguments passed to the startup script itself etc. etc. Plus, my sh-skills are not infallible.
try providing full path for JAVA_HOME (e.g /usr/lib/jvm/java-6-sun/bin/java instead of java). The exec does not have Shell enironment variables.
also use
restart.waitFor(); //wait until process finishes
to make sure Java does not exit before the process finishes.
If you do want to run in shell (and use shell specific stuffs like pipe and ls) do this:
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("java -jar /home/fdqadmin/NetBeansProjects/dbConvert2/dist/dbConvert2.jar");
SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
int result = commandExecutor.executeCommand();
commandExecutor.waitFor(); //wait until process finishes