1

The title basically says it all but just but to reiterate:

I need my Java program to kill itself (dispose w/e) if a specific external program launches.

My java program uses a global hook that listens for mouse input, when I remote into a clients computer the mouse listener/GUI that my program creates can cause all kinds of issues with the mouse if used I'm while connected. To handle this I need my program to automatically "turn off" when the screen-connect application we use launches.

I am already using a global hook to capture mouse input, is there something similar I could use for system events maybe?

  • Show the code that you have already tried before asking for help. Also, @ThomasEdwin OP is not asking how to close the program, he's asking how to detect when another application is opened. – arkdevelopment Dec 29 '17 at 16:32
  • From what i know, you would need to setup a whole TCP connection and make your program wait until some other program connects to it and when it does, then close it – Joza100 Dec 29 '17 at 16:38
  • I noticed that your question is still "open" - as you didn't accept an answer. Please have a look and decide if you want to [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) an answer. Or let me know if there is something I can do to enhance my input to make it accept worthy. Accepting helps future readers to determine if the problem is solved, and shows appreciation for the people who took the time answering you. Thanks! – GhostCat Jul 19 '18 at 14:34

2 Answers2

0

Depending on the version of windows respectively Java you are using there are various libraries you could be using to simply regularly "scan" the operating system for a list of running processes.

If something shows up that you don't like, simply react using System.exit for example.

GhostCat
  • 127,190
  • 21
  • 146
  • 218
0

In your first program, just create a new Thread and let it run. Put this code in the run method:

try {
    ServerSocket socket = new ServerSocket(1111); // the paramater is a port and you can choose any port you want
    socket.accept();
    socket.close();
    System.exit(0);
} catch (IOException e) {
    e.printStackTrace();
}

This will wait for a connection and once the connection is made, it will close itself. That is because the accept method that waits for a connection blocks until there is a connection made. That's why you put it in another thread. After it recieves the connection it will unblock so the code will continue and exit the program! And it MUST be in another friend so it doesn't block your application!

In the second application, in the beginning just make a connection! To make a connection use this code:

try (Socket socket = new Socket("localhost" ,1111);){
        socket.close();
} catch (IOException e) {
}

This will connect it and once it does, the first program will close. Now just continue on with this program! If you don't understand the sockets the best, then check out this link. It is awesomely explained:

https://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html

Joza100
  • 309
  • 2
  • 14