• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass Value to Running Program

 
sohrab khan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I developed a C++ application long time ago, which is Command Line. When started it asks the username, and I've to enter the username, then it asks the password. Afterwards I have about 2 dozen commands I can fire.

Now I m making a GUI for the application in Java. I can run the application using Runtime.exec() method, and display its output to the user using the Process.getInputStream().

But the problem is that I cannot give it inputs like feed-in the username and passwords and the application's own commands (eg 1 of the command is help which displays all the commands).

Important parts of the Java Application are:

rt = Runtime.getRuntime();
Process p = rt.exec("myapplication");
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

while((s = stdInput.readLine()) != null){
System.out.println(s);
}

By doing the above the application asks for the username, Now how do I get the username from a textbox and give it to the C++ application @ runtime in other words how do I pass values to a running program?

Any help will be appreciated
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p.getOutputStream() is your way to go.

Wrap it in a PrintStream, then println() your username and password.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that you should also read from the process's standard and error output. Those streams are buffered, and if the buffer fills up, the process will halt. Here's a good article on the pitfalls of Runtime.exec().
 
sohrab khan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanku v.much. It worked.

Now i've got another problem. I can't figure out how 2 write the while loop. The following is what I did:
-------------Declarations---------------------
private BufferedReader input;
private PrintStream output;
private String cmd;
private Process p;
private Runtime rt;
---------------------------------------------------
public void executeCommand() throws IOException, InterruptedException {
cmd = "cmd";
rt = Runtime.getRuntime();
p = rt.exec(cmd);

input = new BufferedReader(new InputStreamReader(p.getInputStream()));
output = new PrintStream(p.getOutputStream(),true);

//problem area. This loop doesn't end
while(input.readLine()!= null){
System.out.println(input.readLine());
}


Thread.currentThread.sleep(5000);
cmd = "dir";

output.println(cmd);

//problem area
while(input.readLine() != null){
System.out.println(input.readLine());
}

}
-------------------------------------------------------

The problem is that input.readLine() never returns null, neither would input.read() return -1. So the while loops don't end.

Seems like the readLine() & read() blocks until the "cmd" system utility ends. which never happens.

Can any1 tell me what condition 2 use in the while loop?

ThankU.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll see from that article that their StreamGobblers read on independent threads. This is definitely a bit tricky, but necessary to handle both stdout and errout.

If you need to know when you've read all the output from one command so you can chunk it up and use it, I think you'll have to learn to tell the end by looking at the output. Maybe a command output puts out a blank line or ends with a period or something. This will probably not be at all easy, since command output rarely follows a rigid syntax.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!