有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在C中嵌入jar文件#

我有一个加载游戏的jar文件,我想把它嵌入C#应用程序。 有什么方法可以做到这一点,我以为你可以通过使用IKVM做到,但我似乎可以找到如何做到这一点。谢谢

如果您想查看我的java代码:(如果您尝试一下,将需要一段时间才能加载)

import java.applet.*;
import java.awt.Dimension;
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;

public class OSRSLoader
    implements AppletStub
{
    static JMenuBar menubar;
    static JFrame i;

    public static void main(String args[])
        throws Exception
    {
        new OSRSLoader();
    }

    public OSRSLoader()
        throws Exception, FileNotFoundException
    {
        JFrame i = new JFrame("RS Oldschool Client Beta V1:00");

        i.setSize(new Dimension(763, 514));
        i.setPreferredSize(new Dimension(763, 514));
        JLabel label = new JLabel();
        ImageIcon icon = new ImageIcon(new URL("http://www.runescape.com/image/rsp777/oldschool_ani.gif"));
        icon.setImageObserver(null);
        label.setIcon(icon);
        i.add(label);
        i.pack();
        i.setVisible(true);
        String str = getPageSource(new URL("http://oldschool38.runescape.com"));
        Pattern pattern = Pattern.compile("gamep\\w+");
        Matcher match = pattern.matcher(str);
        if(match.find())
        {
            OSRSLoader stub = new OSRSLoader(Pattern.compile("<param name=\"([^\\s]+)\"\\s+value=\"([^>]*)\">"), str);
            Download("http://oldschool38.runescape.com/", (new StringBuilder(String.valueOf(match.group(0)))).append(".jar").toString());
            stub.setCodeBase(new URL((new StringBuilder("http://oldschool38.runescape.com/")).append(match.group(0)).append(".jar").toString()));
            stub.setDocumentBase(new URL((new StringBuilder("http://oldschool38.runescape.com/")).append(match.group(0)).append(".jar").toString()));
            stub.getParameter((String)parameters.get("java_arguments"));
            URLClassLoader classLoader = new URLClassLoader(new URL[] {
                new URL("file:gamepack.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);
        }
    }

    private void createMenuBar() {


    }

    public OSRSLoader(Pattern parameterPattern, String frameSource)
    {
        String key;
        String value;
        for(Matcher param = parameterPattern.matcher(frameSource); param.find(); System.out.println((new StringBuilder("Parameter Loaded. Key = ")).append(key).append(", value =").append(value).toString()))
        {
            key = param.group(1);
            value = param.group(2);
            parameters.put(key, value);
        }

    }

    private String getPageSource(URL url)
        throws IOException, InterruptedException
    {
        URLConnection cn = url.openConnection();
        cn.addRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
        cn.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        cn.addRequestProperty("Accept-Encoding", "gzip,deflate");
        cn.addRequestProperty("Accept-Language", "en-gb,en;q=0.5");
        cn.addRequestProperty("Connection", "keep-alive");
        cn.addRequestProperty("Host", "www.runescape.com");
        cn.addRequestProperty("Keep-Alive", "300");
        cn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.01");
        DataInputStream di = new DataInputStream(cn.getInputStream());
        byte tmp[] = new byte[cn.getContentLength()];
        di.readFully(tmp);
        di.close();
        Thread.sleep(250 + (int)Math.random() * 500);
        return new String(tmp);
    }

    public void appletResize(int i, int j)
    {
    }

    public void setCodeBase(URL codeBase)
    {
        this.codeBase = codeBase;
    }

    public void setDocumentBase(URL documentBase)
    {
        this.documentBase = documentBase;
    }

    public AppletContext getAppletContext()
    {
        return null;
    }

    public URL getCodeBase()
    {
        return codeBase;
    }

    public URL getDocumentBase()
    {
        return documentBase;
    }

    public String getParameter(String name)
    {
        return (String)parameters.get(name);
    }

    public boolean isActive()
    {
        return false;
    }

    public static void Download(String world, String archive)
        throws Exception
    {
        URLConnection jarConnection = (new URL((new StringBuilder(String.valueOf(world))).append(archive).toString())).openConnection();
        FileOutputStream out = new FileOutputStream("./gamepack.jar");
        InputStream input = jarConnection.getInputStream();
        byte info[] = new byte[1024];
        int ln;
        while((ln = input.read(info)) != -1) 
            out.write(info, 0, ln);
    }

    private static Map parameters = new HashMap();
    private URL codeBase;
    private URL documentBase;

}

共 (0) 个答案