Pranay Chandale
Pranay Chandale

Reputation: 110

How to execute external application with arguments and receive result

How do i execute external application and pass the arguments and return the result from external application using java #ProcessBuilder and #RunTime ?

public class test {



public static void main(String[] args) {
    try {
        System.out.println("Starting Application");
    //    Runtime runtime =Runtime.getRuntime();

        Process proc= new ProcessBuilder("NconnectLicenseGenerator.exe","ABCDEFGHIJK").start();
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        System.out.printf("Output of the program is %s :" ,Arrays.toString(args));

Here I want to pass the Arguments to my App and submit the arguments through Java and return the results

        while((line=br.readLine())!=null)
        {
            System.out.println(line);
        }

        System.out.println("Closing Application");
    } catch (IOException e) {            
        e.printStackTrace();
    }      
}

}

Upvotes: 3

Views: 168

Answers (2)

Pranay Chandale
Pranay Chandale

Reputation: 110

I have solved this problem by making a console application in .net and using it with java process-builder.

Upvotes: 1

Alin Ungureanu
Alin Ungureanu

Reputation: 221

You can find here how to send command-line arguments to your Java app: https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

As for calling the external program in Java: Process proc = new ProcessBuilder("NconnectLicenseGenerator.exe", String.join(" ", args).start();

Upvotes: 3

Related Questions