質問

I have a very simple java applet that I took from here: 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");
        }
    }
}

I can get the applet to run in eclipse when I right click and do Run As > Java Applet but now I'm trying to put it into a jar file and run it using jnlp through the browser. These are the steps I've taken to try and do that:

  1. javac -d build HelloClass.java
  2. cd build
  3. jar cvf Hello.jar *.class
  4. Create Hello.jnlp file:
    <?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. Create html page:
    <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>

When I open this page in my browser I get prompted to allow the applet to run but then I get a error with the following details:

Exception: java.lang.UnsupportedClassVersionError: HelloClass : Unsupported major.minor version 51.0
役に立ちましたか?

解決

The code was apparently compiled by a 1.7 SDK without using any cross-compilation options, while the JRE that is trying to load it, is version 6 or less.

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 the bootclasspath option of javac).

他のヒント

There is a mismatch between the compiler version and the JRE version, make sure they are of the same (major) version.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top