Results 1 to 11 of 11
  1. #1
    step is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Applet not work in browser

    Hi

    I wrote a java applet client program that connect to server using socket and get data from server. It work fine in eclipce applet viewer. but it won't work in browser.
    I put html file and class files in the same directory.

    html file is below
    <HTML>
    <BODY>
    <H1 align="center">Welcome to Step's page</H1>
    <applet code="TotalPanel.class" width="310" height="580">
    there is no applet tag
    </applet>
    </BODY>
    </HTML>

    TotalPanel.class has main method in it.

    when I open html file in the browser I got following message in the terminal.

    java.lang.reflect.InvocationTargetException
    at java.awt.EventQueue.invokeAndWait(EventQueue.java: 1000)
    at javax.swing.SwingUtilities.invokeAndWait(SwingUtil ities.java:1348)
    at sun.applet.PluginAppletViewer.handleMessage(Plugin AppletViewer.java:553)
    at sun.applet.PluginAppletViewer.handleMessage(Plugin AppletViewer.java:525)
    at sun.applet.PluginStreamHandler.handleMessage(Plugi nStreamHandler.java:295)
    at sun.applet.PluginMessageHandlerWorker.run(PluginMe ssageHandlerWorker.java:72)
    Caused by: java.lang.NullPointerException
    at sun.applet.PluginAppletViewer$3.run(PluginAppletVi ewer.java:568)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:216)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 602)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)


    in the applet there is button named "connect". this is the botton to connect server.
    when I click botton "connect" on the applet. I got following message in the terminal

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at net.sourceforge.jnlp.runtime.JNLPSecurityManager.c heckPermission(JNLPSecurityManager.java:298)
    at java.lang.SecurityManager.checkConnect(SecurityMan ager.java:1048)
    at java.net.InetAddress.getAllByName0(InetAddress.jav a:1188)
    at java.net.InetAddress.getAllByName(InetAddress.java :1127)
    at java.net.InetAddress.getAllByName(InetAddress.java :1063)
    at java.net.InetAddress.getByName(InetAddress.java:10 13)
    at java.net.InetSocketAddress.<init>(InetSocketAddres s.java:142)
    at SocketClient.connect(SocketClient.java:52)
    at TotalPanel.actionPerformed(TotalPanel.java:124)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:404)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:253)
    at java.awt.Component.processMouseEvent(Component.jav a:6108)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3276)
    at java.awt.Component.processEvent(Component.java:587 3)
    at java.awt.Container.processEvent(Container.java:210 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4469)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2163)
    at java.awt.Component.dispatchEvent(Component.java:42 95)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4461)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4125)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4055)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2149)
    at java.awt.Component.dispatchEvent(Component.java:42 95)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 604)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:177)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:138)

    in this error there is line "SocketClient.connect(SocketClient.java:52)"
    I defined this SocketClient class and line 52 is below.

    socket.connect(new InetSocketAddress(hostName, port_no));

    Please help me to solve this problem.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Applet not work in browser

    Is the applet trying to connect to another site? It will need permission to connect to any site except the one it was loaded from.

  3. #3
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Applet not work in browser

    I'm just checking but I do believe applet code goes like APPLET CODE and width is WIDTH and height is HEIGHT and applet is APPLET. I'm not sure if that will help at all though.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Applet not work in browser

    Case doesn't matter in html. In fact I think lower case is preferred.

  5. #5
    Join Date
    Mar 2012
    Posts
    54
    Rep Power
    0

    Default Re: Applet not work in browser

    Quote Originally Posted by Norm View Post
    Case doesn't matter in html. In fact I think lower case is preferred.
    Oh. Ok, that makes things alot nicer for me. Thanks.

  6. #6
    step is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Applet not work in browser

    Thank you for your answer's. The applet doesn't connect to other sites except it will try to connect to server. I am using socket in the connection. but it seems problem is the applet do not able to connect to the server. how can I set permission to connect server. I searched from internet and made a change in the file "JAVA_HOME_DIRCETORY/jre/lib/security/java.policy".

    before I made change it was like below.

    // Standard extensions get all permissions by default
    grant codeBase "file:${{java.ext.dirs}}/*" {
    permission java.security.AllPermission;
    };

    and I changed it like below.

    // Standard extensions get all permissions by default
    // changed in 2012.3.15
    grant {
    permission java.security.AllPermission;
    };

    the server and the applet is on the same linux computer. I also updated java on that linux.

    now the applet will work if I access it from the linux computer.
    but it does not work if I access it from another computer ( another computer is windows ).
    I checked java plugin of browser on the windows computer and it says it is newest.

    I got following error on java console on windows

    java.security.AccessControlException: access denied (java.io.FilePermission grandfather_left.jpg read)
    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.checkRead(Unknown Source)
    at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
    at sun.awt.SunToolkit.getImage(Unknown Source)
    at GraphPanel.<init>(GraphPanel.java:54)
    at TotalPanel.init(TotalPanel.java:46)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
    exception: java.security.AccessControlException: access denied (java.io.FilePermission grandfather_left.jpg read)

    is it security problem ? and how can I fix it ?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Applet not work in browser

    it does not work if I access it from another computer
    The applet should be able to get a file from the server that the HTML page containing the <APPLET tag that loaded the applet was loaded from.
    The applet can not read files from a disk. It should ask the server for the files using an HTTP GET for example using a Socket or HttpURLConnection.

    Where is the file the applet is trying to read? What class is the applet using to read the file?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    step is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Applet not work in browser

    Quote Originally Posted by Norm View Post
    The applet should be able to get a file from the server that the HTML page containing the <APPLET tag that loaded the applet was loaded from.
    The applet can not read files from a disk. It should ask the server for the files using an HTTP GET for example using a Socket or HttpURLConnection.

    Where is the file the applet is trying to read? What class is the applet using to read the file?

    Thank you for your replay and sorry for my terrible explanation. I will try explain more understandable.
    now I am using 2 computers. 1 of them is linux and other 1 is windows. I wrote 2 programs. 1 of them is written by language C. other 1 is written by java (this is the applet). I called server that C program and client that java program. now let's call them C_server and Java_server ( the applet ).

    Java_server has to connect to C_server and get data from C_server then draw some image.
    I am using socket in this connection.

    C_server and Java_server are both in linux computer. The file ( grandfather_left.jpg ) the Java_server trying to read is also in linux computer.
    I put class files ( of Java_server ) and the file ( grandfather_left.jpg ) in the ~/public_html/. there is also index.html file in the ~/public_html/. index.html loads the applet.

    Now, when I access from linux computer to index.html the applet works right.
    But, when I access from windows computer to indexh.html the applet doesn't work and give the error message.

    I checked whether it works well or not if Java_server doesn't read any file. When it doesn't read any file it worked well even when I access to it from windows.
    but if it use file ( grandfather_left.jpg ), it doesn't work.

    Is it some security proglem or must I put the file ( grandfather_left.jpg ) in some other place?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Applet not work in browser

    What OS is running on the server or on the client PC should not make any difference.

    What class and methods is the java applet using to get the bytes of the grandfather_left.jpg fiile?
    Where is the file located? Is it on the server or on the client PC where the browser is executing?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    step is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Applet not work in browser

    Problem solved. Now it is workig well.

    The file grandfather_left.jpg is located in the same directory with class files and index.html file.
    I mean all files ( pictures, class files, index.html file ) are in directory ~/public_html/ on the linux computer (server).

    First I wrote java application program using JFrame and then turn it to Applet using JApplet.

    I used following method to read picture
    Image image = getToolkit().getImage("grandfather_left.jpg");

    It worked well in application but it does not work on the Applet.
    I changed it to following method and it worked well on the applet.
    Image image = Toolkit.getDefaultToolkit().createImage(getClass() .getResource("grandfather_left.jpg"));

    thank you very much.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Applet not work in browser

    Glad you got it to work.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Simple rectangle display applet doesn't work in browser
    By stainluss in forum Java Applets
    Replies: 7
    Last Post: 01-31-2012, 11:38 PM
  2. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 01:45 PM
  3. applet working in applet viewer, not browser
    By plasticfood in forum Java Applets
    Replies: 4
    Last Post: 06-24-2011, 06:47 PM
  4. No applets work in browser
    By Chotor in forum Java Applets
    Replies: 1
    Last Post: 05-25-2010, 04:19 PM
  5. Why the web browser needs to refresh first so that creating table may work? Pls help.
    By MarkSquall in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-29-2010, 11:14 AM

Posting Permissions

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