从另一个 Java 程序执行不同的 Jar 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6599290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:34:03  来源:igfitidea点击:

Executing a different Jar file from another java program

javajar

提问by Ryan

As a part of my program, I have a connections manager that receives a connection from a client, and then gives the client a port number and a password to use to connect. A this point the manager needs to call the jar file I have to handle this connections, with a few arguments, and continue on,( ignoring what the other program is doing).

作为程序的一部分,我有一个连接管理器,它接收来自客户端的连接,然后为客户端提供端口号和密码以用于连接。在这一点上,管理器需要调用 jar 文件,我必须使用一些参数来处理此连接,然后继续(忽略其他程序在做什么)。

My problem has been with executing the jar file. I've looked up similar question's and have tried using a process builder and using Runtime.exec. I moved around the jar file, and checked it's permissions. It just refuse to work from another java program, but works perfectly from the command line. Here's an example of one of my test runs.

我的问题是执行 jar 文件。我查过类似的问题,并尝试使用流程构建器和 Runtime.exec。我移动了 jar 文件,并检查了它的权限。它只是拒绝从另一个 java 程序工作,但从命令行完美地工作。这是我的一次测试运行的示例。

package test;

import java.io.*;

public class Main {
    public static void main (String [] args ) throws IOException, ClassNotFoundException, InterruptedException {
        Process p = Runtime.getRuntime().exec("java -jar \'/home/ryan/CytoscapeInterface.jar" +
        "\' arg1 arg2");
        //Process builder way
        /*ProcessBuilder pb = new ProcessBuilder("/home/ryan/CytoscapeInterface.jar",
           "-jar", "CytoscapeInterface.jar", "agr1", "arg2");
        pb.redirectErrorStream();
        Process p = pb.start();*/
        BufferedInputStream bis = new BufferedInputStream(p.getErrorStream());
        synchronized (p) { p.waitFor(); }
        System.out.println(p.exitValue());//its 1 for runtime, 2 for process Builder
        int read = bis.available();
        //had a loop, found out I just needed to go through once
        byte[] b = new byte [read];
        bis.read(b);
        read = bis.available();
        bis.close();
        FileOutputStream fos = new FileOutputStream (new File("/home/ryan/Desktop/FileTest.txt"));
        fos.write(b);//Writes error file
        fos.close();
    }
}

waitFor returns 1 for runtime and 2 for the builder. The error output for runtime is "Unable to access jarfile '/home/ryan/CytoscapeInterface.jar'. While using the builder gives a couple lines of error that had some weird characters, the first error was command not found.

waitFor 为运行时返回 1,为构建器返回 2。运行时的错误输出是“无法访问 jarfile '/home/ryan/CytoscapeInterface.jar'。虽然使用构建器给出了几行包含一些奇怪字符的错误,第一个错误是找不到命令。

回答by JSS

I have suceessfully tested the scenario and I can execute the jar file with in java programme (without setting the classpath).

我已经成功地测试了场景,我可以在 java 程序中执行 jar 文件(无需设置类路径)。

Can you make sure that you have added Manifest file in the jar which has Main-Class attribute.

您能否确保在具有 Main-Class 属性的 jar 中添加了 Manifest 文件

My steps and output:

我的步骤和输出:

  1. Created Mainfest file with the following line: Main-Class: com.test.TestJSSJar
  2. Created Test Java program:
  1. 使用以下行创建 Mainfest 文件:Main-Class: com.test.TestJSSJar
  2. 创建测试Java程序:

package com.test;

package com.test;

public class TestJSSJar extends Object {

    public static void main(String args[]) {
        System.out.println("Hi! I'm in the jar");
        System.out.println("Arg:" + args[0]);
    }
}
public class TestJSSJar extends Object {

    public static void main(String args[]) {
        System.out.println("Hi! I'm in the jar");
        System.out.println("Arg:" + args[0]);
    }
}

3.Package the jar (moved to temp folder): jar cvfm jss.jar manifest.txt com

4.Write test program:

3.打包jar(移动到temp文件夹):jar cvfm jss.jar manifest.txt com

4.编写测试程序:

import java.io.BufferedInputStream;
import java.io.IOException;

public class TestJSS extends Object {

    static int i = 0;

    public static void main(String args[]) throws IOException, InterruptedException {
        System.out.println("Calling jar");
        Process p = Runtime.getRuntime().exec("java -jar /temp/jss.jar arg1 arg2");
        BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
        synchronized (p) {
            p.waitFor();
        }
        System.out.println(p.exitValue());
        int b=0;
        while((b=bis.read()) >0){

            System.out.print((char)b);    
        }        
        System.out.println("");
        System.out.println("Called jar");
    }
}

5.Output

5.输出

Calling jar
0
Hi! I'm in the jar
Arg:arg1

Called jar

回答by Jeesmon

Include jar in the classpath and call the main method of the Main-class of the jar from your main.

在类路径中包含 jar 并从您的 main 调用 jar 的 Main-class 的 main 方法。

Suppose Main-class of CytoscapeInterface.jar is JarMain.class (You can look it up in META-INF of CytoscapeInterface.jar), then from your program, call it like this:

假设 CytoscapeInterface.jar 的 Main-class 是 JarMain.class(你可以在 CytoscapeInterface.jar 的 META-INF 中查找),那么在你的程序中,这样调用它:

JarMain.main(new String[]{"arg1", "arg2"});

You can also call it from a new Thread so that you can continue with your program.

您也可以从新线程调用它,以便您可以继续您的程序。

        new Thread(
        new Runnable() {
            public void run() {
                try {
                    JarMain.main(new String[]{"arg1", "arg2"});
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();