Trapping System.exit() calls

Hey folks. I'm wondering if anyone has a best practice for trapping System.exit() calls in managed applications that are running in a a single MBeanServer. In our design, there is a single VM that runs the MBeanServer and any loaded applications. If any loaded application (MBean) uses a System.exit() call, this will bring down the entire agent. We've put some preventative code into a custom SecurityManger to at least catch the exit call and automatically restart the agent, but I'm figuring there is a better way to do this.
We're making it a best practice to NOT use System.exit() in any JMX-managed applications that will run inside our agent and rather use Thread termination code. Has anyone run into these same issues? If you look at the JMX Server example in the Sun JMX tutorial, if I throw a System.exit() call into one of the sample MBeans, the server will die.
As I was perusing a local bookstore, I thumbed through a JMX book that had some example code in it, and in their "Server" example code, they have a shutdown and a startup method. In the shutdown method there is a System.exit() call, which, in my understanding, will bring down the agent. How, then, could you call startup() on an agent that isn't running?
Are these designs assuming that the MBean server is running under J2EE application server like WebLogic or WebSphere?
Any thoughts would be much appreciated. Thanks.
-Phil

the int you pass to System.exit is returned by the JVM...... (I think, I forget........)
Generally use 0 to signify "normal" exit
Generally use 1 to signify an "abnormal" exit (so you terminated the program (using exit) becuase you couldn't handle the exception nicely)
So you can use the return value to "monitor" your program...... it's an old concept, C programmers use it all the time....

Similar Messages

  • Catch/Prevent System.exit call in other thread

    Wasn't sure where to put this so apologies if this is in the wrong section.
    I have an application that runs a particular method contained in a JAR that is written/provided by somebody else (they implement an interface I provide). They have complete free range as to what their application does. I then run this application in a separate thread, the idea being that when it completes my original application is still running.
    What I have provided so far is ok PROVIDED that the given JAR does not perform a System.exit call which is very possible. I want to prevent them from doing this but I don't want to contacting the author asking them to remove all invokations of System.exit.
    I found this article: https://www.securecoding.cert.org/confluence/display/java/EXC04-J.+Prevent+against+inadvertent+calls+to+System.exit%28%29+or+forced+shutdown and so I tried to create my own SecurityManager. Unfortunately though despite explicitly putting the running of the applicaiton in a catch block for AccessControlException it doesn't catch it and fails. Does anybody have an idea of how to overcome this.
    In summary: how can I prevent a call to System.exit being invoked (or when it is invoked, catch it and prevent it from actually happening) inside another thread?

    batterj2 wrote:
    I was unable to do any File I/O afterwards which seems strange as its extending the original SecurityManager so I thought it wouldn't have any difference.No, that code isn't "extending the original SecurityManager". It's creating a SecurityManager which overrides some SecurityManager which may or may not be the same as the actual SecurityManager which was in place when you applied it. From what you say, it's not the same.
    Remember you don't extend objects, you extend classes.
    You could write something which gets a link to the current security manager, then delegates all calls to that security manager except the ones you don't want to delegate.

  • To trap System.exit(0)

    Hi All,
    In one of our code we are saying System.exit(0),System.exit(1),System.exit(-1) based on certain condition that occur in the program. Could any one let us know how to trap the integer returned to the OS when System.exit(x) is called.
    Regards,
    Vinodramu

    Not sure I know what you are asking.
    You could create a security manager which prevents exit() from being called.
    You could replace System using the bootstrap command line option. Although if you do that you can not distribute it due to the license agreement. Once replaced you can do anything you want in the application.
    You could use Runtime.addShutdownHook() if you just want to do something when the application exits.

  • How stop call to System.exit but allow them to install Securitymanager?

    I have an application that allows you to start a java application from inside it (it then gives you monitoring capabilities). I want this to work with pretty much any desktop application, even those that require their own security manager. However, i don't want their application to be able to shutdown the jvm. How can I do this?
    As far as I can tell, there's no way to allow them to add the securitymanager, "wrapped" by mine is there?
    And I thought of using AccessController.doPriviledgedAction(...) and giving them a context where they can add a security manager but not call system.exit, but then if they do add a security manager, won't it have the final say on system.exit calls?
    Is there a solution here?

    6tr6tr wrote:
    Thanks for the reply!
    Hmmm, the problem with that is I need the users to be able to use my application without access to that app's source code. So they need to be able to run that program (as 3rd party black-boxed code) inside my app and still have this work properly. Is there another way?
    Source code?
    You don't need any source code for what I suggested.
    If a frame/jframe tries to exit on close, I can grab and stop that. my problem is if some random code tries to call system.exit(). Is there any other way to intercept it?
    UPDATE: the only solution i could come up with is to use bytecode engineering + classloading.
    Which I doubt will help if they call it with reflection.
    You could use the bootstrap command line options and replace System with your own version. Add some functionality to how it sets up the security manager and/or make System.exit() do nothing, but provide another name for yourself.

  • Capture System.exit

    Hello
    I am writing a program where I need to use utilities from a separate jar file. The only public entry point is that jar file's main method and it ends with System.exit
    Is there a way to capture the System.exit call so my program doesn't terminate?
    If so, can I capture the return value?
    Using Runtime.exec() is not preferred as it limits the number of command line parameters supplied to the called program.
    I'd appreciate any suggestions on how do do this or sugggestions on where to look for further information.
    aqd

    how could you expect me to use JAVA command without compiling it with Javac??
    I complied the java code using javac and then called(executed ) it using the Java..
    I am using JNI_Create/JavaVM() to create a JVM from CPP file. that works fine. Now, my issue is that I want to capture a couple of exit codes that are returned from the System.exit() of the java code.
    now, guys.. is there any way to capture that exit codes returning by Ssytem.exit() in or using JNI ??
    if yes, please help me with the code snippets.
    Thanks,
    Soujanya.R

  • The ABAP call stack was: SYSTEM-EXIT of program BBPGLOBAL

    Hi
    We are using SRM 5.0. We are facing a strange problem. We are able to see the initial screen of SRM EBP in the browser. But once the user name and password are provided the system goes for a dump with the following error:
    The following error text was processed in the system SS0 : System error
    The error occurred on the application server <Server Name> and in the work process 0 .
    The termination type was: ABORT_MESSAGE_STATE
    The ABAP call stack was: SYSTEM-EXIT of program BBPGLOBAL
    <b>SM21 Log:</b>
    Short Text
         Transaction Canceled &9&9&5 ( &a &b &c &d &e &f &g &h &i )
         The transaction has been terminated.  This may be caused by a
         termination message from the application (MESSAGE Axxx) or by an error
         detected by the SAP System due to which it makes no sense to proceed
         with the transaction.  The actual reason for the termination is
         indicated by the T100 message and the parameters.
    Transaction Canceled ITS_P 001 ( )
    Message Id: ITS_P
    Message No: 001
    I just checked these threads but did not help much,
    RABAX_STATE  error after loggin into BBPSTART service in SRM 4.
    ITS_TEMPLATE_NOT_FOUND error
    Besides I tried this note: 790727 as well, still I get the same error.
    Any help would be highly appreciated.
    Thanks in advance
    Kathirvel Balakrishnan

    Hi
    <u>Please do the following steps.</u>
    <b>When you are using the Internal ITS,you need not run the report W3_PUBLISH_SERVICES.(only SIAC_PUBLISH_ALL_INT )
    ALso pls check the foll:
    -->activate the services through SICF tcode.
    > Go to SICF transaction and activate the whole tree under the node Default host>sap>bc>gui>sap>its.
    >Also maintain the settings in SE80>utilities>settings>internet transaction server-->test service/Publish. (BBPSTART , BBPGLOBAL etc)
    Table TWPURLSVR should have entries for the / SRM server line as well as gui and web server.
    Could you please review again the following steps ?
    Did you check that ICM was working correctly (Transaction -  SMICM) ?
    1-Activate the necessary ICF services
    With transaction SICF and locate the
    services by path
    /sap/public/bc/its/mimes
    /sap/bc/gui/sap/its/webgui
    2- Publish the IAC Services
    With Transaction SE80 locate from
    the menu Utilities -> Settings ->
    Internet Transaction Server (Tab) ->
    Publish (Tab) and set “On Selected
    Site” = INTERNAL.
    3- Locate the Internet Services SYSTEM and WEBGUI.
    Publish these services with the Context
    Menu -> Publish -> Complete Service
    4- Browse to http://<server>:<icmport>/sap/bc/gui/
    sap/its/webgui/! and login to the
    webgui.</b>
    Hope this will help.
    Please reward suitable points.
    Regards
    - Atul

  • How  to track System.exit(0) call

    hi there,
    how can i trace the System.exit(0) function call in my program.
    i.e as we know which class is being loaded into the jvm by overriding
    the classloader, can we similarly know when is our jvm going to be
    destroyed. here iam invoking jvm from my windows program using invocation api.
    any help is mostly appreciated.
    thanks in advance
    bye
    ramana

    Not sure I know what you are asking.
    You could create a security manager which prevents exit() from being called.
    You could replace System using the bootstrap command line option. Although if you do that you can not distribute it due to the license agreement. Once replaced you can do anything you want in the application.
    You could use Runtime.addShutdownHook() if you just want to do something when the application exits.

  • Is System.exit() a good call to use?

    I have a program that checks for updates in the db. If there are no updates in the database I want the program to stop. There are also many other conditions that would cause the program to stop. None of them are "Errors" so I wasn't using Exceptions to end the program The program runs from a command line or from chron job. Is it safe / OK to use System.exit() to do this?

    As you describe it, I'd say yes. System.exit() is the way to end your program.
    The danger of System.exit() is if a class you write will be reused in some other system. In that case, System.exit() could be entirely inappropriate in that context, thus preventing your class from being reusable.
    If you only call System.exit() in your main() method, you'll probably be OK.

  • Error in XMLDiff Bean, System.exit() is being called in setFiles() ?

    When I call the setFiles() method in XMLDiff and the files contain dtd references, my application will shut down competely as though System.exit() is being called from within the XMLDiff bean. I have tested this with the XMLDiff demo and it also shut down when a dtd reference was in the files.

    The version of XDK is 9.2.0.2.0
    The test case is the "family.xml" in the demo\java\parser\common directory using the XMLDiffSample application. What I did was to try to compare the family.xml against itself. When I do that it shuts down the demo application as though System.exit() has been called.
    Setting the standalone="yes" did in fact cause it to work properly, but I am not sure why?

  • NoClassDefFoundError after System.exit() is called.

    I have found a strange condition with JDK 1.4.2_05 and could not find a bug which relates to it.
    I have multiple threads running, when I shutdown, I call all the threads to shutdown and finally in main I call System.exit(0).
    The strange thing is that one shutdown I get a NoClassDefFoundError for a class which will load fine if loaded before shutting down.
    This occurs as the class in question is only needed to perform a shutdown, and in fact the attempt to load it comes after the main thread has finished.
    The workaround has been to create an instance of the class when the application is started rather than during shutdown.
    Does anyone seen this behaviour?

    java.lang.NoClassDefFoundError: com/cantor/framework/run/impl/ShutdownRMB
         at com.can.framework.run.RunnerManagerBean.shutdown(RunnerManagerBean.java:147)
         at com.can.framework.layer.VanillaBean.shutdown(VanillaBean.java:278)
         at com.can.main.Main.main(Main.java:135)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:324)
         at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)

  • Intercepting System.exit() and System.out.println() calls

    hi there,
    I have often problems when working with code that uses System.exit() and System.out.println() extensively, because it becomes difficult to debug.
    Basically I do have wrappers for System.exit() (my own static exit function) and for System.out.println() (log4j).
    Still not all programmers are using these methods; Probably the only way to enforce this is some kind of code warrior, but I was hoping to be able to intercept the two System.XXX calls (and throw an appropriate Exception). is this possible ??

    Why not simply make your own security manager andhandle checkExit() and checkWrite?
    Does anyone have a simple example of this? Please?System.exit() can be intercepted using a security manager, but not System.out.xxx.
    Here is a short example:
    //set your security manager
    static{
         SecurityManager s = new OwnSecurityManager();
         System.setSecurityManager(s);
    //redirect the out stream
    try{
         PrintStream ps = new PrintStream(new FileOutputStream("output.txt"));
         System.setOut(ps);
    }catch(IOException ioe){
         ioe.printStackTrace();
    //some tests
    System.out.println("Test");
    System.exit(2);
    //your security manager
    class OwnSecurityManager extends SecurityManager{
         public void checkExit(int code){
           if(code > 0){
             super.checkExit(code);
             throw new SecurityException("Not allowed here->" + code);
         public void checkRead(FileDescriptor fd){
         public void checkRead(String file){
         public void checkRead(String file, Object context){
         public void checkPermission(Permission perm){
           if(perm.getName().equals("exitVM")){
             System.out.println("exitVM");
    }

  • Denying Thread calling System.exit() successfully

    Hi all,
    i start another java app from within my code by calling it's main()-method in an own thread.
    As i don't have any access to the code of the java app i can't prevent it from calling System.exit() when the corresponding window is closed.
    How can i prevent the "threaded" java app from also closing my wrapping application when it is finished?
    Greetings,
    Bernhard

    You can install a security manager (your own) in your application and override the method checkExit(int status) in order to handle the event:
    class OwnSecurityManager extends SecurityManager{
         public void checkExit(int code){
              if(code == 1){
                   super.checkExit(code);
                   throw new SecurityException("Not allowed here"); //or your handling code
              }else{
              //other codes
    //in your app you install the manager like this
    SecurityManager s = new OwnManager();
    System.setSecurityManager(s);

  • Calling System.exit(0) from an applet

    Hello,
    I have a weird problem.
    First... my applet has full permissions in the java.policy file.
    problem:
    When I call System.exit(0) from the applet (jbutton's action listener), the applet disappears from the browser window and the browser freezes.
    but
    if I display a messagedialog (JoptionPane) before calling System.exit(0) ... everything works fine (after I press ok on the message dialog, the browser window disappears)
    here's my code
    <CODE>
    public class testApp extends JApplet{
    public void init(){
    System.err.println("hello");
    JButton button = new JButton("Exit");
    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JOptionPane.showMessageDialog(new JFrame(),"Exiting...");
    System.exit(0);
    this.getContentPane().add(button);
    </CODE>
    what am I missing?

    an applet has security restrictions that forbids it from accessing a client file system. BUT there are ways like signing your applet or just asking the user to change the security setting when he calls the applet.
    This link shows the steps involved in signing an applet very clearly. :-http://developer.java.sun.com/developer/technicalArticles/Security/Signed/
    Or the user(client) can go to the "Internet Option" under the Tools of the web browser(IE). Go to security and change to custom security for the Internet.(Caution the user needs to return the security to default after finishing accessing the applet). Click to "Custom Java Setting" button. If you don't see a "Custom Java Setting" button below then scroll down to "Java Permissions" option and select custom. Now you will see the "Custom Java Setting" button. Go there and select "edit permission" tab. Under "Unsigned Content" select enable. This will grant full permission.

  • Calling System.exit() from multiple threads

    Hi,
    Why would calling System.exit() from multiple threads cause all the threads to block?
    Laith

    Thanks for your replies.
    I made further tests and the problem is actually not as I described it first.
    I'm using a shutdown hook that would stop all running threads if the program is stopped externally (using ctl-c for example) (I'm not using Thread.stop(), but setting a boolean value to false that would make the thread exit a while() loop in run()) .
    One of the running threads would call System.exit(-1) just before it would exit run().
    This thread is getting blocked if it is stopped by the shutdown hook.
    I know that this is a bad coding practice, bout would still appreciate an explanation to the reason why the thread is actually blocked by System.exit().
    Laith

  • Disabling System.exit() and shutdown calls

    I am trying to splice/hack together something that might stop a System.exit(0) call or perhaps some type of exit call for a Java application. The only thing I have come up with now is making an extension of SecurityException and putting the shutdown code within a try. Here is what I have so far...
    class ExitTrappedException extends SecurityException {
         protected static void forbidSystemExitCall() {
              final SecurityManager securityManager = new SecurityManager() {
                   public void checkPermission( java.security.Permission permission ) {
                        if( "exitVM".equals( permission.getName() ) ) {
                             throw new ExitTrappedException() ;
              System.setSecurityManager( securityManager ) ;
         protected static void enableSystemExitCall() {
              System.setSecurityManager( null ) ;
    }And then utilizing it in the following manner...
    forbidExitCall() ;
        try {
          // Call the "exiting" code here...
        } catch( ExitTrappedException e ) {
        } finally {
          enableExitCall() ;
        }I am not very comfortable setting the SecurityManager to null when re-enabling it to be honest. I am curious if someone might have a different solution to stopping a System.exit(0) call and the windows altf4+ shortcut. Any help would be much appreciated.

         static SecurityManager securityDefault = System.getSecurityManager();
         protected static void forbidSystemExitCall() {
              final SecurityManager securityManager = new SecurityManager() {
                   public void checkPermission( java.security.Permission permission ) {
                        if( "exitVM".equals( permission.getName() ) ) {
                             throw new ExitTrappedException() ;
              System.setSecurityManager( securityManager ) ;
         protected static void enableSystemExitCall() {
              System.setSecurityManager( securityDefault ) ;
    }how bout that?
    Edited by: brillohead on Aug 4, 2008 11:21 AM

Maybe you are looking for

  • Cannot deploy Web dynpro application from NWDS

    Hi, I am new to EP. I created a web dynpro application in nwds and tried to deploy it. But it gives the following error . Settings SDM host : nw04 SDM port : 50118 URL to deploy : file:/E:/WINDOWS/TEMP/temp12188WelcomeWebDynproprj.ear Result => deplo

  • How to print a folded booklet in pdf format

    I am using hp Officejet Pro 8600 printer and am trying to print a booklet in pdf format.   Also I am printing on both sides of the sheet and the second page is upside down.

  • Silent Install of Acrobat 9 Pro

    I've seen a few questions about installing Acrobat 9 Pro silently on Mac OS X. Maybe someone's already dealt with this, but I thought it might be helpful. Installing Acrobat 9 Pro silently (and across multiple machines) is possible. I have not, howev

  • Purchase order number in SQ01

    Hi,                        I have created query for GRN List from BSEG table for Material document, vendor number, material number and po number. I am getting texts of these vendoe, material and po number. So can anybody explain how to get these numb

  • Document icons wont open documents

    I am now on Pages version 2.0. The earlier version worked just fine. But with this update, the icon that is produced when I save a document will not open it again later when I double click it. I can only open it by dragging the document icon to the P