How to run a .jar file from inside another java program?

前端 未结 4 824
时光说笑
时光说笑 2021-01-22 04:44

i have a .jar file, which I can run on the command line:

java -jar myFile.jar argument1

I want to save the output of this .jar as a String vari

4条回答
  •  臣服心动
    2021-01-22 05:33

    If you can't include the other jar,

    you can use something like that

    Runtime re = Runtime.getRuntime();
    BufferedReader output;          
    try{ 
      cmd = re.exec("java -jar MyFile.jar" + argument); 
      output =  new BufferedReader(new InputStreamReader(cmd.getInputStream()));
    } catch (IOException ioe){
      ioe.printStackTrace();
    }
    String resultOutput = output.readLine();
    

    I know my code isn't perfect like the catching exception, etc but I think this could give you a good idea.

提交回复
热议问题