How to find and kill running Processes from within Java? - java

I need a Java way to find a running OS process from which I know to name and path of the executable. I want to look whether it is running right now and I need a way to kill the process if I found it.
My Question is a similar to how-to-find-and-kill-running-win-processes-from-within-java but not restricted to windows.
Am I forced to do an implementation per platform or is there some trick/library/api I'm missing that can do this?

The SIGAR library has some pretty cool support for stuff like this. It has java APIs for listing and killing system processes.

Related

Stopping a running application on Mac OS through Java

I work on a Macbook and I would like to close some running applications such as Remote Desktops through using Java.
I'm quite new to programming in Java and other than Google and StackOverflow I'm not sure where to go. I already looked for a solution on Google but all I can find are instruction on how to close Java on Mac OS, not actually how you close a running application through Java code.
So I am looking for some pointers on what Java commands I should use to close a running application in Mac OS. Thank you very much :)
While programming in Java, you only have access to do things inside the JVM. But your code inside the JVM wouldn't usually have permissions to affect other processes running on the operating system.
You can definitely call an external command with something like this:
Process process = Runtime.getRuntime().exec("kill 12345");
That would run the kill command on process id 12345. This would work, assuming you have the right permissions.
You can get more information on the exec command in the docs: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String)

Monitor program/process execution in windows using java

I am trying to develop a small application that can monitor the programs/processes that are executing in a windows machine.
If the program/process is not supposed to run, it should be blocked. It works similar to an antivirus.
This is the basic idea.
I want to know the ways to hook into the OS to get notified about every single program/process trying to run in the machine.
And i want it in java... any help please?
Java wouldn't be your best answer sorry mate. It's not limited to the JVM, can get access outside of that, but for what you're looking for, it wouldn't be an efficient language to use unfortunately. By all means see what other answers you get though.

Kill / Stop an application with Java Code

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.

Utility to view threads in a Java process

I've got a java application on a windows machine which pins the CPU from time to time. Would like to run a utility to get an idea of how many threads etc that application is creating. Is there such a utility?
jconsole is included with the jdk and includes thread/memory/cpu monitoring
You could try jstack, should be part of the jdk.
I've always been partial to YourKit.
There are lots of others though, both open source and commercial. If you do a google search for "java profilers" you should be able to find one to your liking.
get a look at jvisualvm (in the jdk)

Forcing file deletion on Windows from Java

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.

Categories