从其他JVM中的Java桌面应用程序中执行Java main方法 [英] Execute Java main method from within a Java desktop app in a different JVM

查看:147
本文介绍了从其他JVM中的Java桌面应用程序中执行Java main方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个桌面应用程序,当有人按下按钮时,我希望它启动另一个执行类的main方法的JVM.我的桌面应用程序已经依赖包含该类的jar以及要执行的main方法.

I have a desktop app and when someone presses a button I want it to kick off another JVM that executes a class' main method. My desktop app already depends on the jar that contains the class with the main method that I want to execute.

当前,我有以下代码,但是,我希望他们是一种更优雅的方式:

Currently I've got the following code, however, I was hoping their was a more elegant way of doing this:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java -jar another.jar");

我知道我也可以使用ProcessBuilder.

I know I can use ProcessBuilder too.

有没有办法(请使用伪代码)

Is there no way such as (excuse the pseudo code):

Jvm.execute(Main.class);

由于我要调用的Main类已经存在于我的类路径中,因此只能通过运行时运行java命令感到很奇怪.

Since the Main class that I want to call already exists in my classpath, it just feels weird to have to run the java command via Runtime.

推荐答案

很好的问题.尝试搜索管理API:

Very good question. Try to search into management API: http://cupi2.uniandes.edu.co/javadoc/j2se/1.5.0/docs/api/javax/management/package-frame.html

祝你好运.

我不确定该API是否存在,但是如果存在,则应该存在. 我个人使用过ProcessBuilder,但通过检索当前进程的系统属性来指定java的显式路径.

I am not sure that this API exists, but if it is it should be there. I'd personally used ProcessBuilder as you but specify explicit path to java by retrieving system properties of current process.

这篇关于从其他JVM中的Java桌面应用程序中执行Java main方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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