Stop execution of jar

Is it possible to stop the execution of a .jar file using e.g. a .bat-file? If so, how can this be done?

If not, what is the appropriate way to stop the execution? I could use system.exit(), but how can I call this from an external procedure or pass arguments to a running java application?
De_CisseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

__geof__Commented:
I think that the easiest way would be to use a Socket that will listen to a port. You can send string to that socket and call System.exit from there.
I attach an example where s is ServerSocket with the defined port. Should be over 1024 and bellow 64K
Thread thread = new Thread() {
 Socket socket = null;
 BufferedReader inServer;
 String str;
 
 public void run() {
  while (true) {
   try {
    socket = s.accept();
    inServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    while ((str = inServer.readLine()) != null) {
     if (str.equals("stop")) {
      System.exit(0);
     }
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
};
thread.start();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas4019Commented:
If you are running windows the much easier way is this batch command. This way it requires no modification of your Java code. It should stop all JARs running. You could run it in command prompt or run it in a .BAT file.
taskkill /F /IM java.exe

Open in new window

0
__geof__Commented:
taskkill /F /IM java.exe will kill any JVM. You cannot be sure that you won't kill another application in addition to yours.

0
Thomas4019Commented:
That is true, the solution using taskkill arbitrarily kills all Java VMs. This could create problems if you are also running java based programs such as Netbeans. For this reason this is not an elegant but a easy solution.
0
De_CisseAuthor Commented:
Superb way to control start and stop of an application. You can write a generic ClientApplication to stop whatever jar you want.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.