Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch a java application from another java application

I'm doing a Java Record/Replay tool and I need to launch Java applications from my main Java app. I need access to the EventDispatchThread in order to intercept the events and record them, so I'm launching the application through reflection with (code snippet simplified):

Class<?> app = Class.forName(mainClass);
Method m = app.getMethod("main", new Class[] { String[].class }); 
m.invoke(null, new Object[] { new String[] {} });

I previously dynamically load all the jars to the classpath and the application is launching almost perfectly.

The problem occurs when the application needs to access any file and does it with relative paths. Because the application is launched through my application the path is not the same as launched from its normal path and the files are not found.

What can I do to solve this problem? dynamically change the execution environment? any ideas?

like image 470
jpsstavares Avatar asked Nov 06 '22 15:11

jpsstavares


1 Answers

I would suggest loading your code as a "Java Agent" whilst starting the target application.

(With your method you will also find that the system class loader is wrong.)

like image 55
Tom Hawtin - tackline Avatar answered Nov 12 '22 16:11

Tom Hawtin - tackline