Can't call JNLP-deployed applet code from JavaScript
Asked Answered
H

1

6

This is literally my first question about Java, so bear with me.

I built a stupid simple applet class called HelloWorldApp.class from this code:

import java.applet.Applet;

class HelloWorldApp extends Applet
{
    static final long serialVersionUID = 6636669702238171717L;
    public String test()
    {
        return "Hello World!"; // Display the string.
    }
}

I then built a jar file from said class file. Afterwards, I made a stupid simple JNLP file:

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="/Content/applets" href="/Content/applets/datadelivery.jnlp">
    <information>
        <title>Hello World</title>
        <vendor>Me</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
        <jar href="HelloWorldApp.jar" main="true" />

    </resources>
    <applet-desc 
         name="Test Applet"
         main-class="HelloWorldApp"
         width="300"
         height="50">
     </applet-desc>
     <update check="background"/>
</jnlp>

I tossed all three items in the directory /Content/applets. Finally, to tie it all together, I dropped this in the body of an HTML document:

<script type="text/javascript">
    var attributes = {
        id:'deliveryApplet',
        code: 'HelloWorldApp',
        // note: I tried including "codebase: '/Content/applets'", but nothing new happened
        width: 300,
        height: 50
    };
    var parameters = {
        jnlp_href: '/Content/applets/datadelivery.jnlp'
    };
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

...and created a button with an onclick action of calling:

alert(deliveryApplet.test())

The problem? No function call; the JS console responds saying "Uncaught TypeError: Object # has no method 'test'". So somewhere along the line, deployment didn't work out. But where? Based on examples the JNLP stuff looks right.

Homesick answered 28/1, 2013 at 21:29 Comment(10)
alert(deliveryApplet.test()) is a very naive way to get access to an element in the page. I suggest you write a JS function that accepts a string for the ID and returns the appropriate element. Test it thoroughly across a range of browsers. In the applet, call the (tested and robust) JS function. Also, make sure the applet invocation declares scriptable=true.Contaminate
Change the jnlp_ref to a relative path to the jnlp file (i.e. no path). Because you don't have a paint in the Applet it's difficult to see if it gets launched. If it failed to launch, then you would get the red X and the applet didn't launch message. Clear the java cache between changes to the jnlp file and rerunning. You are loading the deployJava.js script?Economically
@Petesh: Yes, deployJava.js is definitely being loaded. I changed jnlp_href to 'datadelivery.jnlp', wiped the Java cache (Java control panel -> Temporary Internet Files -> Settings..., correct?), and tried again. Same problem. But you are right, I am at least launching. I spent some amount of time getting from the red X to the grey unpainted box I'm at now!Homesick
@AndrewThompson: The example most closely resembling my test doesn't use the scriptable parameter and does quite well. I can give it a try anyway; how would that flag be set through the JS implementation above?Homesick
@Petesh: Even though it's not Serializable?Homesick
@Homesick oddly, I never got it to run properly until I put in a 'public static final long serialVersionUID' in the applet class - I cribbed your example, set a cyan paint background so I could see it running.Economically
@Petesh I generated a UID through serialver and applied it -- the above code has been revised to show it. I compiled and built a new jar, then wiped the cache. No change. :/Homesick
huh - I pretty much built this - github.com/petesh/jnlp_applet and it works just fine, including a manual call of alert(deliveryApplet.test()) from Safari. I had to use alert(document.applets[0].test()) for firefox, though!Economically
To more correctly find the item you should use document.getElementById('deliveryApplet')Economically
@Petesh Update: Sorry it's taken me awhile to respond. Anyhoo, I observed two things: 1) I didn't have a MIME type set for .jnlp files in IIS, which would throw a wrench in things. Fixed. 2) I took the time to adopt the github example -- paint() and all -- inside my app, and lo! I discovered the applet isn't loading at all. I feel like I'm getting a path wrong somewhere...if the HTML file is in ~/administration and the applet/JNLP is in ~/Content/applets, what changes (besides the two paths in the JNLP)?Homesick
A
0

To make things simpler to deal with I'd suggest you test your applet using the "deprecated" applet tag. I remember I had strange issues with the javascript applet loader and couldn't figure out why it wasn't working the way it was supposed to work.

Abbess answered 1/3, 2013 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.