0

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 :)

Knarf
  • 137
  • 1
  • 12
  • Invoke `killall ` via Runtime/ProcessBuilder? There's a [solution in windows](https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/12140685/how-to-close-a-external-application-with-java&ved=2ahUKEwi3r_eA_oniAhUBwVkKHW-QBvsQFjAKegQIAxAB&usg=AOvVaw1e8P7JfUy-RyXpCQGwF52T) which you can port – Benjamin Urquhart May 07 '19 at 17:51

1 Answers1

3

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)

mjuarez
  • 16,372
  • 11
  • 56
  • 73