aspose file tools*
The moose likes Beginning Java and the fly likes Runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime" Watch "Runtime" New topic
Author

Runtime

Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
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



Now this is the java program i am running



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
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
believe it or not I get the output!
Richard West
Ranch Hand

Joined: Jan 07, 2005
Posts: 127
Hi everyone,

Jean, i am listing a Runs class and a sample swing program so you can compile them and see what i mean first hand.

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



now this is the simple swing program is running



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
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
i can assure that i get the output either from the first Test class or the second JButtons one..

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test {
public static void main(String[] args) {


String str18 = "java JButtons";
String str20 = null;

Runtime Runtime1 = Runtime.getRuntime();

try {
Process Process1;
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();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

and
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();
}
}


and i called at prompt java test
output is...

You clicked Button 2
You clicked Button 1
You clicked Button 2
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
and if i commented the system.out in the first file the one that call Runs I don't have any output...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Runtime