I write a java program which is run in the background. And it works fine,it does what is waited from it.
I write a bat file in windows to run it.
#echo off
start .\jre7\bin\java.exe -jar ".\my_jar.jar"
exit
When I run this .bat file I can see it on task manager and It works and when it finished , the java.exe process is closed on the task manager.
It works fin on Windows
But When I run it on linux in .sh file,
It the java program works fine because It does what I wait from it ,but on the Sytem Monitor
the java process is not closing. I want it to close by itself like Windows.
in sh file:
export JAVA_HOME="/app/myfolder/java/jre1.7.0_51"
export I_HOME="/app/myfolder/code"
cd $I_HOME
$JAVA_HOME/bin/java -jar my_jar.jar
RStat=$?
What is the problem?
You can also terminate the application with:
System.exit(0);
Its definitely not normal for it to terminate gracefully on windows and not on linux.
Your jar could just be hanging on linux due to differences in whatever native libraries you're using, have you tried calling System.exit()?
Have you set your thread to a daemon?
public final void setDaemon(boolean on)
From the JavaDoc:
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads
Related
I have a .bat file which has command to run a Java console program.
I created windows scheduler task to invoke the bat file.
When I run the windows task then it invokes the java program, but when I stop the windows task (Right click the task, Select option End) then my java program do not stop.
My question is how to stop an external program which got started from the bat file. Is there way to capture the End event/signal from task which can be passed to bat file or java program to stop the program.
I got similar question here https://superuser.com/questions/1054534/stopping-a-scheduled-task-with-a-batch-file-action-doesnt-stop-the-program-runn
but it is unanswered.
Atul
I made a program in java that changes your windows wallpaper at a certain time of the day, this runs the UI and a thread in the background that checks if the wp should be changed or not.
I'm trying to figure out a way to keep track of that background thread and close it using a .bat file.
I run the program like this:
start javaw -jar wchanger.jar
And this is the solution I came up with:
taskkill /f /im "javaw.exe"
The problem with this is that closes every javaw process running on the pc.
I though I could name the process whatever I want, but that doesn't seem to be possible on windows.
I am using a Windows executable to do some optimisation tasks. This executable is run from a Windows Command Prompt (cmd). It reads a batch file that tells it how to run a model interface (it will need to run it multiple times) which reads and writes text files for input/output between it and the optimisation .exe program. This model interface is written in java and has been successfully compiled. My batch file looks like this:
java -cp .;"C:\Program Files\location_of_required_jars\*" name_of_class_file
The java program executes and completes without issue.
My issue is that the executable I am running needs to know when java has terminated. At the moment it hangs after 1 run of the model interface java program, waiting for the java ... line to complete.
Can anyone provide advice on how to get java to inform cmd that it has completed?
If the batch file does not progress further after running first java command that means that java process has not actually finished. You would need to diagnose the problem, I would suggest to first make sure that it indeed does not finish by adding some echo statement after java invocation. Next you would need to run jps command, which is part of JDK. jps will report PIDs of running java processes and then you do jstack <pid> to find out what is preventing your java program from terminating. It can be that main thread did not actually complete or some non daemon threads are still running.
I have a runnable jar which won't shutdown. i have executed it in my windows machine by double clicking on it. How can i terminate that application.
I dont what to kill all java.exe processes as i want other apps to run
There are three options:
Start in commandline with java -jar [YourJarPath] (Close by (Ctr + C) or close your commandline
Restart your Windows
Terminate it per TaskManager (Ctr+Alt+Del)
Under "Details"
Then look for "javaw.exe". Right Click and then something like Shutdown or exit Task.
To find the jar name, start in cmd (commandline) this code:
C:\Program Files\Java\[YOUR JAVA VERSION]\bin\jps.exe
it will return something like this:
The number befor the "name" is your PID that is also listed in the taskmanager right next to javaw.exe
jps -v
Will list all running jars along with their PID. Then you can just kill that process (with kill $PID)
JVM starts new java.exe process each time you run jar. Obviously because jar starting is performed as java -jar <jar_name>.
So by locating exact process which holds your jar's process you can safely kill without worrying about rest java apps.
I am running my project as jar using java -jar command in Linux machine. As soon as this program run , It produces logs in another directory. Running my program this way requires me to keep the shell open. Now If I have to see the logs , I can't do that in the same shell. I am forced to do that by either doing the duplicate session or new session. Is there any way I can run the jar as background process and see the logs in the same shell ?
If you don't care about it staying alive, something as simple as nohup java -jar myjar.jar & should work. If you need it to be automatically restarted if it crashes or start automatically at boot, you'll want to look into something like systemd or monit.