如何在Applet中加载外部jar文件?

时间:2018-04-13 21:34:09

标签: java eclipse

所以我正在使用C#开发一个项目,但为了让我进步,我需要运行我的jar文件。 This is the jar file

jar文件是一个名为Runescape的游戏,如果我要双击那个jar文件,它就不会做太多,所以我在看这个例子

Applet applet = (Applet)classLoader.loadClass("client").newInstance();
applet.setStub(stub);
applet.setSize(new Dimension(763, 504));
applet.init();
applet.start();
i.add(applet);
i.pack();
i.setDefaultCloseOperation(3);
label.setVisible(false);

通过它的外观采用jar文件并在Applet中运行它以向用户提供游戏的可视化表示。 并认为这可能有所帮助。

既然我是一名具有较小Java经验的C#开发人员,我发现很难知道从哪里开始,我已经安装了IntelliJ并准备创建一个项目但是我需要做些什么才能让jar文件运行在Applet中,以后我可以编译该项目并将其用作"客户端"对于游戏?

1 个答案:

答案 0 :(得分:0)

从应用程序运行applet实际上并不那么容易(并且不是部分推荐的)。 我为你的jar文件写了一个小问题测试并遇到一个问题,我相信我错过了一个正确的AppletStub

import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

class appletrunner
{
    public static void main(String[] args) throws Exception //This is not proper Exception handling!
    {
        JFrame frame = new JFrame("test");
        File jar = new File("gamepack_8614663.jar");
        URLClassLoader classLoader = new URLClassLoader(new URL[] {jar.toURI().toURL()}, appletrunner.class.getClassLoader());
        Class<?> client = classLoader.loadClass("client");
        Applet applet = (Applet)client.newInstance();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                applet.stop();
                applet.destroy();
            }
        });
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
        applet.init(); // Exception here
        applet.start();
    }
}

这在applet.init()上失败,带有一个神秘的异常(由runescape jar使用的混淆器引起)。但我认为使用适当的AppletStub本身需要AppletContext它才能正常工作。

您可以尝试为这些接口实现一个类,并使用addStub()将它们添加到applet中。希望这有点帮助。