The blog's code runs on the premise of knowing how to use Java to invoke an external program, and of course the code also discloses how to invoke an external program using Java.
Java (other languages are the same) the external application's working path has changed when it calls an external application. Recently in the development of the project encountered this problem, made a long time to guess that is the reason. I'm running this external program that requires extra data for input, normally external applications and extra input data in a directory. Now the problem with calling this external application from your own code is that while the external application and the extra input data are in the same directory, the external application's work path has become the root directory of the program that you wrote. After a period of time of data query, the problem can be solved.
Java calls to external applications need to use runtime and process,
instance method of process exec is the method used to invoke external programs, and it has several overloads
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)
The first type of call can be used to invoke the function of the external program.
Such as
Runtime rn=runtime.getruntime ();
Process process=rn.exec ("F:/test.exe");
Look at so many overloaded functions at this point, it is very obvious to call the 2nd or 6th function, that is, to specify the working path of the external application.
Runtime rn=runtime.getruntime ();
Process process=rn.exec ("F:/test.exe", New String[]{"}", New File ("E:/data")
At this point you just put the external program to run the required input data file into the E:/data directory.