The Java Java.lang.Runtime class provides a way to run the Windows CMD Environment, exec (String command), and all programs executed under the Windows CMD Command window can be executed using this method.
The following two encapsulated code is provided below, one is to see if the program is running, and the other is to start the program:
1. Check if the program is running: where tasklist/fi "IMAGENAME eq processName.exe" is a command executed under Windows cmd command
/** * Determine if a process is running * @param processName * @return */public static Boolean isrunning (String processName) { BufferedReader bufferedreader = null; try {Process proc = runtime.getruntime (). EXEC ("tasklist/fi \" IMAGENAME eq "+ proce Ssname + "\" "); BufferedReader = new BufferedReader (New InputStreamReader (Proc.getinputstream ())); String line = null; while (line = Bufferedreader.readline ()) = null) {if (Line.contains (processName))//Determine existence {return true; }} return false; } catch (Exception ex) {ex.printstacktrace (); return false; } finally {if (BufferedReader! = null) {try { Bufferedreader.close (); } catch (Exception ex) {}}}}
2, the execution of a program, comand example: Start "" "E:\\cardlisten\\cardreaderserver.exe" "127.0.0.1" "8080", the details can see the cmd Start command
/** * Execute cmd command * @param command * @return * @throws ioexception */public static string executecmd (String command) throws IO Exception { Runtime runtime = Runtime.getruntime (); Process process = Runtime.exec ("cmd/c" + command); BufferedReader br = new BufferedReader (New InputStreamReader (Process.getinputstream (), "UTF-8")); String line = null; StringBuilder build = new StringBuilder (); while (line = Br.readline ())! = null) { build.append (line); } return build.tostring (); }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How to start Windows programs in Java