System Exit error

E

ed doyle

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
 
T

Tor Iver Wilhelmsen

ed doyle said:
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?
 
E

ed doyle

Tor said:
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?


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
 
A

Andrew Thompson

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.
 

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

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top