Javaのプログラムを書いているので、WindowsやLinuxで実行できる必要があり、dirやlsなどのコマンドラインに要求を出す必要があります。次に、これらのコマンドから結果の出力を得る必要があり、AFTERWARDSは別のコマンドを求めるプロンプトを出します。ここに私のコードは次のとおりです。Javaのコマンドライン(マルチスレッド)
import java.io.*;
import java.util.*;
public class myShell
{
public static void main(String[] args)
{
Runtime runtime = Runtime.getRuntime();
Process process;
Scanner keyboard = new Scanner(System.in);
String userInput = "";
String osName = System.getProperty("os.name");
BufferedReader brStdOut = null;
String stdOut;
BufferedReader brErrorStream = null;
String errorStream;
while(userInput.compareToIgnoreCase("exit") != 0)
{
System.out.println();
System.out.print("??");
userInput = keyboard.nextLine();
int indexOfPound = userInput.indexOf('#');
if(indexOfPound == 0)
{
userInput = "";
}
else if(indexOfPound > 0)
{
userInput = userInput.substring(0, indexOfPound);
}
userInput = userInput.trim();
try
{
if(osName.contains("Windows"))
{
process = runtime.exec("cmd /c " + userInput);
}
else
{
process = runtime.exec(userInput);
}
brStdOut = new BufferedReader(new InputStreamReader(
process.getInputStream()));
brErrorStream = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
while((stdOut = brStdOut.readLine()) != null)
{
System.out.println(stdOut);
}
while((errorStream = brErrorStream.readLine()) != null)
{
System.err.println(errorStream);
}
process.waitFor();
}
catch(Exception e)
{
System.out.println("Error executing: " + userInput);
System.out.println(e.getMessage());
}
try
{
brStdOut.close();
}
catch(Exception e)
{
}
try
{
brErrorStream.close();
}
catch(Exception e)
{
}
}
System.exit(0);
}
}
私は現在、Windows 7マシンでのテストと行の#の後に何をコメントとしてみなされていることだと言及する必要があります。
これを実行すると、dirを使用しても結果は正常ですが、ls(Windowsでは認識されていないコマンドであることを示すメッセージが表示されます)を実行してみてください。次のプロンプトの後、または部分的にプロンプトの後に、プロンプトの後に部分的に表示されます。プロンプトの前にエラーメッセージの印刷を一貫して行う方法はありますか?ここで
は今何が起こっているかの例です:私は、日付などのコマンドを実行してみたときに
??dir
Volume in drive C is Windows7_OS
Volume Serial Number is SomeNumbers
Directory of C:\Users\BlankedOut\Documents\Java\Eclipse\Workspace\Prog1
02/05/2012 03:48 PM <DIR> .
02/05/2012 03:48 PM <DIR> ..
02/05/2012 03:48 PM 301 .classpath
02/05/2012 03:48 PM 387 .project
02/05/2012 03:48 PM <DIR> .settings
02/05/2012 08:39 PM <DIR> bin
02/05/2012 08:39 PM <DIR> src
2 File(s) 688 bytes
5 Dir(s) 861,635,362,816 bytes free
??ls
??'ls' is not recognized as an internal or external command,
operable program or batch file.
ls
'ls' is not recognized as an internal or external command,
operable program or batch file.
??exit
私の他の質問を考えています。 DOSウィンドウでは、これは2行の応答を返し、さらに多くの入力を待ちますが、Javaでは応答の最初の行だけを取得します(ただし、dirはすべての行を取得するようですが)ラインは、プログラムがちょうどハングする...誰がなぜこれが起こっているかもしれないか知っていますか?
ありがとうございます!そのため上記のコードブロックの
おそらくdirコマンドにエラーがないので、デバッグして何が起こっているのかを確認してください。 –
whileループの順序を単純に切り替えようとしました。その結果、私がdirを実行しようとすると、出力は生成されず、プログラムはただ停止してしまいました。これは、プロンプトがまだ出力ストリームを基準にしてどこでも印刷できるという事実を修正しませんでした。私は2つのブロックを組み合わせることが助けになると思っていましたが(次のポストはありません)、これはブロックの順序を切り替えるのと同じ問題を抱えていました... –
答えにスレッドを追加しました。 –