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

时间:2011-07-06 15:47:06

标签: java jar

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

我的问题是执行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为运行时返回1,为构建器返回2。运行时的错误输出是“无法访问jarfile”/home/ryan/CytoscapeInterface.jar'。使用构建器时会出现几行错误,其中包含一些奇怪的字符,第一个错误是找不到命令。

2 个答案:

答案 0 :(得分:5)

我已经完成了对场景的测试,我可以在java程序中执行jar文件(不设置类路径)。

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

我的步骤和输出:

  1. 使用以下行创建Mainfest文件: Main-Class:com.test.TestJSSJar
  2. 创建测试Java程序:
  3. 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.包装罐子(移动到临时文件夹): jar cvfm jss.jar manifest.txt com

    4.写测试程序:

    5,输出

    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");
        }
    }
    

答案 1 :(得分:0)

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

假设CytoscapeInterface.jar的Main类是JarMain.class(您可以在CytoscapeInterface.jar的META-INF中查找),然后从您的程序中调用它:

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

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

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