I want to check within my Java application whether the windows virtual keyboard is already running or not.
I searched for it and found, that I can use wmic.exe
to search for a process.
This is what I'm doing:
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();
This is working, but wmic
is somehow creating the file TempWmicBatchFile.bat
with the line process where name='osk.exe' get caption
.
How can I prevent this from happening?
You can avoid opening other stream for passing another command. This is the reason why temp bat file is getting created.
Use the below code. It won't create temp batch file
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();
}
}