2017-07-08 759 views
1

我想用代理“注入”一个jar,现在Java版本都是1.8,并且这些工具来自我的JDK lib文件夹,所以我认为没有什么是错的与任何代理JAR加载但代理初始化失败

这是我的类加载

public static void main(final String[] args) throws Exception { 
    final File jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); 
    System.out.println("Starting Lizard..."); 
    try { 
     Class.forName("com.sun.tools.attach.VirtualMachine"); 
    } 
    catch (ClassNotFoundException e2) { 
     System.out.println("ERROR: Couldn't load VirtualMachine class, is tools.jar present?"); 
     return; 
    } 
    System.out.println("Loading attach library..."); 
    extractLibrary(jarFile); 
    try { 
     System.loadLibrary("attach"); 
    } 
    catch (Exception e3) { 
     System.out.println("ERROR: Couldn't load attach libary!"); 
     return; 
    } 

    System.out.println("Attach library loaded."); 
    System.out.println("Searching for Minecraft JVM..."); 
    for (final VirtualMachineDescriptor descriptor : VirtualMachine.list()) { 
     if (descriptor.displayName().startsWith("net.minecraft.launchwrapper.Launch")) { 
      System.out.println("Minecraft found, attaching..."); 
      System.out.println(descriptor.id()); 
      final VirtualMachine vm = VirtualMachine.attach(descriptor.id()); 
      final String vmJavaVersion = vm.getSystemProperties().getProperty("java.version"); 
      final String clientJavaVersion = System.getProperty("java.version"); 
      System.out.println("vmJava: " + vmJavaVersion); 
      System.out.println("ClientJava: " + clientJavaVersion); 
      if (!vmJavaVersion.equals(clientJavaVersion)) { 
       System.out.println("WARN: Lizard and Minecraft Java version do not match, agent might fail!"); 
      } 
      System.out.println("Loading agent..."); 
      try { 
       vm.loadAgent(jarFile.getAbsolutePath()); 
      } 
      catch (Exception e) { 
       System.out.println("ERROR: Agent failed to load (" + e.getMessage() + ")!"); 
       System.out.println("1: "+ e); 
       e.printStackTrace(); 
       return; 
      } 
      System.out.println("Agent successfully loaded, detaching..."); 
      vm.detach(); 
      System.out.println("Lizard started successfully."); 
      System.exit(0); 
      return; 
     } 
    } 
    System.out.println("Minecraft not found, exiting."); 
    JOptionPane.showMessageDialog(null, "No Minecraft JVM found.", "Lizard", 0); 
} 

这是我agentmain

public static void agentmain(String args, Instrumentation instrumentation) { 
    try { 
     @SuppressWarnings("rawtypes") 
     Class[] arrclass = instrumentation.getAllLoadedClasses(); 
     int n = arrclass.length; 
     int n2 = 0; 
     while (n2 < n) { 
      Class<?> clazz = arrclass[n2]; 
      if (clazz.getName().equals("net.minecraft.client.Minecraft")) { 
       LaunchClassLoader classLoader = (LaunchClassLoader)clazz.getClassLoader(); 
       classLoader.addURL(Agent.class.getProtectionDomain().getCodeSource().getLocation()); 
       Class<?> spookyNan = classLoader.loadClass(gorilla.Gorilla.class.getName()); 
       spookyNan.newInstance(); 
       return; 
      } 
      ++n2; 
     } 
    } 


    catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e.toString()); 
     JOptionPane.showMessageDialog(null, e.getStackTrace()); 
    } 
} 

现在我得到的堆栈跟踪是:

com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize 
    at sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:121) 
    at com.sun.tools.attach.VirtualMachine.loadAgent(VirtualMachine.java:540) 
    at gorilla.Main.main(Main.java:47) 

随着Line47是此行

vm.loadAgent(jarFile.getAbsolutePath()); 

回答

3

AgentInitializationException: Agent JAR loaded but agent failed to initialize意味着agentmain方法已抛出未捕获的异常。检查控制台或目标Java应用程序(Minecraft)的日志以查看原因。

注意:一旦将代理加载到目标应用程序中,您将无法加载相同代理的修改版本。这尤其意味着,如果您的代理程序曾经失败并发生异常,即使在您修复了错误并尝试再次加载正确的类后,代理程序仍会失败并显示相同的错误。

为了加载你必须重命名代理类它打包到不同 JAR文件更新的坐席。

+0

好吧,我会尝试或可行,但真的需要重新启动Java应用程序或者重新启动Java应用程序 –

+0

@jakeyancey重新启动Java应用程序是另一种选择。在这种情况下不需要所有这些更新。 – apangin