Sign in to follow this  
LizardCPP

[web] JSObject applet- JavaScrip integartion problems

Recommended Posts

Hey I'm trying to call Java from javascript and also call javascript from an applet. The first thing works well, but calling java script from an applet is what I'm having problem with. I'm getting the exception:
netscape.javascript.JSException
	at netscape.javascript.JSObject.getWindow(Unknown Source)
	at HelloWorldApplet.testAlert(HelloWorldApplet.java:39)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.plugin.javascript.invoke.JSInvoke.invoke(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
	at sun.plugin.liveconnect.PrivilegedCallMethodAction.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.plugin.liveconnect.SecureInvocation$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.plugin.liveconnect.SecureInvocation.CallMethod(Unknown Source)
	at sun.plugin.liveconnect.SecureInvocation.access$300(Unknown Source)
	at sun.plugin.liveconnect.SecureInvocation$CallMethodThread.run(Unknown Source)
load: class colors.class not found.
java.lang.ClassNotFoundException: colors.class
	at sun.applet.AppletClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.applet.AppletClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.applet.AppletClassLoader.loadCode(Unknown Source)
	at sun.applet.AppletPanel.createApplet(Unknown Source)
	at sun.plugin.AppletViewer.createApplet(Unknown Source)
	at sun.applet.AppletPanel.runLoader(Unknown Source)
	at sun.applet.AppletPanel.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
	at sun.applet.AppletClassLoader.getBytes(Unknown Source)
	at sun.applet.AppletClassLoader.access$100(Unknown Source)
	at sun.applet.AppletClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	... 10 more









I'm follwoing the tutorial/guidelines on this page: link This is my code now: Applet
import netscape.javascript.JSObject;

import java.applet.Applet;
import java.awt.Graphics;

/**
 * Created by IntelliJ IDEA.
 * User: xxxxxxxxxxxx
 * Date: Nov 12, 2005
 * Time: 4:17:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class HelloWorldApplet extends Applet
{
    String myString;

    public void init()
    {
        myString = "Hello, world!";
    }

    public void paint(Graphics g)
    {
        g.drawString(myString, 25, 20);
    }

    public void setString(String aString)
    {
        myString = aString;
        repaint();
    }

    public void testAlert()
    {
        try
        {
            String args2[] = {""};
            JSObject win = JSObject.getWindow(this);
            win.call("test", args2);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}






html
<html>
    <head>
        Testing javascript and LiveConnect
    </head>
    <body>


        <form name="form1">
        <input type="button" value="Set String"
           onClick="document.Hello.setString(document.form1.str.value)">
        <br>
        <input type="text" size="20" name="str">
        </form>

        <form name="form2">
        <input type="button" value="Alert" onClick="document.Hello.testAlert()">
        </form>
        <br>

        <applet archive="HelloWorld.jar" code="HelloWorldApplet.class" name="Hello" width=150 height=25>
        </applet>



        <script type="javascript">
            function test()
            {
               alert("Alering")
            }
        </script>
    </body>
</html>







My first thought was that: Ok I'm Security exception, that would mean that I need a signed applet. So I signed the applet with a test certificate, but I'm still getting this exception. It's when you are pressing alert that that should trigger a alert in the browser, but it don't . If you check the java console you see the exception. Lizard [Edited by - LizardCPP on November 13, 2005 3:25:49 AM]

Share this post


Link to post
Share on other sites
Ok I solved this by passing the "mayscript" in the applet tag.

<applet name="TestName" width="150" height="50" archive="Test.jar" mayscript />

Lizard

Share this post


Link to post
Share on other sites
Remember that, as you seem to be using XHTML, attributes with no value aren't actually well-formed XML.

The conventional way is


<applet name="TestName" width="150" height="50" archive="Test.jar" mayscript="mayscript" />


Mark

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this