我从这里获取了一个非常简单的 Java 小程序:http: //docs.oracle.com/javase/tutorial/deployment/applet/subclass.html
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
public class HelloWorld extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel("Hello World");
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
}
当我右键单击并执行时,我可以让小程序在 Eclipse 中运行,Run As > Java Applet
但现在我试图将它放入一个 jar 文件并通过浏览器使用 jnlp 运行它。这些是我尝试这样做的步骤:
javac -d build HelloClass.java
cd build
jar cvf Hello.jar *.class
- 创建 Hello.jnlp 文件:
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
<information>
<title>Hello Applet</title>
<vendor>Self</vendor>
</information>
<resources>
<!-- Application Resources -->
<j2se version="1.6+"
href="http://java.sun.com/products/autodl/j2se" />
<jar href="Hello.jar" main="true" />
</resources>
<applet-desc
name="Hello Applet"
main-class="HelloClass"
width="300"
height="300">
</applet-desc>
<update check="background"/>
</jnlp>
- 创建html页面:
<html>
<head>
<title>Hello Applet</title>
</head>
<body>
<!-- ... -->
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = {
code:'HelloClass', width:300, height:300} ;
var parameters = {jnlp_href: 'Hello.jnlp'} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
<!-- ... -->
</body>
</html>
当我在浏览器中打开此页面时,系统会提示我允许小程序运行,但随后出现错误,并显示以下详细信息:
Exception: java.lang.UnsupportedClassVersionError: HelloClass : Unsupported major.minor version 51.0