How to start Windows programs in Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.