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

问题描述

如何从独立的java程序中调用Java命令。

How do i call a Java command from a stand alone java program.

我理解Runtime.getRuntime()。exec(cmd c / javac< >的.java);会工作。但是,这将是特定于平台的。

I understand that Runtime.getRuntime().exec("cmd c/ javac <>.java"); would work. However, this would be platform specific.

任何其他可用的API可以使它在j2sdk1.4中运行?

Any other APIs available that could make it work in j2sdk1.4 ?

推荐答案

如果你可以在同一个JVM中运行所有东西,你可以这样做:

If you can run everything in the same JVM, you could do something like this:

public class Launcher {
    ...
    public static void main(String[] args) throws Exception {
        launch(Class.forName(args[0]), programArgs(args, 1));
    }

    protected static void launch(Class program, String[] args) throws Exception {
        Method main = program.getMethod("main", new Class[]{String[].class});
        main.invoke(null, new Object[]{args});
    }

    protected static String[] programArgs(String[] sourceArgs, int n) {
        String[] destArgs = new String[sourceArgs.length - n];
        System.arraycopy(sourceArgs, n, destArgs, 0, destArgs.length);
        return destArgs;
    }

并使用如下命令行运行它:

And run it with a command line like this:

java Launcher OtherClassWithMainMethod %CMD_LINE_ARGS%

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

07-30 20:20