在单独的进程中执行

在单独的进程中执行

本文介绍了在单独的进程中执行 Java 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:39