Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run a java executable jar in another java program

Tags:

java

I know this question has been asked before but those answers didn't provide me an answer.

I want to execute a exec jar file in my java program and get the output from executing jar into a string. Here below are the codes I have used so far without success.

cmdlink = "java -jar iwtest-mac.jar"+" "+cmd;
            System.out.println(cmdlink);
             Process process = Runtime.getRuntime().exec(cmdlink);
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             while ((reader.readLine()) != null) {
                 st = reader.readLine();  

             }
             process.waitFor();

and another code I have tried is as follows:

String cmdlink = "iwtest-mac.jar "+cmd;    
          ProcessBuilder pb = new ProcessBuilder("java", "-jar", cmdlink); //cmd here is a string that contains inline arguments for jar.
            pb.redirectErrorStream(true);
            pb.directory(new File("C:\\Users\\Dharma"));

            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 ); 
            p.waitFor();

Both of the above are not working for me. Any suggestions are appreciated.

like image 293
Vardhan D G Avatar asked Mar 29 '13 09:03

Vardhan D G


People also ask

How can I run a JAR file from another Java program?

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).

How do I run a JAR file on another computer?

You need to install a Java Runtime Environment (JRE) first, then you can directly run the . jar files on your computer. The Java Development Kit (JDK) download package contains it's corresponding JRE, so that's fine to install too.

How do I run a JAR file in Java?

You may have to restart your computer for Java to be fully implemented. Double-click the JAR file. If it's executable and you have Java installed, it should open. If it doesn't open, proceed to the next step. You may see a pop-up window asking which program you want to use to open the file. If so, click Java (TM) and then click OK.

How to run an external program in Java?

How to Run External Program in Java 1 You want to invoke a Windows program such as Notepad 2 You want to invoke a command line utility such as Check disk (chkdsk) or IP information (ipconfig) 3 You want to access the Windows command interpreter and access some of its features such as "dir" command or "find" command. ...

How to compile&run Java program without external jar?

The following example shows how to compile and run Java program in command line mode with external jars. It is developed under Linux. 1. Compile & Run Java Program Without External Jar Let's create a simple hello world program "helloworld.java". public class helloworld { public static void main (String[] args){ System. out. println("Hello!"); } }

How to invoke an executable JAR file from the command line?

So, when invoking an executable JAR, we don't need to specify the main class name on the command line. We simply add our arguments after the JAR file name. If we do provide a class name after the executable JAR file name, it simply becomes the first argument to the actual main class. Most times, a JAR application is an executable JAR.


1 Answers

This works For Me..

public class JarRunner {
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\\JCcc.jar");
        pb.directory(new File("C:\\"));
        try {
            Process p = pb.start();
            LogStreamReader lsr = new LogStreamReader(p.getInputStream());
            Thread thread = new Thread(lsr, "LogStreamReader");
            thread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class LogStreamReader implements Runnable {

    private BufferedReader reader;

    public LogStreamReader(InputStream is) {
        this.reader = new BufferedReader(new InputStreamReader(is));
    }

    public void run() {
        try {
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This is what the Docs says-

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

You can pass any number of arguments in constructor.

Read more about process builder here.

like image 150
Mohammad Adil Avatar answered Sep 30 '22 19:09

Mohammad Adil