Results 1 to 6 of 6
Thread: My thread program plz help
- 11-10-2008, 03:36 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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();
}
}
}
- 11-10-2008, 04:12 PM #2
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.I am getting an error
- 11-10-2008, 04:37 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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
- 11-10-2008, 05:09 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
- 11-10-2008, 05:34 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
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
- 11-11-2008, 03:02 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's pleasure to help you.
By the way, please use code tags and format the code segments you have post here. Unformatted codes are hard to read.
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
where to add thread in the program
By amith in forum Java 2DReplies: 2Last Post: 07-04-2008, 12:12 AM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks