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

    Default My thread program plz help

    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:


    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 online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,617
    Rep Power
    23

    Default

    I am getting an error
    Please post the full text of the message here. Not too many of have compilers in our head. You need to help us by giving us the output from YOUR COMPILER.

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

    Default

    when I type the command "dir"
    I get this: (using NetBeans)

    Exception in thread "main" java.io.IOException: Cannot run program "cdm.exe": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :459)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:466)
    at MyShell.main(MyShell.java:38)
    Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java :452)
    ... 3 more
    Java Result: 1

  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
    19

    Default

    Can you explain more clearly what you are trying to do here.

    At the minute, I think you are going to run the command prompt instance. So the exe should be cmd.exe not cdm.exe

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

    Default

    ops
    That is it.
    Eranga you right, now it is working.
    I can not belive it hehehhe

    So I guess I am all set, programming is running the way I want.

    By the way, on this program, call my shell, create a shell(UNIX) and for each command there is a thread working for it. However, you can type more then one command per line.
    Thank you

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

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. where to add thread in the program
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-04-2008, 12:12 AM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Posting Permissions

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