Run Java program into another Program [duplicate]

2019-03-04 06:58发布

问题:

Possible Duplicate:
Execute another jar in a java program

I try to run a jar file of with a java application from within another java application, but I cannot get it to work. My code is below

// run Jar in other java application
Runtime run=Runtime.getRuntime();           
run.exec("java -jar ManichemManagerRotas BatchProcess 8 2012");

I try to run manichemmanagerrotas.jar with three arguments.

all method i can do it but it cannot work still problem is cannot run jar file's main method class if you have any idea of this problem,plz give me replay me it's important task for me

i try to run jar file then error is that :Unable to access jarfile ManichemManagerRotas

回答1:

Try to add missing .jar:

Runtime run=Runtime.getRuntime();            
run.exec("java -jar ManichemManagerRotas.jar BatchProcess 8 2012");

... and you have to consume the process output... Like this:

InputStream in = run.getInputStream();
InputStream err = run.getErrorStream();


回答2:

If you want to invoke the java class/functions from another class means, make it as jar and add the jar into your projects build path. In another class you create object of the Jared class and invoke the function. (make sure your class and function is public access)



回答3:

May be you forgot to type .jar

run.exec("java -jar ManichemManagerRotas.jar BatchProcess 8 2012"); 


回答4:

The Runtime.exec(...) methods returns a java.lang.Process instance. Just call its getInputStream() method.

Process p = run.exec("java -jar ManichemManagerRotas.jar BatchProcess 8 2012");
InputStream is = p.getInputStream();


回答5:

If you want just write output to some file without modifying it you can just add:

> somefile.txt

at the end of the 'command':

run.exec("java -jar ManichemManagerRotas.jar BatchProcess 8 2012 > somefile.txt");