One solution that comes to my mind is to write a Java program that wraps the existing one.
Let's say the original program is distributed in a single .jar. Then, you would write a Java class, compiled with this jar as a dependency, run with this jar in the classpath, and containing a main
function that calls the main
function of the jar.
public class WrapMyApp {
public static void main(String[] args) {
com.myorg.MyApp.main(args);
}
}
So, using this technique, you are in control of the JVM, and can inject code before or after the call to main
.
What trick can you use to retrieve some infos from the GUI ? Well, if you are lucky, maybe you could try to access the GUI component through the reflexion API. Another solution is to traverse the GUI, starting from the JFrame
. This implies that the GUI layout is known in advance, and that it's static enough. Example :
JWindow w = (JWindow) Window.getWindows()[0];
JPanel p = (JPanel) w.getContentPane().getComponent(2);
JTextArea t = (JTextArea) p.getComponent(5);
String s = t.getText();
You can use tools like SwingExplorer to explore the GUI layout, in order to discover where your components are located.
If this doesn't work, you can try to hook to the EventQueue, listening to the key events :
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent event) {
// useful code here
}
}, AWTEvent.KEY_EVENT_MASK);
Don't forget to wrap your code inside a SwingUtilities.invokeLater()
, when trying to access GUI components.