问题描述
Java应用程序是否可以使用其名称(而不是其位置)以独立于平台的方式在单独的进程中加载?
Can a Java application be loaded in a separate process using its name, as opposed to its location, in a platform independent manner?
我知道您可以执行程序通过......
I know you can execute a program via ...
Process process = Runtime.getRuntime().exec( COMMAND );
...此方法的主要问题是此类调用是特定于平台的。
... the main issue of this method is that such calls are then platform specific.
理想情况下,我将方法包装成简单的方法...
Ideally, I'd wrap a method into something as simple as...
EXECUTE.application( CLASS_TO_BE_EXECUTED );
...并将应用程序类的完全限定名称传递为 CLASS_TO_BE_EXECUTED
。
... and pass in the fully qualified name of an application class as CLASS_TO_BE_EXECUTED
.
推荐答案
两个提示:
System.getProperty(java.home)+/ bin / java
为您提供java可执行文件的路径。
System.getProperty("java.home") + "/bin/java"
gives you a path to the java executable.
((URLClassLoader)Thread.currentThread()。getContextClassLoader())。getURL()
帮助您重建当前应用程序的类路径。
((URLClassLoader) Thread.currentThread().getContextClassLoader()).getURL()
helps you to reconstruct the classpath of current application.
然后你的 EXECUTE.application
只是(伪代码):
Then your EXECUTE.application
is just (pseudocode):
Process.exec(javaExecutable, - classpath,urls.join(:),CLASS_TO_BE_EXECUTED)
这篇关于在单独的进程中执行Java应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!