问题
I'm trying to embed a Java Applet using the <OBJECT> tag, which is the XHTML Strict way of doing it.
After browsing lots of sites, I tried this example which seems to work pretty well:
<!--[if !IE]> Firefox and others will use outer object -->
<object classid="java:Sample2.class"
type="application/x-java-applet"
archive="Sample2.jar"
height="300" width="450" >
<!-- Konqueror browser needs the following param -->
<param name="archive" value="Sample2.jar" />
<!--<![endif]-->
<!-- MSIE (Microsoft Internet Explorer) will use inner object -->
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase="http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab"
height="300" width="450" >
<param name="code" value="Sample2" />
<param name="archive" value="Sample2.jar" />
<strong>
This browser does not have a Java Plug-in.
<br />
<a href="http://java.sun.com/products/plugin/downloads/index.html">
Get the latest Java Plug-in here.
</a>
</strong>
</object>
<!--[if !IE]> close outer object -->
</object>
<!--<![endif]-->
I downloaded that Sample2.jar and works perfectly on localhost.
Now, I replaced Sample2.class for the one I need to use (ar.uba.exactas.infovis.ivides.Scatterplot.class) and using my own JAR files (archive="piccolo.jar piccolox.jar netscape.jar scatterplot.jar"):
<!--[if !IE]> Firefox and others will use outer object -->
<object
classid="java:ar.uba.exactas.infovis.ivides.Scatterplot.class"
type="application/x-java-applet"
archive="piccolo.jar piccolox.jar netscape.jar scatterplot.jar"
height="300" width="450" >
<!-- Konqueror browser needs the following param -->
<param name="archive" value="piccolo.jar piccolox.jar netscape.jar scatterplot.jar" />
<!--<![endif]-->
<!-- MSIE (Microsoft Internet Explorer) will use inner object -->
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
codebase="http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab"
height="300" width="450" >
<param name="code" value="ar.uba.exactas.infovis.ivides.Scatterplot" />
<param name="archive" value="piccolo.jar piccolox.jar netscape.jar scatterplot.jar" />
<strong>
This browser does not have a Java Plug-in.
<br />
<a href="http://java.sun.com/products/plugin/downloads/index.html">
Get the latest Java Plug-in here.
</a>
</strong>
</object>
<!--[if !IE]> close outer object -->
</object>
<!--<![endif]-->
After doing so, I'm gettin this log dump:
java.lang.ClassNotFoundException: ar.uba.exactas.infovis.ivides.Scatterplot.class
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed:http://localhost/infovisUBA/2008-2C/tpfinal/bin/ar/uba/exactas/infovis/ivides/Scatterplot/class.class
at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 7 more
Excepción: java.lang.ClassNotFoundException: ar.uba.exactas.infovis.ivides.Scatterplot.class
The only difference I see is that I'm using a class inside a package.
Also, please note I did make this work using the <APPLET> tag, but I cannot make it with <OBJECT>.
Any clue?
回答1:
Have you by any chance written this:
<param name="code"
value="ar.uba.exactas.infovis.ivides.Scatterplot.class" />
<param name="archive"
value="piccolo.jar piccolox.jar netscape.jar scatterplot.jar" />
instead of:
<param name="code"
value="ar.uba.exactas.infovis.ivides.Scatterplot" />
<param name="archive"
value="piccolo.jar piccolox.jar netscape.jar scatterplot.jar" />
The difference is the lack of ".class" at the end of the code value. Judging by the example, it should be at the end of the classid
attribute, but not at the end of the value for the code
param.
That's what the stack trace suggests to me:
Excepción: java.lang.ClassNotFoundException:
ar.uba.exactas.infovis.ivides.Scatterplot.class
I wouldn't expect to see the ".class" at the end of the class name.
回答2:
If it's not what Jon Skeet suggested, then check your JAR file manifests and ensure that your main class reference doesn't end in ".class". The Exception:
Excepción: java.lang.ClassNotFoundException:
ar.uba.exactas.infovis.ivides.Scatterplot.class
indicates that somehow, you're telling Java to run ar.uba.exactas.infovis.ivides.Scatterplot.class
instead of ar.uba.exactas.infovis.ivides.Scatterplot
. You just have to find where this is occurring.
Also notice in the middle of the Exception dump, the following complaint (wrapped by me):
Caused by: java.io.IOException:
open HTTP connection failed:
http://localhost/infovisUBA/2008-2C/tpfinal/
bin/ar/uba/exactas/infovis/ivides/Scatterplot/class.class
it is somehow trying to load the wrong class ... you have an extra ".class"
or "/class"
somewhere in your HTML. Just to try this, if you have not already tried this, replace
classid="java:ar.uba.exactas.infovis.ivides.Scatterplot.class"
with
classid="java:ar.uba.exactas.infovis.ivides.Scatterplot"
Also, do you get the same Exception in both browsers?
回答3:
I'm having the same problem with my applet. It runs fine from IE, but fails with ClassNotFoundException. The error message indicates, that the Firefox java plugin tries to download the class file from the server separately, and not from the supplied jar file.
I found out, that in Firefox, the order of the archive and code attributes matters. My original spec was:
applet name="XYZ" code="x.y.Applet" archive="xxx.jar" codebase="" width='100%' height='100%' mayscript="mayscript"
I changed the order to:
applet name="XYZ" archive="xxx.jar" code="x.y.Applet" width='100%' height='100%' mayscript="mayscript"
And it works now.
回答4:
Well, this was a hard one...
Struggled a lot of time but finally found that the problem was Opera itself. I was using an alpha version which had this bug. Now it works great!
来源:https://stackoverflow.com/questions/651032/classnotfoundexception-in-java-applet-using-object-tag