This blog post'sCodeThe premise is to understand how to use Java to call externalProgramOf course, the Code also reveals how to use Java to call external programs.
The working path of the external application has changed when Java (similar to other languages) calls an external application. I encountered this problem when I was working on project development recently. After a long time, I guess this is the reason. My external program requires additional data for input. Normally, external applications and additional input data are stored in a directory. The problem with calling this external application by writing code is: although the external application is in the same directory as the additional input data, however, the working path of the external application has changed to the root directory of the program you write. After a period of data query, the problem was solved.
Java calls external applications to use runtime and process,
Process instance method exec is used to call external programs, it has the following
Exec (string command) exec (string command, string envp [], file DIR) exec (string cmd, string envp []) exec (string cmdarray []) exec (string cmdarray [], string envp []) exec (string cmdarray [], string envp [], file DIR)
Normally, the first method can call an external program.
For example
Runtime Rn = runtime. getruntime (); process process=rn.exe C ("F:/test.exe ");
After reading so many overload functions, if you want to specify the working path of an external application, it is obvious that you call 2nd or 6th functions, that is
Runtime Rn = runtime. getruntime (); process process=rn.exe C ("F:/test.exe", new string [] {""}, new file ("E:/Data ")
In this case, you only need to put the input data file required by the external program to the E:/data directory.