Runtime

F

freesoft_2000

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
 
R

Roedy Green

String str18 = //The string to run the java program with the java
intreperter
String str20 = null;

For us to help, you have to take off your clothes and let us look at
the REAL code.

I have a suspicion the problem is you think that exec is an eval
function that accepts Java source, but I can't tell until you reveal
the true code you are having trouble with.

In the meantime, have a look at http://mindprod.com/jgloss/exec.html
which covers most of the common problems people have with exec.

If you are looking for an eval function, see
http://mindprod.com/jgloss/eval.html

Further, try naming your variables so that their intended function is
clearer. We need all the clues we can get what you are trying to do.
Numbered variable names are for obfuscation.
 
F

freesoft_2000

Hi everyone,

Roedy i am going to post two runnable classes here
so you can compile them and see what i mean first hand. I tried running
the JTest program in my first post with the below Runs class program 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

Roedy the links you listed for me were very helpful but they seem to have
the same code i have and that's why i am finding this weird.

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
 
R

Roedy Green

public void runswing(String Str18)
//Str18 is the string to run the java program with the java intreperter
This code does not compile. You are missing a { after your method.
 
R

Roedy Green

a.runswing(//The string to run the java program with the java
intreperter);

this code does not compile. The comment chops off the );

You need to write this as:

a.runswing(/* The string to run the java program with the java
interpreter */);

But you can't do that since runswing demands a parameter. You need an
ACTUAL string in there, not a comment, even just to compile.

See http://mindprod.com/jgloss/exec.html
for what that string might look like.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top