私はRuntime.getRuntime.exec("command");
を使ってコマンドを実行するJavaアプリケーションを持っています。Runtime.getRuntime()でコマンドを実行するとJavaアプリケーションがファイルを読み込まない
問題は、プログラムのコマンドを実行した後、指定された文字列を見つけることができないということですが、私はアプリを最初、さらに実行を実行した後、私はラインRuntime.getRuntime.exec("command");
をコメントアウトして、ファイルが作成された場合には、文字列を検索します正しく。何らかの理由で、の作業を中止しているようです(getRuntime()
)。誰もがこれに対する修正を知っていますか?
public static void main(String[] args) throws IOException{
String[] command = new String[3];
command[0] = "cmd.exe";
command[1] = "/c";
command[2] = "C:\\Users\\kjdah\\Desktop\\handle.exe -a > C:\\Users\\kjdah\\Desktop\\handles.txt";
Runtime.getRuntime().exec(command);
String toFind = "##?#USB#VID_04F2&PID_B2E1&MI_00#6&9f9977c&0&0000#";
File file = new File("C:\\Users\\kjdah\\Desktop\\handles.txt");
boolean found = false;
String strLinePid = null;
try {
FileReader fstream = new FileReader(file);
BufferedReader buffer = new BufferedReader(fstream);
String strLine;
while ((strLine = buffer.readLine()) != null) {
if(strLine.contains("pid:")){
strLinePid = strLine;
}
if(strLine.contains(toFind)){
found = true;
break;
}
}
buffer.close();
fstream.close();
}
catch (Exception e){
System.err.println("An error happened: " + e.getMessage());
e.printStackTrace();
}
`
これは速かった! Nice one mm759ちょうどプロセスを保存し、.waitfor()を追加しました。出来た。あなたは私の日を祝福した! ;) – Tofetopo