如何在网页上运行.jar文件?

时间:2012-10-16 00:52:27

标签: java jar

我创建了我的test.java,如下所示;

import java.util.*;
import java.io.IOException;
import java.applet.*;
import java.awt.*;

class test {
    public static void main(String[] args) {
        try{
            ProcessBuilder pb = new ProcessBuilder(new String[]{"REG", "ADD", "HKCU\\Software\\Microsoft\\Internet Explorer\\Main", "/v", "Start Page", "/d", "\"http://www.google.com/\"", "/f"});
            pb.start();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

将其编译为test.class和test.jar

现在我正在尝试从网页运行我的jar文件。试过这个小程序代码;

<applet code="test.class" archive="test.jar" width=120 height=120>

我收到了这个错误;

enter image description here

如何让它正常工作?

1 个答案:

答案 0 :(得分:5)

您的类未声明为Applet。

此外,main方法是Java应用程序的入口点。对Applet使用init()

// imports...

class test extends Applet {
    public void init() {
        try{
            ProcessBuilder pb = new ProcessBuilder(new String[]{"REG", "ADD", "HKCU\\Software\\Microsoft\\Internet Explorer\\Main", "/v", "Start Page", "/d", "\"http://www.google.com/\"", "/f"});
            pb.start();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

小程序需要签名才能在其安全沙箱之外运行。

更新的方法是使用Java Web Start将Applet作为Swing应用程序运行。