-5

i want to read anything written in the terminal the Java class read it and store it in variable to use it in another method
can any one help me?

public class Termainl {
    public static  void  main (String args[] ) throws IOException{
           String[] cmdArray = {"gnome-terminal","java -classpath /home/r/byz/ Orchestrator"};

        try {
            Runtime.getRuntime().exec(cmdArray);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
medos
  • 7
  • 1
  • Welcome to stackoverflow. You have to show us some code of what you have tried and what does not work in it in order to get some help. – iCantSeeSharp Aug 09 '13 at 23:13
  • 2
    public class Termainl { public static void main (String args[] ) throws IOException{ String[] cmdArray = {"gnome-terminal","java -classpath /home/r/byz/ Orchestrator"}; try { Runtime.getRuntime().exec(cmdArray); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } – medos Aug 09 '13 at 23:15
  • 1
    i made this i can open the terminal window i want to read anything wrote on the terminal window in this class – medos Aug 09 '13 at 23:16
  • 1
    @medos you should add that information to your question, rather than in a comment. Click `edit`, and indent your code with four spaces, or surround one-liners with backticks (`). Note that you must add an empty line before indented code. – Michael Dorst Aug 09 '13 at 23:16
  • This will not work. Have a look at http://stackoverflow.com/questions/4644415/how-to-get-input-from-console-class-in-java – Daniel Schneller Aug 09 '13 at 23:20
  • i want to read from the ubuntu`s terminal not IDE console – medos Aug 09 '13 at 23:23
  • 1
    Your question is a little vauge. You want to run a java app from a terminal window, with the output going to the terminal window and the input coming from it? Or are you saying you want to run a program from within your java app, and capture the output of that program? – Edward Falk Aug 09 '13 at 23:47
  • You need to program in C/C++ extension plugin for your java because its impossible. What you need is stdio – SSpoke Aug 10 '13 at 00:47
  • @SSpoke You can read the output of a process started from Java without any extension! – C.Champagne Aug 11 '13 at 08:49

1 Answers1

0

I don't think you can easily get the output of the console like this but you can get the output of the process you launch. Why not directly launch the process and get its output.

You could try something like this:

try {
       Runtime.getRuntime().exec( "java -classpath /home/r/byz/ Orchestrator" );
       String line;
       BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); 
       while ((line = input.readLine()) != null) { 
              //<-- Parse data here. 
        }
        input.close();

 } catch (Exception err) {
    //......
 }
C.Champagne
  • 5,381
  • 2
  • 23
  • 35
  • i cant understand your code i want to get the result of specific process run on the terminal and get number on the terminal i want to read this number in my class and use it in another process is is possible or not – medos Aug 10 '13 at 16:07
  • @medos This code reads the output of your process. Each line of this output is stored in the `line` String variable so you can get that line and parse it if necessary. – C.Champagne Aug 11 '13 at 08:40