Results 1 to 4 of 4
  1. #1
    masdrobeda is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default can you give-me a little hand about java threads?

    I have done this so far, but I am getting an error on main class

    I believe its how to get the command (BufferedReader and StringTokenizer methods)

    Please give me a hint. Code Below:
    by the way, I have to create myshell with differents commands working in differents threads.


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

    public class MyShell
    {
    public static void main(String args[]) throws IOException
    {
    boolean exit = false;
    while (!exit)
    {

    // Modification of txt into String
    System.out.println("There is MyShell >>> ");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    // Devide whole string into different sub-strings(tokens)
    // and check if there is more tokens to read.
    String input = in.readLine();
    StringTokenizer token = new StringTokenizer(input, "&");

    while(token.hasMoreTokens())
    {
    String[] command = new String[3];
    command[0] = "cdm.exe";
    command[1] = "/C";
    command[2] = token.nextToken();

    if(command[2].equals("exit"))
    {
    System.exit(0);
    exit = true;
    }

    Runtime run = Runtime.getRuntime();
    Process proc = run.exec(command);
    thread_1 errorMessage = new thread_1(proc.getErrorStream(), "ERRORp");
    thread_1 outputMessage = new thread_1(proc.getInputStream(), "Output ");
    errorMessage.start();
    outputMessage.start();
    }
    }
    }
    }


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

    class thread_1 extends Thread
    {

    InputStream input;
    String str;

    thread_1(InputStream input, String str)
    {
    this.str = str;
    this.input = input;
    }

    // Override run method
    public void run()
    {
    try
    {
    InputStreamReader iReader = new InputStreamReader(input);
    BufferedReader bReader = new BufferedReader(iReader);
    String output = null;

    while ((output = bReader.readLine())!= null)
    System.out.println(str + ">>> " + output);

    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    See post in your other thread.
    One thread is enough!!!

  3. #3
    masdrobeda is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    sorry about that

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. how can i give a file path to java stream ?
    By sheckoo in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 08:42 AM
  2. Can someone give me a helping hand...
    By kwesiaryee in forum Advanced Java
    Replies: 1
    Last Post: 09-17-2008, 07:11 PM
  3. Really Would use a hand
    By mollentze in forum New To Java
    Replies: 7
    Last Post: 07-03-2008, 02:30 PM
  4. Reading a file into java and give it out
    By little_polarbear in forum New To Java
    Replies: 7
    Last Post: 06-05-2008, 10:32 AM
  5. Replies: 9
    Last Post: 03-25-2008, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •