Java Process calls executable files (Windows or Linux)

Source: Internet
Author: User

Recently, the project saw the need to call other third-party tools in the Java program (such as calling third-party transcoding tools in the Program), so process emerged in this demand:

(I will briefly describe the usage and do not disclose the actual application)

1. Call. bat on Windows Platform

A. in disk F: The New a.txt file is "Love code ";

Create cmd. BAT and edit "Notepad F: \ a.txt"

B. Write the following call statement in the Java program:

 

Commandutil.exe C ("CMD/C start F: // cmd. Bat ");

 

 

The exec method of commandutil is as follows:

 

Public static Boolean exec (string command) throws ioexception, interruptedexception {log.info ("execution Script:" + command); Process = runtime.getruntime(cmd.exe C (command); int exitvalue = process. waitfor (); If (process! = NULL) {process. Destroy ();} If (exitvalue! = 0) {return false;} return true ;}

C. Run the program on the Windows platform. You can open notepad and see the word "love code;

 

2. Call the. Sh script on Linux.

In the same usage, you only need to replace the BAT file with the shell script file in Linux and call it with the same code.

Run a program on the Linux platform to call the shell script.

 

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.