Results 1 to 9 of 9
- 07-21-2010, 10:50 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
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!
- 07-21-2010, 11:04 PM #2
Look in the browser's java console for error messages. Copy and post them here.hen I try running the applet in a browser I just get a blank screen!
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
- 07-21-2010, 11:26 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
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)
- 07-21-2010, 11:31 PM #4
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.
- 07-22-2010, 09:17 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
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
- 07-22-2010, 09:35 AM #6
hi, before you can load your socket-applet in a browser, you have to sign it. you'll find some instructions here
- 07-22-2010, 01:01 PM #7
java.lang.NoClassDefFoundError: Client$1
That looks like the name of an anonymous class.
i assume that the error is the NoClassDefFoundError:still get the same error as above
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.
- 07-22-2010, 02:08 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
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!
- 09-01-2010, 03:54 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Applet runs on Web page, but no database connection
By mneskovic in forum Java AppletsReplies: 4Last Post: 05-19-2010, 04:46 AM -
How can I change webpage by applet?
By fantasyme in forum Java AppletsReplies: 10Last Post: 04-23-2010, 07:24 PM -
Applet Errors runs in jGrasp/ not in Eclipse and not in browser
By Wallsurfer in forum Java AppletsReplies: 10Last Post: 10-11-2009, 07:07 PM -
Problem: program runs with eclipse not with dat file
By livnihai in forum EclipseReplies: 0Last Post: 10-04-2009, 02:47 PM -
print webpage through applet
By shakti singh in forum Java AppletsReplies: 1Last Post: 07-22-2008, 07:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks