System Exit error

Discussion in 'Java' started by ed doyle, Nov 6, 2004.

  1. ed doyle

    ed doyle Guest

    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
     
    ed doyle, Nov 6, 2004
    #1
    1. Advertising

  2. ed doyle <> 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?
     
    Tor Iver Wilhelmsen, Nov 6, 2004
    #2
    1. Advertising

  3. ed doyle

    ed doyle Guest

    Tor Iver Wilhelmsen wrote:
    > ed doyle <> 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?



    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
     
    ed doyle, Nov 6, 2004
    #3
  4. 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
     
    Andrew Thompson, Nov 6, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FuZHk=?=

    Code to Exit Web App and Exit Internet Explorer

    =?Utf-8?B?U2FuZHk=?=, Aug 3, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    7,926
    =?Utf-8?B?U2FuZHk=?=
    Aug 5, 2005
  2. Joe Smith
    Replies:
    4
    Views:
    65,961
    sandeep1976
    Nov 8, 2006
  3. Replies:
    2
    Views:
    468
    Jeff Epler
    May 31, 2005
  4. QQ
    Replies:
    5
    Views:
    532
    Jonathan Adams
    May 10, 2005
  5. Vicky

    Difference between exit(0) & exit (1)

    Vicky, Aug 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    714
    Kenneth Brody
    Aug 8, 2006
Loading...

Share This Page