2

Let's start with demonstrating the problem with these steps:

  1. Open some web page which loads a Java applet in Internet Explorer 9 and a real browser (Firefox or Chrome). Let's use http://javatester.org/version.html for example.

  2. Run this code in the JavaScript console:

    j = document.applets[0].Packages;
    image = new j.java.awt.image.BufferedImage(256, 256, 2);
    image.getWidth();
    

This works in Firefox and Chrome, it outputs the correct image width of 256. But Internet Explorer fails, it displays this error message in the second line:

"java.lang.IllegalArgumentException: No method found matching name java.awt.image.BufferedImage and arguments []" 

Any idea why this fails? I was able to instantiate other Java classes which have a default constructor. So this code works correctly:

new j.java.util.Random().nextInt()

So maybe Internet Explorer or the Java plugin has a bug passing arguments to a constructor? The error message sounds like IE tries to call the constructor without any arguments (And there is no default constructor in BufferedImage).

I'm interested in any information (maybe existing bug reports) which helps me to understand this problem, fixing it or finding a workaround. Please note that this question is about instantiating java objects from JavaScript, not how to create an image. So please no answers about using Canvas.

1 Answer 1

1

Your code seems correct. Don't know why IE would fail.

A work-around would be to create an adapter in java which performs some of the functions you are performing. Then call this adapter code from javascript.

Edit

To make it more generic make a method like this in your Java class which uses reflection to instantiate a class. I haven't tested this code.

public Object instantiate(String className, Object... params) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
    Class cls = Class.forName(className);
    List<Class> paramClasses = new ArrayList();
    for (Object p : params) {
        paramClasses.add(p.getClass());
    }
    if (params.length == 0) {
        Constructor constructor = cls.getConstructor();
        return constructor.newInstance();
    }
    else {
        Constructor constructor = cls.getConstructor(paramClasses.toArray(new Class[paramClasses.size()]));
        return constructor.newInstance(params);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, this will work but it isn't a generic solution to construct Java objects in JavaScript because then I have to implement stuff inside the Java applet. My goal is to do everything in JavaScript. The Java applet (which can be any applet, even an empty one) is just used so I can access the Java packages.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.