Код: Выделить всё
import com.sun.tools.attach.VirtualMachine;
public class AttachAgent {
public static void main(String[] args) throws Exception {
// Replace with real PID of the running Swing app
String pid = "16740";
// Replace with the full path to your built SwingAgent JAR
String agentPath = "C:\\Temp\\SwingHacker.jar";
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(agentPath);
vm.detach();
System.out.println("Agent attached!");
}
}
Код: Выделить всё
SwingHacker.jar
├─ META-INF
│ ├─ MANIFEST.MF
├─ SwingAgent.class
< /code>
Это manifest.mf: < /p>
Manifest-Version: 1.0
Agent-Class: SwingAgent
Can-Redefine-Classes: true
Permissions: all-permissions
Код: Выделить всё
import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.lang.instrument.Instrumentation;
import javax.swing.JButton;
public class SwingAgent {
public static void agentmain(String agentArgs, Instrumentation inst) {
System.out.println("Agent attached!");
try {
Thread.sleep(2000L);
} catch (Exception var6) {
}
for(Window window : Window.getWindows()) {
clickButtonByText(window, "Login");
}
}
private static boolean clickButtonByText(Component comp, String text) {
if (comp instanceof JButton && text.equals(((JButton)comp).getText())) {
System.out.println("Clicking button: " + text);
((JButton)comp).doClick();
return true;
} else {
if (comp instanceof Container) {
for(Component child : ((Container)comp).getComponents()) {
if (clickButtonByText(child, text)) {
return true;
}
}
}
return false;
}
}
}
< /code>
Когда я запускаю это, я получаю эту ошибку: < /p>
Exception in thread "main" com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize
at jdk.attach/sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:169)
at jdk.attach/com.sun.tools.attach.VirtualMachine.loadAgent(VirtualMachine.java:538)
at AttachAgent.main(AttachAgent.java:13)
Подробнее здесь: https://stackoverflow.com/questions/795 ... iled-to-in