Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:52:34+00:00 2026-06-09T00:52:34+00:00

When I first developed a Java service for Windows using Apache daemon, I used

  • 0

When I first developed a Java service for Windows using Apache daemon, I used the JVM mode which I liked a lot. You specify your class and start\stop (static) methods. But with Linux, Jsvc doesn’t look like it has the same option. I would really like to know why ?!

Anyway If I’m going to use Linux’s init system, I’m trying to find a similar way to accomplish the same behavior which is to start the app in anyway but to stop it, I’ll have to call a method in a class.

My question is, after the jar is started, how can I use the JVM libraries or anything else, to call a method in my application (which will attempt to stop my application gracefully).

Another side question, if an application is started and that application has static methods, If I use the java command line to run a main method in one if that’s application class, and the main method, which is static would call another static method in the class in which I would like to signal the termination signal, would that call by in the same JVM?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:52:37+00:00Added an answer on June 9, 2026 at 12:52 am

    Why not rather add a ShutdownHook to your application?

    A shutdown hook is simply an initialized but unstarted thread. When
    the virtual machine begins its shutdown sequence it will start all
    registered shutdown hooks in some unspecified order and let them run
    concurrently. When all the hooks have finished it will then run all
    uninvoked finalizers if finalization-on-exit has been enabled.
    Finally, the virtual machine will halt. Note that daemon threads will
    continue to run during the shutdown sequence, as will non-daemon
    threads if shutdown was initiated by invoking the exit method.

    This will allow your jar to terminate gracefully before being shutdown:

    public class ShutdownHookDemo {
        public void start() {
            System.out.println("Demo");
            ShutdownHook shutdownHook = new ShutdownHook();
            Runtime.getRuntime().addShutdownHook(shutdownHook);
        }
    
        public static void main(String[] args) {
            ShutdownHookDemo demo = new ShutdownHookDemo();
            demo.start();
            try {
                System.in.read();
            }
            catch(Exception e) {
            }
        }
    }
    
    class ShutdownHook extends Thread {
        public void run() {
            System.out.println("Shutting down");
            //terminate all other stuff for the application before it exits
        }
    
    }
    

    It is important to note

    The shutdown hook runs when:

    • A program exists normally. For example, System.exit() is called, or the last non-daemon thread exits.
    • the Virtual Machine is terminated. e.g. CTRL-C. This corresponds to kill -SIGTERM pid or
    • kill -15 pid on Unix systems.

    The shutdown hook will not run when:

    • The Virtual Machine aborts
    • A SIGKILL signal is sent to the Virtual Machine process on Unix systems. e.g. kill -SIGKILL pid or kill -9 pid
    • A TerminateProcess call is sent to the process on Windows systems.

    Alternatively if you must you can use this to call a method in a class:

    public class ReflectionDemo {
    
      public void print(String str, int value) {
        System.out.println(str);
        System.out.println(value);
      }
    
      public static int getNumber() { return 42; }
    
      public static void main(String[] args) throws Exception {
        Class<?> clazz = ReflectionDemo.class;//class name goes here
        // static call
        Method getNumber = clazz.getMethod("getNumber");
        int i = (Integer) getNumber.invoke(null /* static */);
        // instance call
        Constructor<?> ctor = clazz.getConstructor();
        Object instance = ctor.newInstance();
        Method print = clazz.getMethod("print", String.class, Integer.TYPE);
        print.invoke(instance, "Hello, World!", i);
      }
    }
    

    and to load a class dynamically:

    ClassLoader loader = URLClassLoader.newInstance(
        new URL[] { yourURL },
        getClass().getClassLoader()
    );
    Class<?> clazz = Class.forName("mypackage.MyClass", true, loader);
    Class<? extends Runnable> runClass = clazz.asSubclass(Runnable.class);
    

    References:

    • http://onjava.com/onjava/2003/03/26/shutdownhook.html
    • http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)
    • How to shutdown java application correctly from C# one
    • How does one access a method from an external jar at runtime?
    • How to load a jar file at runtime
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here I am, first time windows developer (done java swing, iphone, flash/flex) and
Hello I have developed my first application for Android using the Ice Cream Sandwich
I developed a website using EF 4.1 code first,Mvc3 ,Sql Sever 2008 r2, and
i need to develop a .net library which exposes a java web service proxy
I am trying to develop my first restful service in Java and having some
I develop an app using PhoneGap. I got a service which is in background
Although we are a Java camp using SVN for source control and Apache Ivy
First, I developed a Java EE application with a Adobe Flex frontend and I
In the past, I've only developed Java applications. This is my first time attempting
I have developed a J2ME app using java.awt.Window as I want a full screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.