0

Starting a java command line application is easy, you only have to write the following line in a command prompt (in the directory where the app is located).

java myApp.java

However, to stop the application in the right way, so that you ensure that all unmanaged resources are cleaned (and anything that must be done before stop, will be done) requires custom code.

The app will run in a debian system with no GUI as a daemon (it will run in background).

Here below I write the skeleton of the code.

public class MainClass {

    public static void main(String[] args) throws Exception {

        boolean stop = false;

        while(!stop){
            doSomething();
        }

        stop();     
    }

    private static void doSomething(){
        //Main code of app here
    }

    private static void stop(){
        beforeStop();

        System.exit(0);
    }

    private static void beforeStop(){
        clean();

        //Code to do anything you have to do before stop
    }

    private static void clean(){
        //Code to clean unmanaged resources
    }

}

As you can see, the app will run 24/24 and won't stop until you don't stop it. Killing the process (as some people suggest) is not a good solution, because (for example) some unmanaged resources might not be cleaned properly.

I need a code which makes possible to alter the boolean variable "stop" from OUTSIDE.

The best solution is the one which makes possible to stop the app with a command similar to the start command, see pseudo code below (executed in a command prompt, in the directory where myApp.java is located).

myApp.java stop=true

But if it's not possible, the second option would be to have an other java command line app, which stops myApp.java, so that I could stop myApp.java with the following code

java stopMyApp.java

Is someone able to suggest a useful code example?

Errore Fatale
  • 978
  • 1
  • 9
  • 21
  • 3
    Catch the SIGINT signal send by Ctrl+C. –  Apr 25 '18 at 12:32
  • Look up the Java Service Wrapper amongst other tools for building reliable Java services. – bmargulies Apr 25 '18 at 12:37
  • 2
    Don't invent your own mechanism. Catch SIGINT (https://stackoverflow.com/q/2541475/9455968), clean up your resources, exit. –  Apr 25 '18 at 12:54
  • "Catch SIGINT" is such an OS specific description, you implement a JVM shutdown hook as the question Lutz Horn linked to suggests. CTRL+C is what people will want to do to interrupt a shell program, so you better do your best to make it possible to just do that. – Gimby Apr 25 '18 at 12:57
  • I'm realizing that my question is probably the wrong question. My application doesn't require user interaction. You start application and then you can do anything else you want in your OS. CTRL+C is what you do when you are running an application and you want to exit from it to do something else in your OS. You can't use CTRL+C to stop an application which is running behind the scenese, right? The command: CTRL+C myApp.java doesn't exist. Probably my question is wrong, because what I need is not an application, but a "daemon"/service...?? – Errore Fatale Apr 25 '18 at 13:12
  • 1
    I edited the OP, so it's clear that the program is intended to be a daemon. – Errore Fatale Apr 25 '18 at 13:20
  • Possible duplicate of [Run a Java Application as a Service on Linux](https://stackoverflow.com/questions/11203483/run-a-java-application-as-a-service-on-linux) – Gimby Apr 26 '18 at 09:42

1 Answers1

1

You can use a text file with one word. Your program reads it every x seconds and depending on that word it will autostop.

You can change the file text content by hand or with another program you can run whenever you want.

Even better you can use WatchService API (Java 7) or VFS API from Apache Commons to be notified when the file changes.

If you use a DB you can use it instead of a plain file.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54