Results 1 to 12 of 12
  1. #1
    redforce.bala is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Exclamation Applet with javascript

    Hi all


    This is my first JSObject program.In this program i trired to execute a javascript from applet.But i am getting error like this.

    Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: netscape/javascript/JSObject
    at JavaToJavaScript.actionPerformed(JavaToJavaScript. java:30)
    at java.awt.Button.processActionEvent(Button.java:392 )
    at java.awt.Button.processEvent(Button.java:360)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Caused by: java.lang.ClassNotFoundException: netscape.javascript.JSObject
    at sun.applet.AppletClassLoader.findClass(AppletClass Loader.java:211)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.applet.AppletClassLoader.loadClass(AppletClass Loader.java:144)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    ... 12 more

    I attached my source code here

    Java Source code

    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import netscape.javascript.JSObject;

    public class JavaToJavaScript extends Applet implements ActionListener
    {
    private static final long serialVersionUID = 1L;

    Button b1;
    private static final String ex="EXECUTE";

    public void init()
    {
    setLayout(new FlowLayout());
    b1=new Button();
    b1.addActionListener(this);
    b1.setActionCommand(ex);
    b1.setLabel("Execute Javascript");
    add(b1);
    }

    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getActionCommand().equals(ex))
    {
    JSObject win = JSObject.getWindow(this);
    win.call("callHello", new String[]{"RedForce"});
    }
    }

    }


    Html source code

    <html>
    <script type="text/javascript">
    function callHello(name)
    {
    alert("hello "+name);
    }
    </script>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Testing</title>
    </head>
    <body>
    <h1>Applet access javascript method</h1>
    <applet width=300 height=100 MAYSCRIPT code="JavaToJavaScript.class">
    </applet>
    </body>
    </html>

    Kindly give some valid suggestions to overcome this problem..thanks in advance..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default

    Does your Applet tag need to include the dependency on whatever jar file holds that JSObject?

  3. #3
    redforce.bala is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    ya i included the plugin.jar(contains JSObject class) file in the classpath.After that also i am getting the same error.could u help me to overcome this..

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default

    No, you misunderstand...your applet tag:
    Java Code:
    <applet width=300 height=100 MAYSCRIPT code="JavaToJavaScript.class">
    contains no reference to the jar file.

  5. #5
    redforce.bala is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    ok


    How to give the jar file in applet tag can u give an example..or else u include in the tag above i will change according to my system..Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default

    There's this thing called Google, which I'm sure will point you to the documentation on Oracles site that will explain what to put in an applet tag for dependencies.

    I don't write applets, so I'd have to Google to find out...

  7. #7
    redforce.bala is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    Ok

    i included the jar file in the applet tag as

    <applet width=300 height=100 MAYSCRIPT code="JavaToJavaScript.class" archive="D:/Program Files/Java/jdk1.6.0_20/jre/lib/plugin.jar">


    After that also i am getting the same error.What could be the problem?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default

    The jar file needs to be on the server.
    The archive attribute references it using a URL (I think), relative to the class (possibly)...or put another way, what does the documentation for archive say?

    ETA: Hmm, it's part of plugin? Then there might well be something else up then.
    Are you running this under Mozilla?
    Last edited by Tolls; 10-01-2010 at 09:53 AM.

  9. #9
    redforce.bala is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    No i am actually running this from eclipse.And also from command prompt like

    appletviewer Javatojavascript.html

    Just now i tried executing this program(html file) in browser its working in all browsers.But i want to know why its not working in command prompt and eclipse..

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default

    Ah.
    Appletviewer doesn't work with JSObject.
    Eclipse uses Appletviewer.

    I think the reason is that applet viewer doesn't actually have a javascript engine associated with it, so can't run with JSObject.

  11. #11
    redforce.bala is offline Member
    Join Date
    Sep 2010
    Posts
    23
    Rep Power
    0

    Default

    ok thanks mr.tolls

    Can you give ur mail id if u wish..Because i can directly post the queries to u.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,087
    Rep Power
    20

    Default

    Forums are for spreading information, so ask on the forum.
    PMs and emails (which I wouldn't give out anyway) don't do that.

Similar Threads

  1. JavaScript help
    By Jeremy720 in forum New To Java
    Replies: 1
    Last Post: 11-08-2008, 10:36 PM
  2. [SOLVED] calling javascript from applet method
    By shwein in forum Java Applets
    Replies: 5
    Last Post: 11-01-2008, 04:45 PM
  3. [SOLVED] calling javascript from applet
    By shwein in forum Java Applets
    Replies: 5
    Last Post: 10-31-2008, 11:45 PM
  4. javascript
    By fiero in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 01:45 PM
  5. Java applet and Javascript type problem
    By Interdit in forum Java Applets
    Replies: 0
    Last Post: 05-27-2008, 01:38 PM

Tags for this Thread

Posting Permissions

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