簡體   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