从另一个Java应用程序启动Java应用程序 [英] Launch a java application from another java application

查看:109
本文介绍了从另一个Java应用程序启动Java应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Java Record / Replay工具,我需要从我的主Java应用程序启动Java应用程序。
我需要访问EventDispatchThread以拦截事件并记录它们,所以我通过反射启动应用程序(简化代码片段):

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[] {} });

我以前动态加载所有jar到类路径,应用程序几乎完美启动。

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?

推荐答案

我建议在启动目标应用程序时将代码加载为Java代理。

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.)

这篇关于从另一个Java应用程序启动Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆