Runtime
Hi everyone,
I am using the Runtime class to run a java program by using the java intreperter.
This is what i am doing to get the output stream of the java program
String str18 = //The string to run the java program with the java intreperter
String str20 = null;
Runtime Runtime1 = Runtime.getRuntime();
Process Process1 = Runtime1.exec(str18);
//This is the line where i am trying to get the output stream of the java
//program by getting its inputstream. I am right am i not or do i have
//to get its outputstream of the program instead
BufferedReader in = new BufferedReader(new InputStreamReader(Process1.getInputStream()));
while ((str20 = in.readLine()) != null)
{
System.out.println(str20);
}
Process1.waitFor();
Now this is the java program i am running
public class JTest
{
public static void main(String args[])
{
int i = 0;
for(i=0;i<5;i++)
{
System.out.println("This is ouput " + i);
}
}
}
The thing is the java program runs and everything is alright but i am not getting its console output by the way i am doing it.
I am right in that to get the output of a program, i get its inputstream and not outputstream right?
I am also using threads to run the program but that should have no effect right?
If what i am doing is wrong could someone guide me on the right way to get the output stream of a program
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
# 1 Re: Runtime
Be sure to put code in to read/echo the Error stream in case there's an error!
How do you know that the program is runnning?
Norm at 2007-11-11 22:40:35 >

# 2 Re: Runtime
Be sure to put code in to read/echo the Error stream in case there's an error!
I did that also but i got no errors for the program
How do you know that the program is runnning?
I tried running a different program which is a swing program and when the JButton is clicked the console is supposed to output "Button Clicked", The thing is the swing program shows up with a JButton in a JFrame so i know that the program is running but i am no getting its console output
Am i missing something Norm?
Richard West
# 3 Re: Runtime
Hi everyone,
I am going to post two runnable clasess here norm so you can compile them and see first hand what i mean. I tried running the above JTest program of mine with the below Runs class and it works but now here comes the weird part, it does not work with a swing application as i will list.
You see the runtime in my program does not seem to be able to get any output streams from the swing program
This is what i am doing to run the swing application
public class Runs
{
public void runswing(String Str18)
//Str18 is the string to run the java program with the java intreperter
String str20 = null;
String str21 = null;
Runtime Runtime1 = Runtime.getRuntime();
Process Process1 = Runtime1.exec(str18);
//This is the line where i am trying to get the output stream of the java
//program by getting its inputstream. I am right am i not or do i have
//to get its outputstream of the program instead
BufferedReader in1 = new BufferedReader(new InputStreamReader(Process1.getInputStream()));
while ((str20 = in1.readLine()) != null)
{
System.out.println(str20);
}
BufferedReader in2 = new BufferedReader(new InputStreamReader(Process1.geterrorStream()));
while ((str21 = in2.readLine()) != null)
{
System.out.println(str21);
}
Process1.waitFor();
}
public static void main(String args[])
{
Runs a = new Runs();
a.runswing(//The string to run the java program with the java intreperter);
}
}
now this is the simple swing program is running
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JButtons implements ActionListener
{
JFrame fr = new JFrame ("Frame");
JLabel Label1 = new JLabel("Label1 ",SwingConstants.RIGHT);
JButton Button1 = new JButton("Button 1");
JButton Button2 = new JButton("Button 2");
public void initialize ()
{
Container pane = fr.getContentPane();
pane.setLayout(new FlowLayout());
fr.setSize(250,300);
fr.setLocation(300,300);
fr.setBackground(Color.lightGray);
pane.add(Label1);
pane.add(Button1);
pane.add(Button2);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button1.addActionListener(this);
Button2.addActionListener(this);
fr.pack();
fr.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
JComponent b = (JComponent)event.getSource();
if(b == Button1)
{
Label1.setText("Button 1 clicked");
System.out.println("You clicked Button 1");
}
else if(b == Button2)
{
Label1.setText("Button 2 clicked");
System.out.println("You clicked Button 2");
}
}
public static void main(String args[])
{
JButtons a = new JButtons();
a.initialize();
}
}
Now the problem is that when i run the above swing program using my Runs class(listed above) and click on their JButtons the console is supposed to output the corresponding JButton that is clicked.
Now i am still not able to read the console output. I have a feeling that the problem has to do with the while loops in the Runs class i have above but i am not sure and i can't prove it.
Am i missing something or am i doing something wrongly?
I hope someone can guide me how to do this correctly
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
# 4 Re: Runtime
Sorry, I'll let you do the debugging.
Here's some code I use:
try {
proc = Runtime.getRuntime().exec(prog);
// Test if we're to capture and display the results:
if (showRes != null || Testing) {
/* THIS CODE HANGS??
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
System.out.println(">>> Start of Copying STDOUT");
// Read and display the output ?? HANGS??
while((str = br.readLine()) != null) {
if (Testing)
System.out.println(str);
resOut.append(str + "\n");
}
br.close();
System.out.println(">>> End of Copying STDOUT");
*/
DataInputStream dis = new DataInputStream(new BufferedInputStream(
proc.getInputStream()));
// StringBuffer stdOut = new StringBuffer();
byte[] stdOut = new byte[800];
int cnt = 0;
int lpCnt = 0;
while(true && cnt < stdOut.length) {
if (lpCnt++ > stdOut.length) break; // EXIT for sure??
try {
int c = dis.readByte();
stdOut[cnt++] = (byte)c;
// System.out.println((byte)c + " " + new String(stdOut, 0, cnt));
}catch(EOFException eof) {
// System.out.println("Got EOF ex");
break;
}catch(Exception ex) {
ex.printStackTrace();
break;
}
} // end while()
dis.close();
if (Testing)
System.out.println("Read " + cnt + " bytes and looped " + lpCnt);
String rslts = new String(stdOut, 0, cnt);
if (showRes != null)
resOut.append(rslts);
// Check for errors
// Note: This is message from XCopy??
if (rslts.indexOf(GoodCopyText) < 0) {
System.out.println("Bad copy? " + rslts + " " + rslts.indexOf(GoodCopyText));
hadError = true; // Remember, we'll exit after ErrorStream is output
}
//*/
/* BufferedReader bre = new BufferedReader(
new InputStreamReader(proc.getErrorStream()));
// System.out.println(">>> Start of Copying ERROUT");
// Read and display the error messages ?? HANGS ??
while((str = bre.readLine()) != null) {
if (Testing)
System.out.println("ERR: " + str);
if (showRes != null)
resOut.append(str + "\n");
}
bre.close(); // */
dis = new DataInputStream(new BufferedInputStream(
proc.getErrorStream()));
// StringBuffer stdOut = new StringBuffer();
cnt = 0; // Reset count
while(true && cnt < stdOut.length) {
if (lpCnt++ > stdOut.length) break; // EXIT for sure??
try {
int c = dis.readByte();
stdOut[cnt++] = (byte)c;
// System.out.println((byte)c + " " + new String(stdOut, 0, cnt));
}catch(EOFException eof) {
// System.out.println("Got EOF ex");
break;
}catch(Exception ex) {
ex.printStackTrace();
break;
}
} // end while()
dis.close();
rslts = new String(stdOut, 0, cnt);
Norm at 2007-11-11 22:43:38 >

