不 好意思,以下是错误提示:
[root@localhost os]# javac shell.java
shell.java:17: ';' expected
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:29: illegal start of expression
throws IOException{
^
shell.java:51: ')' expected
}catch(Interrupted Exception e){
^
shell.java:53: '{' expected
}
^
shell.java:57: '}' expected
}
^
shell.java:13: cannot resolve symbol
symbol : method Command (java.lang.String)
location: class shell.Command
Process p=r.exec(Command(CommandLine));
^
shell.java:15: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
^
shell.java:15: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
^
shell.java:15: cannot resolve symbol
symbol : variable proc
location: class shell.Command
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
^
shell.java:17: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:17: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:17: cannot resolve symbol
symbol : variable proc
location: class shell.Command
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:35: cannot resolve symbol
symbol : variable tokens
location: class shell
while (tokens.hasMoreTokens())
^
shell.java:37: cannot resolve symbol
symbol : variable tokens
location: class shell
str[count]=new String(tokens.nextToken());
^
shell.java:41: cannot resolve symbol
symbol : variable numberOfCommands
location: class shell
Thread t[] = new Thread[numberOfCommands];
^
shell.java:42: cannot resolve symbol
symbol : variable numberOfCommands
location: class shell
for (int i=0; i<numberOfCommands; i++) {
^
shell.java:44: non-static variable this cannot be referenced from a static context
t[i] = new Thread(new Command(str[i]));
^
shell.java:48: cannot resolve symbol
symbol : variable numberOfCommands
location: class shell
for (int i=0; i<numberOfCommands; i++) {
^
shell.java:51: cannot resolve symbol
symbol : class Interrupted
location: class shell
}catch(Interrupted Exception e){
^
19 errors
[root@localhost os]# javac shell.java
shell.java:17: ';' expected
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:29: illegal start of expression
throws IOException{
^
shell.java:51: ')' expected
}catch(Interrupted Exception e){
^
shell.java:53: '{' expected
}
^
shell.java:57: '}' expected
}
^
shell.java:13: cannot resolve symbol
symbol : method Command (java.lang.String)
location: class shell.Command
Process p=r.exec(Command(CommandLine));
^
shell.java:15: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
^
shell.java:15: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
^
shell.java:15: cannot resolve symbol
symbol : variable proc
location: class shell.Command
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
^
shell.java:17: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:17: cannot resolve symbol
symbol : class StreamGobbler
location: class shell.Command
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:17: cannot resolve symbol
symbol : variable proc
location: class shell.Command
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
^
shell.java:35: cannot resolve symbol
symbol : variable tokens
location: class shell
while (tokens.hasMoreTokens())
^
shell.java:37: cannot resolve symbol
symbol : variable tokens
location: class shell
str[count]=new String(tokens.nextToken());
^
shell.java:41: cannot resolve symbol
symbol : variable numberOfCommands
location: class shell
Thread t[] = new Thread[numberOfCommands];
^
shell.java:42: cannot resolve symbol
symbol : variable numberOfCommands
location: class shell
for (int i=0; i<numberOfCommands; i++) {
^
shell.java:44: non-static variable this cannot be referenced from a static context
t[i] = new Thread(new Command(str[i]));
^
shell.java:48: cannot resolve symbol
symbol : variable numberOfCommands
location: class shell
for (int i=0; i<numberOfCommands; i++) {
^
shell.java:51: cannot resolve symbol
symbol : class Interrupted
location: class shell
}catch(Interrupted Exception e){
^
19 errors
好好检查一下程序
我刚才看了一下
有忘了加;的 StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT")
;
还有e.printStack Trace();改为e.printStackTrace();
Interrupted Exception e 改为InterruptedException e 剩下的好像整个程序我建议你还是重写一下啊 呵呵
[root@localhost os]# javac shell.java
shell.java:29: illegal start of expression
throws IOException{
^
shell.java:13: cannot resolve symbol
symbol : method Command (java.lang.String)
location: class shell.Command
Process p=r.exec(Command(CommandLine));
^
shell.java:35: cannot resolve symbol
symbol : variable tokens
location: class shell
while (tokens.hasMoreTokens())
^
shell.java:37: cannot resolve symbol
symbol : variable tokens
location: class shell
str[count]=new String(tokens.nextToken());
^
shell.java:44: non-static variable this cannot be referenced from a static context
t[i] = new Thread(new Command(str[i]));
^
5 errors
而且有的错误......,建议还是从HelloWord开始吧:)给你弄出个编译过了的,要实现什么功能慢慢添吧。public class Shell
{
class Command implements Runnable{
String CommandLine;
Command(String CommandLine){
this.CommandLine=CommandLine;
}
public void run(){
try{
Runtime r=Runtime.getRuntime();
Process p=r.exec(CommandLine);
}catch(Exception ex){ex.printStackTrace();}
// any error message?
// StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR");
// any output?
// StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
//
}
}
public static void main(String arg[])
{
Shell a=new Shell();
a.test();
}
public void test(){
System.out.println("Command: ");
System.out.flush();
String str[];
str=new String[32];
int count=0;
int numberOfCommands=0;
String s="";
//throws IOException{
try{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
s=in.readLine();
}catch(Exception e){e.printStackTrace();}
// }
StringTokenizer tokens = new StringTokenizer(s, " &");
while (tokens.hasMoreTokens())
{
str[count]=new String(tokens.nextToken());
count++;
}
numberOfCommands=count-1;
Thread t[] = new Thread[numberOfCommands];
for (int i=0; i<numberOfCommands; i++) {
t[i] = new Thread(new Command(str[i]));
t[i].start();
}
try{
for (int i=0; i<numberOfCommands; i++) {
t[i].join();
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
try{
for (int i=0; i<numberOfCommands; i++) {
t[i].join();
}
}catch(Interrupted Exception e){
e.printStackTrace();
应该也只有join()方法会抛出错误吧?
//输入多个命令,每个命令由"&"号分隔,程序同时执行这些命令,执行完毕后提示输入信息.
//以下是小弟的代码:import java.util.*;
import java.io.*;public class TestShell
{
class Command
implements Runnable
{
String CommandLine;
Command(String CommandLine)
{
this.CommandLine = CommandLine;
} public void run()
{
try
{
System.out.println(CommandLine);
Runtime r = Runtime.getRuntime();
Process proc = r.exec(CommandLine.trim());
java.io.BufferedReader in
= new java.io.BufferedReader(new java.io.InputStreamReader( proc.getInputStream()));
String s=null;
while ( (s=in.readLine())!=null)
{
System.out.println(s);
}
in.close(); }catch(Exception ex)
{
ex.printStackTrace();
}
}
} public static void main(String arg[]) throws IOException
{
System.out.println("Command: ");
System.out.flush();
String str[];
str = new String[32];
int count = 0;
int numberOfCommands = 0; BufferedReader in = new BufferedReader(new InputStreamReader(System.
in));
String s;
s = in.readLine();
StringTokenizer tokens = new StringTokenizer(s, "&");
while (tokens.hasMoreTokens())
{
str[count] = new String(tokens.nextToken());
count++;
}
System.out.println(count);
numberOfCommands = count ;
Thread t[] = new Thread[numberOfCommands];
for (int i = 0; i < numberOfCommands; i++)
{
t[i] = new Thread(new TestShell().new Command(str[i]));
t[i].start();
}
}
}