2012-03-02 7 views
0

新しいJFrameでminecraft.jarの中でMinecraftApplet.classを実行したい。新しいJFrameでアプレットを(JAR内で)実行する

私は私の既存のコードは動作しないので、これを行う方法に求めていますただし、:

これは、jarファイルのURLをロードStartScriptで、その後、クラスパスを更新し、最終的にはGameFrameでスレッドを開始し、追加しますcreateApplet

public class StartScript implements Runnable{ 

public ClassLoader classLoader; 
public boolean natives_loaded; 
protected URL[] urlList; 

public void run() 
{ 
    try 
    { 

     String path = (String)AccessController.doPrivileged(new PrivilegedExceptionAction() { 
      public Object run() throws Exception { 
       return InfiniLauncherUtils.getWorkingDirectory() + File.separator + "bin" + File.separator; 
     }}); 
     File dir = new File(path); 

     if (!dir.exists()) 
     { 
      dir.mkdirs(); 
     } 
     loadJarURLs(); 
     updateClassPath(dir); 

     Thread start = new Thread(new InfiniLauncherGameFrame(createApplet())); 
     start.start(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    }  
} 

public Applet createApplet() throws InstantiationException, IllegalAccessException, ClassNotFoundException 
{ 

    Class appletClass = classLoader.loadClass("net.minecraft.client.MinecraftApplet"); 
    return (Applet)appletClass.newInstance(); 

} 


protected void updateClassPath(File dir)throws Exception 
{ 
    URL[] urls = new URL[this.urlList.length]; 

    for (int i = 0; i < this.urlList.length; i++) { 
     urls[i] = new File(dir, getJarName(this.urlList[i])).toURI().toURL(); 
     System.out.println(urlList[i]); 
    } 

    if (classLoader == null) { 
     classLoader = new URLClassLoader(urls) { 
      protected PermissionCollection getPermissions(CodeSource codesource) 
      { 
       PermissionCollection perms = null; 
       try 
       { 
        Method method = SecureClassLoader.class.getDeclaredMethod("getPermissions", new Class[] { CodeSource.class }); 

        method.setAccessible(true); 
        perms = (PermissionCollection)method.invoke(getClass().getClassLoader(), new Object[] { 
         codesource }); 

        String host = "www.minecraft.net"; 

        if ((host != null) && (host.length() > 0)) 
        { 
         perms.add(new SocketPermission(host, "connect,accept")); 
        } else codesource.getLocation().getProtocol().equals("file"); 

        perms.add(new FilePermission("<<ALL FILES>>", "read")); 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
        return perms; 
       } 
     }; 
    } 
    String path = dir.getAbsolutePath(); 
    if (!path.endsWith(File.separator)) path = path + File.separator; 
    unloadNatives(path); 

    System.setProperty("org.lwjgl.librarypath", path + "natives"); 
    System.setProperty("net.java.games.input.librarypath", path + "natives"); 

    natives_loaded = true; 
} 

protected void loadJarURLs() throws Exception { 
    String jarList[] = {"lwjgl.jar", "jinput.jar", "lwjgl_util.jar", LoginScript.mainGameURL}; 

    //StringTokenizer jar = new StringTokenizer(jarList, ", "); 
    int jarCount = jarList.length+1; 

    this.urlList = new URL[jarCount]; 

    URL path = new URL("http://s3.amazonaws.com/MinecraftDownload/"); 

    for (int i = 0; i < jarCount - 1; i++) { 
     this.urlList[i] = new URL(path, jarList[i]); 
     System.out.println(urlList[i]); 
    } 

    String osName = System.getProperty("os.name"); 
    String nativeJar = null; 

    if (osName.startsWith("Win")) 
     nativeJar = "windows_natives.jar.lzma"; 
    else if (osName.startsWith("Linux")) 
     nativeJar = "linux_natives.jar.lzma"; 
    else if (osName.startsWith("Mac")) 
     nativeJar = "macosx_natives.jar.lzma"; 
    else if ((osName.startsWith("Solaris")) || (osName.startsWith("SunOS"))) 
     nativeJar = "solaris_natives.jar.lzma"; 
    else { 

    } 

    if (nativeJar == null) { 
    } else { 

     this.urlList[(jarCount - 1)] = new URL(path, nativeJar); 
    } 
    } 


protected String getJarName(URL url) 
{ 
    System.out.println(url); 
    String fileName = url.toString(); 

    if (fileName.contains("?")) 
    { 
     fileName = fileName.substring(0, fileName.indexOf("?")); 
    } 
    if (fileName.endsWith(".pack.lzma")) 
    { 
     fileName = fileName.replaceAll(".pack.lzma", ""); 
    } 
    else if (fileName.endsWith(".pack")) 
    { 
     fileName = fileName.replaceAll(".pack", ""); 
    } 
    else if (fileName.endsWith(".lzma")) 
    { 
     fileName = fileName.replaceAll(".lzma", ""); 
    } 

    return fileName.substring(fileName.lastIndexOf('/') + 1); 
    } 

private void unloadNatives(String nativePath) 
{ 
    if (!natives_loaded) { 
     return; 
    } 
    try 
    { 
     Field field = ClassLoader.class.getDeclaredField("loadedLibraryNames"); 
     field.setAccessible(true); 
     Vector libs = (Vector)field.get(getClass().getClassLoader()); 

     String path = new File(nativePath).getCanonicalPath(); 

     for (int i = 0; i < libs.size(); i++) { 
      String s = (String)libs.get(i); 

      if (s.startsWith(path)) { 
       libs.remove(i); 
       i--; 
      } 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

}

GameFrameクラス

public InfiniLauncherGameFrame(Applet aapplet) 
{ 
    applet = aapplet; 
    System.out.println("ran"); 
} 



public void run() 
{ 
    System.out.println("few"); 
    removeAll(); 
    add(applet); 
    validate(); 
    setVisible(true); 
    setSize(300, 300); 

} 

私はそれを実行したとき、私はすべてのエラーを得ることはありません、しかし、新しいフレームは、左上の小さな白いボックスと黒です。ここで

画像:http://imgur.com/rJjqC

私は例外のカップルを捨てたので、私はエラーを取得していない可能性があります。

ご迷惑をおかけして申し訳ありません。

+0

* "新しいJFrameでminecraft.jarの中でMinecraftApplet.classを実行したい" * Java Web Startを使用して起動する。新しいタイプの「埋め込み」JNLP /アプレットでない限り、フレーム内にあるアプレットビューアで自由に起動します。 BTW - これは、ミニクラフトEULAで許可されていますか? –

+0

私はこのページが見つかるまで数日間問題を抱えていました。http://www.leepoint.net/notes-java/deployment/applications_and_applets/70applets.htmlおそらくそれはあなたに役立つでしょう十分ではなかった。 –

答えて

0

愚かな提案かもしれませんが、明示的にrepaint()メソッドを呼び出していない可能性がありますか?それは私にはたくさん起こる

関連する問題