1

我在现场运行 Java Applet 时遇到问题。Applet 本身可以从 jar 文件本地运行,并且在编译时没有错误或警告。我什至成功地签署了它,但是当我尝试从网站运行它时,我总是得到 java.lang.reflect.InvocationTargetExpection 。任何想法我做错了什么?下面是小程序源代码:

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.io.*; 
public class test {

    private static void createAndShowGUI()  {

        JFrame frame1 = new JFrame("JAVA");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton(" Remote Desktop <<");
        //Add action listener to button
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
             { 
                try   {
                Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\mstsc.exe"); //calls cmd.exe and prints hello world to console
                BufferedReader in = new BufferedReader( 
                new InputStreamReader(p.getInputStream())); 
                String line = null; 
                while ((line = in.readLine()) != null) { 
                System.out.println(line); 
                } 
                } catch (IOException l) { 
                l.printStackTrace(); 
                } 
             }

        });      

        frame1.getContentPane().add(button);
        frame1.pack();
        frame1.setVisible(true);
    }


    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

这里是来自 java 控制台的输出:

java.lang.UnsupportedClassVersionError: button : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    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: button : Unsupported major.minor version 51.0
4

1 回答 1

1
  1. 您已经使用 JVM 版本 7 编译了您的代码,但是您正在使用旧版本的 JVM(例如 1.6)运行您的小程序。
  2. 您创建了一个 Java 应用程序(使用 main 方法),而不是 Applett。要创建 Applett,您的类需要扩展ApplettJApplet.
于 2012-08-26T10:39:33.320 回答