System.ext()

S

steph

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.
 
M

Mark Thornton

steph said:
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
 
A

Andrew Thompson

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)
 
M

Mark Thornton

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




With or without a GUI?




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




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
 
A

Andrew Thompson

steph wrote: ... ...
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.
 
S

Sudsy

Andrew said:
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.
 
A

Andrew Thompson

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
 
A

Andrew Thompson

<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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top