Prevent wmic.exe from writing TempWmicBatchFile.bat

I want to check in my Java application whether the Windows virtual keyboard is working or not.

I searched for it and found that I can use wmic.exe to search for the process.

This is what I do:

 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 works, but wmic somehow creates the file TempWmicBatchFile.bat with the line process where name='osk.exe' get caption .

How can I prevent this?

+4
source share
1 answer

You can avoid opening another thread to transmit another command. It is for this reason that the temp bat file is created.

Use the code below. It will not create a temporary 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(); } } 
+2
source

Source: https://habr.com/ru/post/1433838/


All Articles