繁体   English   中英

使用jnlp获取java applet在浏览器中运行

[英]Get java applet to run in browser using jnlp

我有一个非常简单的java applet,我从这里开始: 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");
        }
    }
}

当我右键单击并执行Run As > Java Applet时,我可以让applet在eclipse中运行,但现在我正在尝试将其放入jar文件并使用jnlp通过浏览器运行它。 这些是我尝试这样做的步骤:

  1. javac -d build HelloClass.java
  2. cd build
  3. jar cvf Hello.jar *.class
  4. 创建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>
  1. 创建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>

当我在浏览器中打开此页面时,系统会提示您允许applet运行,但后来出现错误,其中包含以下详细信息:

Exception: java.lang.UnsupportedClassVersionError: HelloClass : Unsupported major.minor version 51.0

代码显然是由1.7 SDK编译而不使用任何交叉编译选项 ,而试图加载它的JRE是版本6或更低版本。

要编译特定Java版本的代码,请使用交叉编译选项。 要正确执行此操作,需要使用目标版本的rt.jar (以使用javacbootclasspath选项)。

编译器版本和JRE版本之间存在不匹配,请确保它们具有相同(主要)版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM