Is it possible to stop an application in windows using java code ?
I'd like to create a program which is like a shortcut to stop an application on my pc. Is it possible using ProcessBuilder ? Thank you
Plain Java, without native access? No.
ProcessBuilder lets you control processes that you started from within the Java application, but it doesn't give you control to processes that were started by other processes.
As per user988052's comment, you can use pskill from the Sysinternals Suite to accomplish this through Runtime.exec. pskill uses the TerminateProcess function call. You can also call this function with JNI (or some other native caller, like JNA, NLink, etc.), but you'll need to obtain a handle to the process via the OpenProcess function call.
Is it possible to stop an application in windows using java code ? ... Is it possible using ProcessBuilder ?
Only if you launched the application from the same Java application that you are trying to fill it from.
Other than that, you need to resort to running a Windows-specific command to do the killing ... or something based on JNI to make native Windows library calls.
The other complications are that you may not be able to kill certain processes due to permissions issues, and some process may refuse to be killed.
I believe the JRE does not provide an API for system processes. ProcessBuilder can create system processes, but it cannot provide you with a Process object for a process created outside the JVM. I believe that is a philosophical decision on the part of the Java folks, to avoid certain OS-specific tasks where possible. You might think that every OS has pids, or that they all have some kind of kill message, but Java runs on lots of OSes, some of them quite strange.
You could make native code to do it and wrap that with JNI, but that would be wasteful. As Mr 988052 says, I suggest you execute a system-specific command through Runtime.exec or ProcessBuilder. You would need to decide which OSes you want to support, and be sure to get the commands (and the OS-detection code) right for each one.
Related
To ensure the correct function of my Java program I need to know which other Java processes are running on the system currently that I have NOT started.
How do I get information about the other running Java processes?
Is it possible to terminate this Java processes from my Java application? It is NOT possible the exit the whole Java process, because that will terminate my program, too.
Is it possible to write a platform independent source code?
Many thanks in advance!
First: I assume you cant control the other java process, and you have not written it
solution 1: use platform dependant commands to kill the other process. Not safe at all.
Runtime runtime = Runtime.getRuntime();
public Process exec(String[] cmdarray);
see that: Killing a process using Java
Platform independant: NO, because of precedent procedures
Solution 2, safer : use JMX , and it is platform independant
example here:How to stop java process gracefully?
Solution 3:
If you can do something with the other process: try to synchronize something (file, or else), to quit quietly.
I'm writing a program which monitors Java Applications. I'd like to monitor the state of the native processes and know when they exit. This is fine; Java gives me the Process object and I can waitFor() on it.
However, the problem that arises is that if my program dies, I would like it to get the reference to the process back - which it can't do in Java because it never created the process. My first guess was to send the process object over RMI and have the client app send it back when a reconnection occurs, but that won't work because Process isn't serializable.
I can think of a way to do this natively using JNA on Windows, but does anyone know if there is an existing library which will do this?
Cheers!
You can use WMI to implement this on Windows and command line utilities on Unix (if you need).
I know the following tools to work with WMI: JaWin, WIntegra, JInterop.
If your monitor is running in windows too you can use JaWin. It is a JNI wrapper over COM+ API. If you want to be cross platform and be able to monitor Windows machine from Unix machine use JInterop. It is open-source library that implements DCOM protocol in pure java. JIntegra is similar but commercial.
EDIT: probably even simpler solution. If you want to monitor java application just use JMX. Connect to this application using JMX either remotely or locally.
Good luck.
What I would like to accomplish is to start a Java program and have it keep running until the user kills it with a control-C. I realize that it is possible to do this by creating a BufferedReader and having it endlessly loop while reading the BufferedReader, but what I am doing involves me backgrounding the Java program (e.g., java -jar app.jar &) which kills standard in so that method would not work. I've read a bit on Java's daemon threads, but I also do not think that is the correct solution in this instance because I want the JVM to stay alive.
Any help would be much appreciated.
Thanks,
Chris
I'm guessing you're launching the java program from a different process perhaps? A potential option may be integrating the Java Service Wrapper ? Depending on what you are doing, licensing may be an issue, and you probably wouldn't kill the process with a ctrl-c, but it's a thought (in addition to the others given above).
Alright, so I'm writing this program that essentially batch runs other java programs for me (multiple times, varying parameters, parallel executions, etc).
So far the running part works great. Using ProcessBuilder's .start() method (equivalent to the Runtime.exec() I believe), it creates a separate java process and off it goes.
Problem is I would like to be able to pause/stop these processes once they've been started. With simple threads this is generally easy to do, however the external process doesn't seem to have any inbuilt functionality for waiting/sleeping, at least not from an external point of view.
My question(s) is this: Is there a way to pause a java.lang.Process object? If not, does anyone know of any related exec libraries that do contain this ability? Barring all of that, is extending Process a more viable alternative?
My question(s) is this: Is there a way to pause a java.lang.Process object?
As you've probably discovered, there's no support for this in the standard API. Process for instance provides no suspend() / resume() methods.
If not, does anyone know of any related exec libraries that do contain this ability?
On POSIX compliant operating systems such as GNU/Linux or Mac OS you could use another system call (using Runtime.exec, ProcessBuilder or some natively implemented library) to issue a kill command.
Using the kill command you can send signals such as SIGSTOP (to suspend a process) and SIGCONT (to resume it).
(You will need to get hold of the process id of the external program. There are plenty of questions and answers around that answers this.)
You will need to create a system for sending messages between processes. You might do this by:
Sending signals, depending on OS. (As aioobe notes.)
Having one process occasionally check for presence/absence of a file that another process can create/delete. (If the file is being read/written, you will need to use file locking.)
Have your "main" process listen on a port, and when it launches the children it tells them (via a comamnd-line argument) how to "phone home" as they start up. Both programs alternate between doing work and checking for handling messages.
From what you have described (all Java programs in a complex batch environment) I would suggest #3, TCP/IP communication.
While it certainly involves extra work, it also gives you the flexibility to send commands or information of whatever kind you want between different processes.
A Process represents a separate process running on the machine. Java definitely does not allow you to pause them through java.lang.Process. You can forcibly stop them using Process.destroy(). For pausing, you will need the co-operation of the spawned process.
What sorts of processes are these? Did you write them?
Is there a programatic way from java to force a file deletion on windows even if the file is locked by some process? I cannot kill the process that locks the file.
you can go through JNI. But the whole point of locking was NOT to allow for deletion from another process.
So, unless you're running Windows 9x, this might be impossible to do.
That said, if you're aware of a way to do it using WIN32, then you might be able to do it in java using JNI.
I am not an expert on windows, but I do not know of a way in OR OUT of Java to reliably force the deletion of a file that is currently open to a process while the process continues to run.
Use Unlocker. Call it from Java as an external process.