Java: How to stop a Executable jar which never terminates - java

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.

Related

Run java process as background process in linux

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.

Batch - How to wait for process with specific name so start and finish?

I'm creating a launch script to start a game launcher which requires a specific Java version. It currently looks like this:
#ECHO OFF
echo Enabling Java 7...
SET JAVA_HOME=C:\Program Files\Java\jre7
echo Active Java version location: %JAVA_HOME%
echo Starting ATLauncher...
start /wait ATLauncher.exe
echo Launcher started!
It just sets the Java version to Java 7 (changes the JAVA_HOME environment variable to the path to the Java 7 jre) and starts the game launcher (ATLauncher.exe) and waits for it to finish. This does what I want it to, except one thing: The process ATLauncher.exe is just a "starter" which starts a debug console and a Java application, which prompts the user to select something to play, and then starts the selected application.
This brings two problems:
1: All applications started after the ATLauncher.exe process are Java programs, so their process names are just javaw.exe, so it's hard to identify them.
2: The batch file closes when the ATLauncher.exe process stops (which is what I expect), but I want it to run until the last of the launched Java processes has been terminated, and then run some more commands, and then stop.
Here is the "flow" I want to achieve:
The script is started.
The JAVA_HOME variable is changed.
The process ATLauncher.exe starts.
The process ATLauncher.exe starts two javaw.exe processes.
The process ATLauncher.exe stops.
One of the javaw.exe processes starts a 3rd javaw.exe process.
The 3rd javaw.exe process stops.
The 1st and 2nd javaw.exe eventually stops, or the "flow" begins from step 6 again.
The script executes some more commands and stops.
I hope this is clear enough! Just tell me if I have to explain something a bit clearer!
Thanks!
Try this:
:loop
rem wait 4 sec
ping -n 5 localhost >nul
tasklist /fi "IMAGENAME EQ javaw.exe" /fi "STATUS EQ RUNNING" | find /i "javaw.exe" > nul && goto:loop
rem more commands here
I could suggest solution to one of your problem of identifying java process
While starting your java application provide JVM argument with -D option
something like this
java -Dappname="appname" classname
Later on while searching your application through ps or some other command put a grep on appname.
hope this helps.

Why the java process is not closing when the app is finished?

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

How To stop an Executed Jar file

It feels like a dumb question to ask, but i cant seem to figure it out. when i run a *.jar file on windows it doesnt apears in the taskmanager processes. how can i terminate it , i have tried TASKKILL but it also doesnt work for me.
On Linux
ps -ef | grep java
It will show u a list of processes out of which one will be your executable jar. Just kill that process by its process id.
sudo kill -9 <pid>
Is there any way to do this from the java code of the same jar file. Like killing itself once process completed.
Find the process id by jps command & and kill them by taskkill command.
Note that "-f" is required with taskkill or it may just send a termination signal not actually terminating it.
You can identify the process in taskmanager by looking for "java" or "javaw" processes. The problem will be in case you are running more than one java processes. If you are able to identify your process, simply kill/end it.
Other way around:
Run
jps -lv
which shows PIDs and command lines of all running Java processes. Determine PID of the task you want to kill. Then use command:
taskkill /PID <pid>
to kill the your jar process.
Did you try to kill the java.exe processes in the taskmanager? It should stop then.
you could open jvisualvm to see the running java-processes. the process-id is displayed there. now open the task-manager go to the processes tab and add the process-id column to be displayed. now you can select the right java.exe or javaw.exe to kill
As everyone stated it is either java or javaw process. The problem is when you're running multiple apps like that. One workaround might be naming the process differently as stated in:
How can I set the process name for a Java-program?
spring boot start/stop sample (on Windows OS).
start.bat
#ECHO OFF
call run.bat start
stop.bat:
#ECHO OFF
call run.bat stop
run.bat
#ECHO OFF
IF "%1"=="start" (
ECHO start your app name
start "yourappname" java -jar -Dspring.profiles.active=prod yourappname-0.0.1.jar
) ELSE IF "%1"=="stop" (
ECHO stop your app name
TASKKILL /FI "WINDOWTITLE eq yourappname"
) ELSE (
ECHO please, use "run.bat start" or "run.bat stop"
)
pause
If you run the JAR file By command line and it is running yet. Press,
In Windows:
• Ctrl+C: shuts down the process, (it might be needed administrator privilege)
In Linux:
• Ctrl+ C : politely ask the process to shut down now.
• Ctrl+ \ : mercilessly kill the process that is currently in the foregroun.
In windows task manager you will see process called "java.exe". Kill that process your application will get stop.
To know the process first go to applications in task manager and then go to process by selecting that application. It will lead you to exact process of that application.
Regards,
Jaynil
if you are using a jframe and you want your application to stop when you click the "X":
here's a tutorial: http://tips4java.wordpress.com/2009/05/01/closing-an-application/
This is probably the easiest way to kill the process with no external dependencies (jps or anything).
wmic Path win32_process Where "CommandLine Like '%YourJarName.jar%'" Call Terminate
via How can we stop a running java process through Windows cmd?

using javaw to run jars in batch files results in more than one java processes in process explorer - XYNTService

I have a somewhat strange issue. I have a java application that installs few services that run as Jars. Previously I used installed Java to run these Jars. There are four services and all will be instantiated from a single batch file with sequential call to each other. Something like this,
start %JAVA_HOME% commandtoruntjarfile
would work and all four services will run in the background and only one java.exe visible in process explorer. So I had another service installed as windows service which would start stop these services by calling the run bat or shutdown bat.
Now the customer requirement changed to using an internalized version of java. I extract java to a location, make our own environment variable name "ABC_HOME" and the required syntax in batch changes to
%ABC_HOME%\javaw commandtorunjarfile
When its run it works. but there is no stopping these. When I go to process explorer I see 4 java.exe running each for the four run commands in the batch file. If I stop the windows service all the four keep working. If I restart the windows service the number of java.exe in process explorer goes to eight and keeps going up until windows has had enough of it.
How do I get around it? I think the solution should be to have the one java process in process explorer but I cant seem to find any solution for that.
[EDIT]
The four sub services are actually XYNT processes. In the normal scenario it would be something like this
[Process1]
CommandLine = java -Xrs -DasService=yes -cp jarfiles
WorkingDir = c: bin scache
PauseStart = 1000
PauseEnd = 1000
UserInterface = No
Restart = Yes
For using java from a specific location the following change was needed
CommandLine = %JAVA_PATH%\bin\java.exe -Xrs -DasService=yes -cp jarfiles
but this wouldn't work as it would not accept the path variable in XYNT.ini file. so I called a batch file here and in that batch file I used the above code. So here is what the change looks like,
CommandLine = batchfile.bat
and in batchfile.bat
%JAVA_PATH%\bin\java.exe -Xrs -DasService=yes -cp jarfiles
Usually, every Java program run on your system has its own virtual machine running, which means: one java.exe/javaw.exe per instance of your program.
I can not tell why it "worked" from your point of view with java.exe like you described first, but the behaviour you described for javaw.exe (having 4 java processes in the process explorer) would be what I'd have expected.
For me the question is not why you're seeing 4 vs. 1 java processes, but how you can start/stop the "services". Killing the Java VM externally doesn't seem a very good solution. I'd consider building some IPC into the Java services that allow you to gracefully terminate the processes.

Categories

Resources