Keep info of new javaw process to close it later using cmd - java

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.

Related

Java: How to stop a Executable jar which never terminates

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.

Program started from batch file not showing GUI when executed within Java

I'm new of Stack Overflow but used it so much to learn lots of things. I looked a lot for a solution of my problem and with no luck.
Anyway, the situation is:
I have a servlet that must execute a batch file to execute some operations such as start a program.
Here is the code o the servlet:
StringBuffer batchCommand = new StringBuffer(TerminalProperties.getRunningStorePath());
batchCommand.append("file.bat");
Runtime runtime = Runtime.getRuntime();
batProcess = runtime.exec(batchCommand.toString());
The content of the batch file is:
taskkill /f /im program.exe
cd C:\folder
start /MAX /WAIT /HIGH program.exe
Everything goes well when I run the batch file from a command line or with double-click. If the program.exe is running, it closes and then is restarted. When it executed from java servlet everything works but the program is started IN BACKGROUND; I need it to show the GUI.
I found some problems similar but related to task scheduler, that is not my problem.
I tried use runas /user:Administrator program.exe in the batch file but with no success.
Tried also to start the program with java code:
runtime.exec("cmd /c \"cd C:\folder & start /MAX /WAIT /HIGH program.exe\"");
but nothing happens...
Any ideas how can I fix it? Thanks for your time.

Batch Restart Java.exe

Java.exe often crashes or is unresponsive when using the Filemaker Console.
We need to kill the process and start it again manually in order to make it work properly again.
I found that I can use the command taskkill /im java.exe /F to kill it, but I can't seem to start it again with the batch file.
I will change directory to Program Files\Java\jre1.8.0_91\bin\, then I will type java.exe, it shows me all the switches as if i type java.exe /?
But if I do a search in Windows, I see java.exe as being the result, and I click it with my mouse, it starts correctly..
How can I start java.exe correctly with the command line please
What are the correct switches to start java.exe from command line?

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?

Categories

Resources