Java open multi-process, execute. exe file

Source: Internet
Author: User

1.. exe file generation

Using your own generated application, the application has Parameters. For the sake of simplicity, take the input parameters as an example, the Application's source code is as Follows:

Myprint.cpp#include < iostream >int main (int argc, char * Argv[]) {char * it = argv[1];if (it = = Nullptr) {std::cou T << "this was nothing to be printed." << std::endl; Else{while (*it! = ') {std::cout << *it++;}}  Char ch;std:: cin >> ch; System ("pause"); return 0;}

The function builds the MyPrint.exe application after the connection has been Compiled.


2. java executes. exe, Adding a process for each execution



Responsible for opening and closing applications ***********************

Maintains a string class, with the open Application Process class mapping table

1. Depending on the sequence number generation process, the String class can be converted to the desired Index object in other cases, as long as one by one corresponds to the application being run

2. Depending on the ordinal, finding the current transport process can close it

2.1 Close an application, because only the process name is provided, so first obtain the PID according to the process name,

2.2 In accordance with the PID kill the application

3. Reference source:

3.1 2.1 and 2.2 reference http://blog.sina.com.cn/s/blog_999cc24a0102vpjr.html

3.2 Introduction of the location of the package https://java.net/projects/jna/downloads/directory/3.3.0

3.3 Download two jar packages at 3.1 given address, create a new Lib folder under the SRC directory of the project, put the jar file inside, \src\lib

In Eclispe Select the current project, right-click, build path--> Config--and libraries to add the above two. jar

--icesongqiang




import java.io.ioexception;import java.lang.reflect.field;import java.util.hashmap;import  java.util.map;import com.sun.jna.pointer;import com.sun.jna.platform.win32.kernel32;import  com.sun.jna.platform.win32.winnt;class procmanasrc{//  establishes a string-to-proc mapping, indexing switch process based on numbers private  Static map m_string_proc= new hashmap ()  ;p rivate procmanasrc () {};p ublic  Static void startprocess (string str) {//query First whether String m_index = str;if has been run (!m_ String_proc.containskey (m_index)) { //does not exist to create try {// string cmd =  "cmd /c  start .\\myprint\\myprint.exe " +" \ ""  + str + "\" " ; string cmd  =  ". \\myprint\\myprint.exe"  + "\" " + str +" \ ""  ; //  To add quotation marks System.out.print (cmd); System.out.println (); Runtime rt = runtime.getruntime (); Process proc = rt.exec (cmd); &NBSP;M_STRING_PROc.put (str, proc);  } catch  (ioexception e)  {        e.printstacktrace ();   }}}public static void closeprocess (String str) {string m_index = str;if (m_string_proc.containskey (m_index)) { //exists before deleting Killprocesstree (m_ String_proc.get (m_index)); M_string_proc.remove (m_index);}}
Private static void killprocesstree (process process) {         try {                field f = process.getclass (). Getdeclaredfield ("handle");                f.setaccessible (true);                long handl = f.getlong ( process);                kernel32  kernel = Kernel32.INSTANCE;                winnt.handle handle = new winnt.handle ();                handle.setpointer (Pointer.createConstant ( handl));     &nbsP;          int ret = kernel. GetProcessID (handle);                long pid = long.valueof (ret);                string cmd = getkillprocesstreecmd (PID);                Runtime rt =  Runtime.getruntime ();                process killprcess = rt.exec (cmd);                 killprcess.waitfor ();                killprcess.destroy ();         }catch (exception e)     &nbsP;   {            e.printstacktrace ();         }   }

 private static string getkillprocesstreecmd (long pid) {        String result =  "";        if (pid !=  Null)            result =  "cmd.exe /c  taskkill /PID  "+pid+"  /F /T  "       return";  result;   }   }public class ProcMana{     Procmana () {}    public static void main (string agrv[]) {     system.out.println ("Start.");     string strs[] = {"hello",  "world",  "bye"};     for (int i= 0 ; i< strs.length ();  ++i) {         procmanasrc.startprocess (strs[i]);     }   &Nbsp;try {    thread.sleep (1000*30);    } catch  ( Interruptedexception e)  {    // TODO Auto-generated catch  Block    e.printstacktrace ();     }    for (int  i= 0 ; i< strs.length ();  ++i) {         procmanasrc.closeprocess (strs[i]);     }    }}

As you can see, the code executes the. EXE application for STRs three elements, and switches control depending on the Element.

It is important to note that

String cmd = "cmd/c start. \\myprint\\myprint.exe" + "\" "+ str +" \ "";

There is no cmd plugin to use cmd/c start to see the printout, but in the program management can see the process Muprint.exe run, but the use of cmd/c start is not closed, Personal feeling is because after using the Java program should not find the DOS open applications.

I do not understand this Point. however, in general, we are directly calling other applications, dos windows are not needed, so in many cases are adaptable. The author here to lift the print application just to do a demonstration, and actually another to Use.


This article is from the "icesongqiang" blog, make sure to keep this source http://icesongqiang.blog.51cto.com/9438141/1854248

Java open multi-process, execute. exe file

Related Article

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.