问题
I'm developing a Java applet that show a message box when you visit the site. This is my Java code:
import java.applet.Applet;
import javax.swing.JOptionPane;
public class JavaRun extends Applet {
private static final long serialVersionUID = 1L;
public void init()
{
JOptionPane.showMessageDialog(null, "hello world!");
}
}
This is the html:
<applet width='100' height='100' code='JavaRun' archive='data.jar'>
</applet>
On my computer (that have the java SDK) it's work, but when I'm using it on my laptop that have only the standard Java, I get these errors:
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source) at sun.plugin2.applet.Plugin2ClassLoader.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) Exception: java.lang.UnsupportedClassVersionError: Micro : Unsupported major.minor version 51.0... enter code here
回答1:
I believe You have different versions of Java environments on your computers. Run this command on both computers
java -version
And compare version numbers. Probably should run
javac -version
on Your development machine. If you get different version numbers update Java runtime environment on your laptop.
回答2:
UnsupportedClassVersionError: Micro : Unsupported major.minor version 51.0
This is the problem. You've compiled your applet with a class version that is not supported by the JVM you're running on the other system.
Use the -target
flags for javac
to produce class files for a given target JVM version.
回答3:
UnsupportedClassVersionError
- To compile code for a particular Java version, use the cross-compilation options. To do this properly will require an
rt.jar
of the target version (to use thebootclasspath
option ofjavac
). - To deploy code that requires a particular version, use deployJava.js
- To maintain your sanity and provide a better user experience, convert the applet to an application and launch it from a link using Java Web Start.
来源:https://stackoverflow.com/questions/9546359/java-applet-work-only-on-my-computer