如何从另一个Java应用程序打开Java桌面工具

时间:2014-06-11 06:09:08

标签: java platform

我使用nebeans平台模块创建了一个Java桌面工具。 现在我们正在创建一个通用工具,从单击按钮开始我们应该可以启动现有工具。

想象一下,我们有一个JFrame,其中有两个按钮。 一个是tool1_btn 第二是tool2_btn

现在当我点击tool1_btn工具时,会弹出一个。

如果我写喜欢

try {
    String line;

    Process p = Runtime.getRuntime().exec("notepad.exe");

    /* java -classpath C:\\projects\\workspace\\testing\\bin test.TestOutput"
     * Create a buffered reader from the Process input stream.
     */
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    /**
     * Loop through the input stream to print the program output into console.
     */
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    /**
     * Finally close the reader
     */
    input.close();
} catch (Exception e) {
    e.printStackTrace();
}

会弹出notepad.exe ..

但是如果我们为现有的工具jar提供Java类路径,它只会在后端运行它。

我想打开工具,好像它被双击并打开..这有可能......请发给我..

如果我的问题不明确,请告知我..

2 个答案:

答案 0 :(得分:1)

您应该首先使用ProcessBuilder而不是普通Process。除了其他一切,它允许您重定向错误流(到输出流和其他地方),以及指定程序的起始上下文(程序将在哪个目录中启动)

像...一样的东西。

try {
    String line;

    ProcessBuilder pb = new ProcessBuilder(
            "java",
            "-cp",
            "C:\\projects\\workspace\\testing\\bin",
            "test.TestOutput"
    );
    pb.redirectError();

    Process p = pb.start();

    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    /**
     * Loop through the input stream to print the program output into console.
     */
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }
    /**
     * Finally close the reader
     */
    input.close();
} catch (Exception e) {
    e.printStackTrace();
}

例如......

答案 1 :(得分:0)

我是这样做的: File exe = new File(...); Runtime.getRuntime()。exec(“rundll32 url.dll,FileProtocolHandler”+ exe.getAbsolutePath());