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