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

System Exit error

1 view
Skip to first unread message

ed doyle

unread,
Nov 6, 2004, 10:23:43 AM11/6/04
to
Hi,
I am relatively new to Java programming, but I have created a simple
server application and a client applet largely from following the
examples in Deitel's "Java - How to Program Book".Everything works
perfectly if I start the server application, and then run the client
applet in appletviewer. But I thought I would get fancy and add code to
the client that if the server application was not running, the client
would gracefully shut down. Here is my first attempt which does not work.

public void start() {
while(connection == null) {
try {
connection = new Socket(InetAddress.getLocalHost(),
5000);
input = new
DataInputStream(connection.getInputStream());
output = new
DataOutputStream(connection.getOutputStream());
}
catch(IOException e) {
//e.printStackTrace();
System.out.println("Unable to connect to server!");
System.exit(0);
}
}
outputThread = new Thread(this);
outputThread.start();
}

When I run the client with no server, I get the following stack trace.

Unable to connect to server!
java.security.AccessControlException: access denied
(java.lang.RuntimePermission exitVM)
at
java.security.AccessControlContext.checkPermission(AccessControlContext.java:269)
at
java.security.AccessController.checkPermission(AccessController.java:401)
at
java.lang.SecurityManager.checkPermission(SecurityManager.java:524)
at java.lang.SecurityManager.checkExit(SecurityManager.java:736)
at java.lang.Runtime.exit(Runtime.java:88)
at java.lang.System.exit(System.java:715)
at TicTacToeClient.start(TicTacToeClient.java:64)
at sun.applet.AppletPanel.run(AppletPanel.java:377)
at java.lang.Thread.run(Thread.java:534)

Line 64 is where I added the line System.exit(0); I'm not sure what the
stack trace is telling me or where to look for the problem. I have
seen other code where a System.exit(0); line is placed in a catch
clause, so I 'think' that is valid. I am not sure if there is a problem
with putting it in a start() method, or if the issue is I am off in a
thread telling the entire applet to shut down. Any clues on what I
should read next, or what I need to next learn to cope with this problem
would be useful. I don't need the problem solved for me, just a pointer
on where to look next.

Thanks for any comments.

Ed

Tor Iver Wilhelmsen

unread,
Nov 6, 2004, 10:36:23 AM11/6/04
to
ed doyle <doy...@sprynet.com> writes:

> System.exit(0);

> Line 64 is where I added the line System.exit(0); I'm not sure what
> the stack trace is telling me or where to look for the problem.

The error is that you cannot call System.exit() in an applet. After
all, how should the VM terminate? It's running as part of the browser
- should it close the browser as well?

ed doyle

unread,
Nov 6, 2004, 10:48:54 AM11/6/04
to


Ouch! - you are right. That makes sense. I will have to think up a new
way to shut down when the server is not running. Thanks - Ed

Andrew Thompson

unread,
Nov 6, 2004, 10:57:46 AM11/6/04
to
On Sat, 06 Nov 2004 15:48:54 GMT, ed doyle wrote:

(Applet no System.exit(0))


> Ouch! - you are right. That makes sense. I will have to think up a new
> way to shut down when the server is not running.

I think the last thing you need to do is 'shut down' the applet,
as opposed to simply presenting the user with a message to the
effect that the server is not available.

There are any number of ways of doing that, the most reliable
(probably) being to use a CardLayout for the Applet with your
'client' GUI on one card, and a 'Server not available' message
(Label, TextArea etc.) on another. Flip between them as
appropriate.

With this approach, you could even have the client applet
attempt to connect to the server every minute or so and
'come to life' if the server appears.

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