Results 1 to 9 of 9
  1. #1
    warydig is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default Chat applet runs in Eclipse but not on webpage?

    Hi all,

    I have found a simple java chat tutorial online which includes a server.class, client.class and a clientapplet.class. When I run ClientApplet in eclipse it runs perfectly but when I try running the applet in a browser I just get a blank screen! I think it could be something to do with my HTML but I can't work out what is wrong... Below is the source code to Client.class, ClientApplet.class and my HTML file chat.html.

    Client.class:

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;

    public class Client extends Panel implements Runnable
    {

    // Components for the visual display of the chat windows
    private TextField tf = new TextField();
    private TextArea ta = new TextArea();
    // The socket connecting us to the server
    private Socket socket;


    // The streams we communicate to the server; these come
    // from the socket
    private DataOutputStream dout;
    private DataInputStream din;
    // Constructor
    public Client( String host, int port ) {
    // Set up the screen
    setLayout( new BorderLayout() );
    add( "North", tf );
    add( "Center", ta );
    // We want to receive messages when someone types a line
    // and hits return, using an anonymous class as
    // a callback
    tf.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    processMessage( e.getActionCommand() );
    }
    } );
    // Connect to the server
    try {
    // Initiate the connection
    socket = new Socket( host, port );
    // We got a connection! Tell the world
    System.out.println( "connected to "+socket );
    // Let's grab the streams and create DataInput/Output streams
    // from them
    din = new DataInputStream( socket.getInputStream() );
    dout = new DataOutputStream( socket.getOutputStream() );
    // Start a background thread for receiving messages
    new Thread( this ).start();
    } catch( IOException ie ) { System.out.println( ie ); }
    }
    // Gets called when the user types something
    private void processMessage( String message ) {
    try {
    // Send it to the server
    dout.writeUTF( message );
    // Clear out text input field
    tf.setText( "" );
    } catch( IOException ie ) { System.out.println( ie ); }
    }
    // Background thread runs this: show messages from other window
    public void run() {
    try {
    // Receive messages one-by-one, forever
    while (true) {
    // Get the next message
    String message = din.readUTF();
    // Print it to our text window
    ta.append( message+"\n" );
    }
    } catch( IOException ie ) { System.out.println( ie ); }
    }
    }

    ClientApplet.class:

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.net.*;

    public class ClientApplet extends Applet
    {
    public void init() {
    String host = "localhost";
    int port = 5000;
    setLayout( new BorderLayout() );
    add( "Center", new Client( host, port ) );
    }
    }


    chat.html

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
    <applet code = 'ClientApplet.class'
    archive = 'Client.jar',
    width = 300,
    height = 300 />
    </BODY>
    </HTML>


    I .jarred the class files up using: jar cvf Client.jar Client.class ClientApplet.jar.

    Does anyone have any ideas?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    hen I try running the applet in a browser I just get a blank screen!
    Look in the browser's java console for error messages. Copy and post them here.

    Remove the .class from the code = 'ClientApplet.class'
    Some browsers think the classname is class (the file would be class.class) in a package: ClientApplet

  3. #3
    warydig is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your reply! Ok I took .class out and then ran it again, this is the error message in the console:

    java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:5000 connect,resolve)
    at java.security.AccessControlContext.checkPermission (Unknown Source)
    at java.security.AccessController.checkPermission(Unk nown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkConnect(Unknown Source)
    at sun.plugin2.applet.Applet2SecurityManager.checkCon nect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at Client.<init>(Client.java:39)
    at ClientApplet.init(ClientApplet.java:18)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    Exception: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:5000 connect,resolve)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    Applets are NOT allowed to communicate with anyone other than the site they are loaded from. Are you loading your applet from a locally running server (127.0.0.1) or are you loading the applet from a file in a folder?
    Load it from the server and it should work.
    To load it from a local folder and have it work you'll need to give it permission.

  5. #5
    warydig is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Ok I loaded the applet from a local server and now I get a Red 'X' in the corner of the applet window. the java console says the following:


    java.lang.NoClassDefFoundError: Client$1
    at Client.<init>(Client.java:29)
    at ClientApplet.init(ClientApplet.java:15)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    java.lang.NoClassDefFoundError: Client$1
    at Client.<init>(Client.java:29)
    at ClientApplet.init(ClientApplet.java:15)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)


    I noticed that a file Client$1.class was created by eclipse, I'm not sure what this class does but I tried jarring that up with Client.class and ClientApplet.class but still get the same error as above! Anyone have any ideas?

    Thanks

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    hi, before you can load your socket-applet in a browser, you have to sign it. you'll find some instructions here

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default

    java.lang.NoClassDefFoundError: Client$1
    That looks like the name of an anonymous class.

    still get the same error as above
    i assume that the error is the NoClassDefFoundError:
    I don't know why you would get a class not found if the class is in the jar file. Are you sure it has the correct path and that the browser is getting the class files from the jar file?
    It should work.

    This error has nothing to do with signing the jar file.

  8. #8
    warydig is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Ok it seems to work now, I jarred the files up again using "jar cvf Client.jar *.class" and it worked, I'm not sure why I got the anonymous class error! The information you gave about running the applet from the server fixed the problem, thank you! SOLVED!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

Similar Threads

  1. Applet runs on Web page, but no database connection
    By mneskovic in forum Java Applets
    Replies: 4
    Last Post: 05-19-2010, 04:46 AM
  2. How can I change webpage by applet?
    By fantasyme in forum Java Applets
    Replies: 10
    Last Post: 04-23-2010, 07:24 PM
  3. Replies: 10
    Last Post: 10-11-2009, 07:07 PM
  4. Replies: 0
    Last Post: 10-04-2009, 02:47 PM
  5. print webpage through applet
    By shakti singh in forum Java Applets
    Replies: 1
    Last Post: 07-22-2008, 07:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •