I'm trying to "inject" a jar using Agents, now the Java version are both 1.8, and the tools are from my JDK lib folder so I don't think anything is wrong with that either
This is my loading class
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);
}
And this is my 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());
}
}
now the stacktrace I get is:
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)
With Line47 being this line
vm.loadAgent(jarFile.getAbsolutePath());
AgentInitializationException: Agent JAR loaded but agent failed to initialize means that agentmain method has thrown an uncaught exception. Check the console or the logs of the target Java application (Minecraft) to see the cause.
Note: once you've loaded an agent into the target application, you won't be able to load the modified version of the same agent. This particularly means that if your agent has once failed with an exception, even after you has fixed the error and tried to load the correct class again, the agent will still fail with the same error.
In order to load the updated agent you'll have to rename the agent class and pack it to a different JAR file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With