Java invokes an external program and specifies the working path

Source: Internet
Author: User
Tags instance method

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.

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.