Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

System.ext()

6 views
Skip to first unread message

steph

unread,
Sep 11, 2004, 9:58:41 AM9/11/04
to
Hello,

Is the any way to prevent a class to do a System.exit()

i.e: in my application, i invoque a gui with an 'exit' button and i don't want
to stop the jvm when clicking on this button

thanks.
--
stephane
retirez les lettres majuscules et le 666 de l'adresse pour l'utiliser.
remove uppercase letters and 666 from email address to use.

Mark Thornton

unread,
Sep 11, 2004, 10:18:04 AM9/11/04
to
steph wrote:

> Hello,
>
> Is the any way to prevent a class to do a System.exit()
>
> i.e: in my application, i invoque a gui with an 'exit' button and i
> don't want to stop the jvm when clicking on this button
>
> thanks.

Install a SecurityManager (not always possible) and rejects attempts to
call System.exit. The problem here is that you can't install a
SecurityManager if one has already been installed.

Mark Thornton

Andrew Thompson

unread,
Sep 11, 2004, 11:45:05 AM9/11/04
to
On Sat, 11 Sep 2004 15:58:41 +0200, steph wrote:

> Is the any way to prevent a class to do a System.exit()

Yes. Do not call System.exit() in your code.

> i.e: in my application,

With or without a GUI?

> ..i invoque a gui with an 'exit' button

Why would you invoke a GUI with a
button labelled 'exit'?

>..and i don't want

> to stop the jvm when clicking on this button

So don't.

(To get more meaningful answers, you
are going to need to make more sense)

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Mark Thornton

unread,
Sep 11, 2004, 11:54:12 AM9/11/04
to
Andrew Thompson wrote:

> On Sat, 11 Sep 2004 15:58:41 +0200, steph wrote:
>
>
>>Is the any way to prevent a class to do a System.exit()
>
>
> Yes. Do not call System.exit() in your code.
>
>
>>i.e: in my application,
>
>
> With or without a GUI?
>
>
>>..i invoque a gui with an 'exit' button
>
>
> Why would you invoke a GUI with a
> button labelled 'exit'?
>
>
>>..and i don't want
>>to stop the jvm when clicking on this button
>
>
> So don't.
>
> (To get more meaningful answers, you
> are going to need to make more sense)
>

I presume that he is calling some gui code that he didn't write or
doesn't have permission to modify. That gui code has an exit button
which invokes System.exit. He wants to be able to stop that gui code
from terminating the VM.
Its a nasty problem which arose originally from the need for any GUI
code to use System.exit if the application was ever to exit. This is no
longer necessary as the gui threads will now terminate once there are no
active frames. However there is still a lot of code which still uses
System.exit.
It is easy to make sense of this request if you recognise the symptoms
of the dilemma.

Mark Thornton

Andrew Thompson

unread,
Sep 11, 2004, 12:22:17 PM9/11/04
to
On Sat, 11 Sep 2004 16:54:12 +0100, Mark Thornton wrote:

> It is easy to make sense of this request if you recognise the symptoms
> of the dilemma.

Or if it's put the way you put it.

Andrew Thompson

unread,
Sep 11, 2004, 12:24:22 PM9/11/04
to
On Sat, 11 Sep 2004 15:18:04 +0100, Mark Thornton wrote:

> steph wrote:
..


>> Is the any way to prevent a class to do a System.exit()

..


> Install a SecurityManager (not always possible) and rejects attempts to
> call System.exit. The problem here is that you can't install a
> SecurityManager if one has already been installed.

Can't you install a more retricted security
manager? I thought it was simply that you
could not install a new security manager with
more/wider permissions than the parent.

Sudsy

unread,
Sep 11, 2004, 12:42:30 PM9/11/04
to
Andrew Thompson wrote:
> On Sat, 11 Sep 2004 15:18:04 +0100, Mark Thornton wrote:
>
>
>>steph wrote:
>
> ..
>
>>>Is the any way to prevent a class to do a System.exit()
>>
> ..
>
>>Install a SecurityManager (not always possible) and rejects attempts to
>>call System.exit. The problem here is that you can't install a
>>SecurityManager if one has already been installed.
>
>
> Can't you install a more retricted security
> manager? I thought it was simply that you
> could not install a new security manager with
> more/wider permissions than the parent.
>

From the javadocs for System#setSecurityManager:

"If there is a security manager already installed, this method first
calls the security manager's checkPermission method with a
RuntimePermission("setSecurityManager") permission to ensure it's ok
to replace the existing security manager. This may result in throwing
a SecurityException."

So a security manager can only be replaced if it allows itself to
be replaced.

Andrew Thompson

unread,
Sep 11, 2004, 9:44:49 PM9/11/04
to
On Sat, 11 Sep 2004 15:58:41 +0200, steph wrote:

> Is the any way to prevent a class to do a System.exit()

<sscce>
import java.awt.*;
import java.awt.event.*;

public class NoExit extends Frame implements ActionListener {

Button frameLaunch = new Button("Frame"),
exitLaunch = new Button("Exit");

public NoExit() {
super("Launcher Application");

SecurityManager sm = new ExitManager();
System.setSecurityManager(sm);

setLayout(new GridLayout(0,1));

frameLaunch.addActionListener(this);
exitLaunch.addActionListener(this);

add( frameLaunch );
add( exitLaunch );

pack();
setSize( getPreferredSize() );
}

public void actionPerformed(ActionEvent ae) {
if ( ae.getSource()==frameLaunch ) {
TargetFrame tf = new TargetFrame();
} else {
System.exit(-3);
}
}

public static void main(String[] args) {
NoExit ne = new NoExit();
ne.setVisible(true);
}
}

class TargetFrame extends Frame {
static int x=0, y=0;

TargetFrame() {
super("Close Me!");
add(new Label("Hi!"));

addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.out.println("Bye!");
System.exit(0);
}
});

pack();
setSize( getPreferredSize() );
setLocation(++x*10,++y*10);
setVisible(true);
}
}

class ExitManager extends SecurityManager {

public void checkExit(int status) {
if (status > -2) {
throw( new SecurityException() );
}
}
}
</sscce>

As I suspected, restricting permissions
in an unsigned app running locally is
child's play, but rewidenning them is
a bit more tricky.

I tried storing a reference to the original
securitymanager, so I could swap back to it
to allow the main app to exit, but that threw
SecurityExceptions, hence the kludgy
exit stategy.

The security manager throws an exception for
any exit code > -2, so we call System.exit(-3)
in our main app to end the VM.

HTH

Andrew Thompson

unread,
Sep 15, 2004, 11:40:48 PM9/15/04
to
On Sun, 12 Sep 2004 01:44:49 GMT, Andrew Thompson wrote:

> On Sat, 11 Sep 2004 15:58:41 +0200, steph wrote:
>
>> Is the any way to prevent a class to do a System.exit()
>
> <sscce>

<snip>

That one was very hackish, and I mistakenly
thought the security manager could not be
changed back to the original. This version
demonstrates that it can.

<sscce>
import java.awt.*;
import java.awt.event.*;

import java.security.Permission;

/** NoExit demonstrates how to prevent 'child'
applications from ending the VM with a call
to System.exit(0).
@author Andrew Thompson */


public class NoExit extends Frame implements ActionListener {

Button frameLaunch = new Button("Frame"),
exitLaunch = new Button("Exit");

/** Stores a reference to the original security manager. */
ExitManager sm;

public NoExit() {
super("Launcher Application");

sm = new ExitManager( System.getSecurityManager() );
System.setSecurityManager(sm);

setLayout(new GridLayout(0,1));

frameLaunch.addActionListener(this);
exitLaunch.addActionListener(this);

add( frameLaunch );
add( exitLaunch );

pack();
setSize( getPreferredSize() );
}

public void actionPerformed(ActionEvent ae) {
if ( ae.getSource()==frameLaunch ) {
TargetFrame tf = new TargetFrame();
} else {

// change back to the standard SM that allows exit.
System.setSecurityManager(
sm.getOriginalSecurityManager() );
// exit the VM when *we* want
System.exit(0);
}
}

public static void main(String[] args) {
NoExit ne = new NoExit();
ne.setVisible(true);
}
}

/** This example frame attempts to System.exit(0)
on closing, we must prevent it from doing so. */


class TargetFrame extends Frame {
static int x=0, y=0;

TargetFrame() {
super("Close Me!");
add(new Label("Hi!"));

addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.out.println("Bye!");
System.exit(0);
}
});

pack();
setSize( getPreferredSize() );
setLocation(++x*10,++y*10);
setVisible(true);
}
}

/** Our custom ExitManager does not allow the VM
to exit, but does allow itself to be replaced by
the original security manager.
@author Andrew Thompson */
class ExitManager extends SecurityManager {

SecurityManager original;

ExitManager(SecurityManager original) {
this.original = original;
}

/** Deny permission to exit the VM. */
public void checkExit(int status) {
throw( new SecurityException() );
}

/** Allow this security manager to be replaced,
if fact, allow pretty much everything. */
public void checkPermission(Permission perm) {
}

public SecurityManager getOriginalSecurityManager() {
return original;
}
}
</sscce>

--
Andrew Thompson


http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite

http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane

0 new messages