4

Javaアプリケーション内で、Windows仮想キーボードがすでに実行されているかどうかを確認したいと思います。

wmic.exe私はそれを検索し、プロセスの検索に使用できることを発見しました。

これは私がしていることです:

Process proc = Runtime.getRuntime().exec("wmic.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(proc
    .getInputStream()));
OutputStreamWriter oStream = new OutputStreamWriter(proc
    .getOutputStream());
oStream .write("process where name='osk.exe' get caption");
oStream .flush();
oStream .close();
input.readLine();
while ((in = input.readLine()) != null) {
    if (in.contains("osk.exe")) {
        input.close();
        proc.destroy();
        return;
    }
}
input.close();
proc.destroy();

これは機能していますが、どういうわけか行を含むwmicファイルを作成しています。TempWmicBatchFile.batprocess where name='osk.exe' get caption

どうすればこれを防ぐことができますか?

4

1 に答える 1

2

別のコマンドを渡すために他のストリームを開かないようにすることができます。これが、tempbatファイルが作成される理由です。

以下のコードを使用してください。一時バッチファイルは作成されません

public class WmicTest {

    public static void main(String[] args) throws IOException {

        Process proc = Runtime.getRuntime().exec("wmic.exe process where name='osk.exe' get caption");
        BufferedReader input = new BufferedReader(new InputStreamReader(proc
                .getInputStream()));
//        OutputStreamWriter oStream = new OutputStreamWriter(proc
//                .getOutputStream());
//        oStream.write("process where name='osk.exe' get caption");
//        oStream.flush();
 //       oStream.close();
        input.readLine();
        String in;
        while ((in = input.readLine()) != null) {
            if (in.contains("osk.exe")) {
                System.out.println("Found");
                input.close();
                proc.destroy();
                return;
            }
        }
        input.close();
        proc.destroy();
    }
}
于 2013-02-12T17:17:01.067 に答える