Dhaval
Dhaval

Reputation: 43

Can i create java GUI application which can run other java programs in it?

I am trying to create a java swing app in which I want to use two text boxes one for input and other for output procedure. When i click the button I want to run the code in input text box and the output should be in output text box.

I try with the process builder. I open cmd using process builder and run program in it.it works fine if the program has only printing lines.But if the program ask for input it is not working.

Which we can do in eclipse IDE in which the program is running in console window and we can give input at that time.

Then I convert the input program to jar file and try to run it code:

 String demo = "javaw -jar D:\\x.jar";             
Process proc = Runtime.getRuntime().exec(demo);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String f;

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

proc.waitFor();
br.close();

Or there is another way without using cmd plz tell me. How can I write appropriate code for it ?

Upvotes: 0

Views: 932

Answers (1)

John
John

Reputation: 5287

Yeah, there is another way.

Not entirely certain what you are trying to achieve and why, should the second process parse input and make result available for the first one?

You could also have two different processes which communicate with sockets, if you don't need this parent child process relationship within same application.

You can avoid (and should) Runtime - read more about this:

http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

The alternative way:

// for java you need to pass: "java.exe","-cp","bin","package.class")

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

You can pass values with environment variables, they will be accessible by name to child process.

Map<String, String> environementVariable = processBuilder.environment();
environementVariable.put("parameters", "value");

You can access them like this:

Map<String, String> env = System.getenv();

Start the process:

Process process = processBuilder.start();

Upvotes: 2

Related Questions