I'm trying to call a Javascript function from inside a Java Signed Applet that I developed. This goes pretty well when I use a browser with Sun Java plugin. I tested on Windows 7 and Ubuntu 10.04 with Firefox, Chrome and Internet Explorer (Windows) browsers.
When I try to run the same code on any browser with Iced Tea plugin the following error is informed:
net.sourceforge.jnlp.LaunchException: Fatal: Initialization Error: Could not initialize applet. at net.sourceforge.jnlp.Launcher.createApplet(Launcher.java:735) at net.sourceforge.jnlp.Launcher.getApplet(Launcher.java:676) at net.sourceforge.jnlp.Launcher$TgThread.run(Launcher.java:886)Caused by: java.lang.NullPointerException at net.sourceforge.jnlp.runtime.JNLPClassLoader.getPermissions(JNLPClassLoader.java:915) at java.security.SecureClassLoader.getProtectionDomain(SecureClassLoader.java:206) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:447) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at net.sourceforge.jnlp.runtime.JNLPClassLoader.findClass(JNLPClassLoader.java:1499) at net.sourceforge.jnlp.runtime.JNLPClassLoader.loadClassExt(JNLPClas sLoader.java:1526) at net.sourceforge.jnlp.runtime.JNLPClassLoader.loadClass(JNLPClassLoader.java:1341) at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) at java.lang.Class.getDeclaredMethod(Class.java:1952) at java.awt.Component.isCoalesceEventsOverriden(Component.java:6190) at java.awt.Component.access$500(Component.java:188) at java.awt.Component$3.run(Component.java:6144) at java.awt.Component$3.run(Component.java:6142) at java.security.AccessController.doPrivileged(Native Method) at java.awt.Component.checkCoalescing(Component.java:6141) at java.awt.Component.(Component.java:6110) at java.awt.Container.(Container.java:276) at java.awt.Panel.(Panel.java:64) at java.awt.Panel.(Panel.java:56) at java.applet.Applet.(Applet.java:65) at javax.swing.JApplet.(JApplet.java:130) at br.ufrj.land.tangram.measures.NetworkMeasurerApplet.(NetworkMeasurerApplet.java:11) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Nati ve Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at java.lang.Class.newInstance0(Class.java:372) at java.lang.Class.newInstance(Class.java:325) at net.sourceforge.jnlp.Launcher.createApplet(Launcher.java:726) ... 2 more Caused by: java.lang.NullPointerException at net.sourceforge.jnlp.runtime.JNLPClassLoader.getPermissions(JNLPClassLoader.java:915) at java.security.SecureClassLoader.getProtectionDomain(SecureClassLoader.java:206) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:447) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.Acc essController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at net.sourceforge.jnlp.runtime.JNLPClassLoader.findClass(JNLPClassLoader.java:1499) at net.sourceforge.jnlp.runtime.JNLPClassLoader.loadClassExt(JNLPClassLoader.java:1526) at net.sourceforge.jnlp.runtime.JNLPClassLoader.loadClass(JNLPClassLoader.java:1341) at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2442) at java.lang.Class.getDeclaredMethod(Class.java:1952) at java.awt.Component.isCoalesceEventsOverriden(Component.java:6190) at java.awt.Component.access$500(Component.java:188) at java.awt.Component$3.run(Component.java:6144) at java.awt.Component$3.run(Component.java:6142) at java.security.AccessController.doPrivileged(Native Method) at java.awt.Component.checkCoalescing(Component.java:6141) at java.awt.Component.(Component.java:6110) at java.awt.Container.(Container.java:276) at java.awt.Panel.(Panel.java:64) at jav a.awt.Panel.(Panel.java:56) at java.applet.Applet.(Applet.java:65) at javax.swing.JApplet.(JApplet.java:130) at br.ufrj.land.tangram.measures.NetworkMeasurerApplet.(NetworkMeasurerApplet.java:11) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at java.lang.Class.newInstance0(Class.java:372) at java.lang.Class.newInstance(Class.java:325) at net.sourceforge.jnlp.Launcher.createApplet(Launcher.java:726) at net.sourceforge.jnlp.Launcher.getApplet(Launcher.java:676) at net.sourceforge.jnlp.Launcher$TgThread.run(Launcher.java:886)java.lang.NullPointerException at net.sourceforge.jnlp.NetxPanel.runLoader(NetxPanel.java:154) at sun.applet.AppletPanel.run(AppletPanel.java:379) at java.lang.Thread.run(Thread. java:722) java.lang.NullPointerException at sun.applet.AppletPanel.run(AppletPanel.java:429) at java.lang.Thread.run(Thread.java:722)
The Linux flavor that I'm using with Iced Tea plugin is Ubuntu 12.04
I tried compiling my code with Sun JDK 6 and 7 and Open JDK 6 and 7 but both fail with Iced Tea plugin on browsers.
I tried my code with Iced Tea version 6 and version 7
I tried my code with Firefox and Chrome browsers on my Ubuntu
HTML (inside body tag):
<!-- JavaScript File That Contains My Javascript Function -->
<script type="text/javascript" src="./my_javascript_file.js"></script>
<!-- Applet That Will Call My Javascript Function -->
<object height="1" width="1" id="MyApplet" name="MyApplet"
java_version="1.5+" type="application/x-java-applet">
<param name="code" value="foo.MyApplet" />
<param name="archive" value="MyApplet.jar" />
<param name="JVM_PARAM" value="-Djava.security.policy=applet.policy" />
<param name="mayscript" value="true" />
<param name="scriptable" value="true" />
<param name="name" value="MyApplet" />
</object>
Javascript method that i'm trying to call (inside my_javascript_file.js):
window.performAppletCode = function (parameter_1) {
//Function code
}
Java applet code (relevant part) inside "MyApplet.jar":
package foo;
import java.util.Arrays;
import javax.swing.JApplet;
import netscape.javascript.JSException;
import netscape.javascript.JSObject;
public class MyApplet extends JApplet {
//Parameter that will be sent to Javascript function
public String parameter_1;
//JSObject that will be responsible for accessing javascript on my page
public JSObject window = null;
private Thread myThread;
private ImportantClass importantClassInstance;
@Override
public void init() {
}
@Override
public void start() {
if (this.window == null) {
try {
this.window = JSObject.getWindow(this);
if (this.window == null){
System.out.println("Window is null");
}
}
catch (JSException jse) {
jse.printStackTrace();
}
fooStart();
}
}
public void fooStart() {
importantClassInstance = new ImportantClass();
// MyThread Section
myThread = new Thread(new Runnable() {
@Override
public void run() {
// Run latency test.
int[] fooArray = importantClassInstance.fooMethod();
parameter_1 = Arrays.toString(fooArray);
//Javascript function call
window.call("performAppletCode", new Object[] {parameter_1});
}
});
myThread.start();
}
}
My applet is signed using jarsigner and the certificate is included on my HTML directory.
If anyone knows a possible clue to solving this problem, please, let me know.
Thanks, Guilherme.
When called from JS, an applet loses trust. To fix that, see AccessController.doPrivileged(...)
and this answer .
I think I found the solution, but I sill don't know why this happens. Here's the thing:
When I start my web page (with the applet and everything else) from a directory that it's located on my OS Ubuntu, that crazy error pops up on my Java console. BUT when I start my page from a directory that it's located on a remote server (eg another computer in my home LAN) everything works perfect. So, for some reason, I need to request my own page from some remote server to everything work OK. This only happens with the above situation described on my question. I will search on the web trying to find why this happens.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.