Java calls external programs and specifies the working path

Source: Internet
Author: User

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.

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.