Results 1 to 2 of 2
  1. #1
    maca is offline Member
    Join Date
    Jul 2014
    Location
    UK
    Posts
    20
    Rep Power
    0

    Default Is it possible to change exit status without calling System.exit?

    So basically, I have an application, the main method is pretty simple, something like

    Java Code:
    class MyClass {
        protected static String contextFileName = "applicationContext.xml";
    
        public static void main(String[] args) {
            int returnCode;
            try (AbstractApplicationContext context = new ClassPathXmlApplicationContext(contextFileName)) {
                MyBean myBean= context.getBean("myBean", MyBean .class);
                returnCode = myBean.run();
            } catch (Exception e) {
               returnCode = -1;
            }
    
            System.exit(returnCode);
        }
    }
    The problem is, because exit is so harsh, if I try to test the above in a JUnit test, then the test will exit. So, I'm wondering, is there any way for me to set the exit status without calling System.exit() ?

    If not, I guess an alternative would be to split the code into another static method that I can test, something like:

    Java Code:
    class MyClass {
        protected static String contextFileName = "applicationContext.xml";
    
        public static void main(String[] args) {
            System.exit(run());
        }
    
        protected static int run() {
            int returnCode;
            try (AbstractApplicationContext context = new ClassPathXmlApplicationContext(contextFileName)) {
                MyBean myBean= context.getBean("myBean", MyBean .class);
                returnCode = myBean.run();
            } catch (Exception e) {
               returnCode = -1;
            }
            return returnCode;
        }
    }
    Thanks :)

  2. #2
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: Is it possible to change exit status without calling System.exit?

    Simple put, no. If you call System.exit(int) without security manager it exits and returns success/fail. Of course you can store a value you want to call System.exit(intError) with... Here's some things to search

    Google: System.exit security manager prevent jvm exit



    Have you learned what the Java Docs are yet?
    System.exit ===>
    System (Java Platform SE 7 )
    Security Manager ===>

    <Taken from docs>
    public static void exit(int status)

    Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

    This method calls the exit method in class Runtime. This method never returns normally.

    The call System.exit(n) is effectively equivalent to the call:

    Runtime.getRuntime().exit(n)


    Parameters:
    status - exit status.
    Throws:
    SecurityException - if a security manager exists and its checkExit method doesn't allow exit with the specified status.
    See Also:
    Runtime.exit(int)

Similar Threads

  1. System.exit(0);
    By Nirantak in forum New To Java
    Replies: 3
    Last Post: 12-12-2012, 04:17 PM
  2. Replies: 7
    Last Post: 06-27-2012, 05:30 PM
  3. Exit System
    By jboy in forum New To Java
    Replies: 5
    Last Post: 09-14-2009, 08:27 AM
  4. System.exit 0 or 1?
    By antgaudi in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:33 PM
  5. system.exit(..)
    By ramakanta.majhi in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 01:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •