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?
Who is Participating?
__geof__Connect With a Mentor 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")) {
   } catch (IOException e) {
    // TODO Auto-generated catch block

Open in new window

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

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

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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.