Saobi
Saobi

Reputation: 17021

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

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 variable inside another java program.

How can I do it?

I tried including myFile.jar as a reference in my program, and doing myFile.main(new String{"argument1"}) in my program. But this just prints the results to console, I can't use the results in my program.

Hope this is not too confusing.

Upvotes: 3

Views: 13130

Answers (4)

Nettogrof
Nettogrof

Reputation: 2136

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.

Upvotes: 0

Jonathan Holloway
Jonathan Holloway

Reputation: 63662

Take a look at ProcessBuilder:

http://java.sun.com/javase/6/docs/api/java/lang/ProcessBuilder.html

It effectively creates an operating system process which you can then capture the output from using:

process.getInputStream().

The line:

processbuilder.redirectErrorStream(true)

will merge the output stream and the error stream in the following example:

e.g.

public class ProcessBuilderExample {

    public ProcessBuilderExample() {
        // TODO Auto-generated constructor stub
    }
    public static void main(String[] args) throws IOException, InterruptedException {

        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "gscale.jar");
        pb.redirectErrorStream(true);
        pb.directory(new File("F:\\Documents and Settings\\Administrator\\Desktop"));

        System.out.println("Directory: " + pb.directory().getAbsolutePath());
        Process p = pb.start();
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        for (String line = br.readLine(); line != null; line = br.readLine()) {
                System.out.println( line ); // Or just ignore it
        }
        p.waitFor();                    
    }
}

Upvotes: 1

NawaMan
NawaMan

Reputation: 25687

I

Running Jar file require you to have the jar file included in your class path. This can be done at run time using URLClassLoader. Simply construct a URLClassLoader with the jar as one of the URL. Then call its forClass(...) if you know the class name (full name of course). Or inspect the manifest file using its 'findResources(String name)'.

Once you get the class, you can use reflection to get its static method main.

Seeing your question again, you know the class name, so if you are sure the jar file in already in the class path, then you can just call it as you tried.

II

To capture the output, you can call System.setOut(PrintStream out) and System.setErrPrintStream out) to change the print stream. You can pass the printstream that you create. Like this:

ByteArrayOutputStream BAOS = new ByteArrayOutputStream();
PrintStream MyOut = new PrintStream(BAOS);
System.setOut(MyOut);

// Do something to have something printed out.
...

String TheCaptured = new String(BAOS.toByteArray());

Hope this helps.

Upvotes: 1

monksy
monksy

Reputation: 14234

Being a Jar file, you can add it to your class path and call the functionality of the program your self. You might need to know more about the logic behind the Jar to use it without having it output the information..

I believe what you are looking for is how to shell execute the Jar archive. An example can be found here.

Upvotes: 0

Related Questions