2

I have a Java applet on a webp age which I am trying to code against with Javascript.

public class WebCapture extends Applet {

  public String[] getSourceDeviceNames() {
    return new String[] {'Device 1', 'Device 2'};
  }

  public void acquireImage() {
    //...
  }

}

I compile this applet and supporting libraries into a jar file and it is included in my HTML page like so:

<embed id="webCaptureApplet"
 code="com.brian.webcapture.applets.WebCapture"
 archive="jar/WebCapture.jar"
 type="application/x-java-applet">

    <noembed>
      Oops! It looks like Java is not installed. =[
    </noembed>

</embed>  

This seems to be effective, I see a blank Java applet rendered to the screen and if I call

document.getElementById('webCaptureApplet');

I can alert() the object and receive a response [object HTMLEmbedElement]

Great. Now, when I try to execute the following code:

var applet = document.getElementById('webCaptureApplet');
var devices = applet.getSourceDeviceNames();

for(var i = 0; i < devices.length; i++) {
  alert(devices[i]);
}

When I attach a breakpoint to the line containing for() and inspect the object with firebug, the toString() value of devices is [Ljava.lang.String;@1d0b3465. I think this makes sense as an array is being passed back and it is not toString() friendly for showing a simple value.

When I look at devices.length in the foor loop, the value shown is the method stub for a public netscape.javascript.JSException(java.lang.String, java.lang.String, int, java.lang.String, int)

I have read countless how to's on Javascript calling Java and I can not see where I have gone wrong with what seems to be pretty seamless interop.

I should say that at one point it was working through Firefox on OS X, but it is holding on to a cached applet, so I am confused what the problem is there. I have tried deleting my cache on both my work station and my laptop and they still seem to cling to old jar's. I am not sure how much this is effecting me.

It seems to be that it does not make a difference if my applet's getSourceDeviceNames returns a string or an array of strings. It will still show the same errors on inspection. I am not sure if the cache is effecting that.

2 Answers 2

1

According to http://www.uow.edu.au/~hasan/buss930/jscript/packages.htm#1014128 "Java arrays are wrapped with a JavaScript object that understands array.length and array[index]." which works fine with Sun Java, but returns the JSException while running on OpenJDK.

The only method I was able to find, is to count the elements of the array.

var length = 0;
while(array[length])
    length++;

It seems, that this feature is buggy in OpenJDK.

0

You are confused with arrays here. When you are invoking java code from javascript the simple types are mapped naturally while other types may cause some problems. For example array is mapped to JavaArray (http://www.nusphere.com/kb/jscoreguide/lc.html).

I performed a short search for you and did not managed to find haw to extract elements from the array in javascript side. You are welcome to continue search but if you can modify code of your applet probably better way is to expose method getSourceDeviceName(int index) that will return then name of device #i and getNumberOfSourceDeviceNames() that returns the array length.

Working with primitives is always easier and more portable than with other types when working with live connect.

1
  • Thanks, I appreciate the resource in the link you provided. I understand what you are saying about primitive types, and I may be unable to access the JavaArray by index in the manner represented in my code sample. However, that code block is not being reached. I am still receiving problems with ostensibly supported procedure. (length is a readonly property of JavaArray). This fails on instantiation, and nothing works with the object.
    – Brian
    May 15, 2012 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.