Java external execution of operating system commands (Java creates local system processes)

Source: Internet
Author: User

Java external execution of the operating system command Java to create a local system process

The Java thread mechanism, process, and processbuilder record the in/out/error information of enabled processes.

Level: elementary

Wang Yancheng (ybygjy@gmail.com)

April 13, 2010)

This article mainly describes how to use Java Process and processbuilder classes and how to handle problems encountered during use.

In our web applications, local system processes need to be used for processing, for example:

  • Use process to enable a system service
  • Regular File Format Conversion
  • Application restarts automatically
  • Scheduled system backup and Database Backup

Of course, using Java to execute local system applications is only one way to solve the problem. We can also use other methods to solve the problem.

Small details
· Local System Environment Variables

The purpose of configuring local system environment variables is to allow the Java VM to find external applications. We must remember how to configure the Java environment variables ?. After configuring the environment variables, we can specify the operating system commands for the Java VM when creating the system process (see the code for details ).

· Listening for standard/abnormal output during process execution

Back to Top

Sample Code

This code is included in the flashpaper-based document read-only document and code.

Import Java. io. file; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. NIO. bytebuffer; <br/> Import Java. NIO. channels. channels; <br/> Import Java. NIO. channels. readablebytechannel; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; <br/> Import Java. util. map; </P> <p> Import Org. apache. commons. logging. log; <br/> Import Org. apache. commons. logging. logfactory; </P> <p> Import com.hd.sy.util.doc. fileconvert; <br/> Import COM. HD. util. hdexception; <br/>/** <br/> * supported file conversion swf format <br/> * @ author wangyancheng <br/> * @ version 2009-12-3 <br/> */< br/> public class fileconvert4swfimpl implements fileconvert {<br/>/** name of the converted file */<br/> private file fileinst = NULL; <br/>/** output directory */<br/> private file outfolder = NULL; <br/>/** <br/> * {@ inheritdoc} <br/> * check whether fileinst is empty. <Br/> */<br/> Public file doconvertfile (File inputfileinst, file outfilepath) throws hdexception {<br/> New innerconvertthread (inputfileinst, outfilepath ); <br/> return fileinst; <br/>}< br/>/** <br/> * {@ inheritdoc} <br/> * check whether fileinst is empty. <br/> */<br /> Public file doconvertfile (File inputfileinst) throws hdexception {<br/> New innerconvertthread (inputfileinst, null); <br/> return fileins T; <br/>}< br/>/** <br/> * setoutfolder <br/> * @ Param setoutfolder <br/> */<br/> Public void setoutfolder (File setoutfolder) {<br/> This. outfolder = setoutfolder; <br/>}< br/>/** <br/> * {@ inheritdoc} <br/> */<br/> Public String getcommand () {<br/> return defaultcommand; <br/>}< br/>/** <br/> * {@ inheritdoc} <br/> */<br/> Public void setcommand (string tmpcommand) {<br/> T His. defaultcommand = tmpcommand; <br/>}< br/>/** default command */<br/> private string defaultcommand = "flashprinter.exe "; <br/>/** <br/> * innerconvert <br/> * @ author wangyancheng <br/> * @ version 2009-12-4 <br/> */<br/> class innerconvertthread implements runnable {<br/>/** inputfile */<br/> private file infile = NULL; <br/>/** outfilepath */<br/> private file OUTFILE = new file (system. ge Tproperty ("Java. io. tmpdir ")); <br/>/** <br/> * constructor <br/> * @ Param infileobj <br/> * @ Param outfilepath <br/> */<br/> Public innerconvertthread (File infileobj, file outfilepath) {<br/> This. infile = infileobj; <br/> This. OUTFILE = outfilepath = NULL? Outfolder: outfilepath; <br/> thread th = new thread (this); <br/> // th. setdaemon (true); <br/> th. start (); <br/>}< br/>/** <br/> * constructor <br/> * @ Param setinfile input file to set <br/> * @ Param setoutfolder Output Folder to set <br/> */<br/> Public innerconvertthread (string setinfile, string setoutfolder) {<br/> This. infile = new file (setinfile); <br/> This. OUTFILE = NULL = setoutfolde R? OUTFILE: New file (setoutfolder); <br/> New innerconvertthread (infile, OUTFILE ); <br/>}< br/>/** <br/> * {@ inheritdoc} <br/> */<br/> Public void run () {<br/> string tmpoutfile = OUTFILE. getpath () <br/>. concat (file. separator ). concat (infile. getname (). replaceall ("[.] {1 }. * $ ",". SWF "); <br/> fileinst = new file (tmpoutfile); <br/> List commandarray = new arraylist (); <br/> commandarray. add (default Command); <br/> commandarray. add (infile. getpath (); <br/> commandarray. add ("-o"); <br/> commandarray. add (tmpoutfile); <br/> processbuilder pbobj = new processbuilder (); <br/> pbobj. command (commandarray); <br/> map envmap = pbobj. environment (); <br/> envmap. clear (); <br/> envmap. putall (system. getenv (); <br/> pbobj. directory (OUTFILE); <br/> pbobj. redirecterrorstream (true); <br/> try {<br/> Process Proobj = pbobj. start (); <br/> final inputstream ins = proobj. getinputstream (); <br/> final bytebuffer = bytebuffer. allocate (1024); <br/> thread th = new thread () {<br/> Public void run () {<br/> readablebytechannel rbcobj = channels. newchannel (INS); <br/> while (true) {<br/> try {<br/> while (rbcobj. read (bytebuffer )! =-1) {<br/> bytebuffer. flip (); <br/> logger.info (Java. NIO. charset. charset. defaultcharset (). decode (bytebuffer); <br/> bytebuffer. clear (); <br/>}< br/>} catch (ioexception e) {<br/> logger. error (E); <br/>}< br/>}; <br/> th. setdaemon (true); <br/> th. start (); <br/> try {<br/> proobj. waitfor (); <br/> logger. error ("conversion completed. "+ tmpoutfile); <br/>} catch (interruptedexception e) {<br/> logger. error (E); <br/>}< br/>}catch (ioexception e) {<br/> logger. error (E ); <br/>}< br/>/** <br/> * test entry <br/> * @ Param ARGs parameter list <br/> */<br/> Public static void main (string [] ARGs) {<br/> file inputfileinst = new file ("D: // work // workspace // mywork // SRC // org // ybygjy // file // cv2swf // file // servlet-2_3-fcs-spec.pdf "), <br/> outputfileinst = new file ("D: // work // workspace // mywork // SRC // org // ybygjy // file // cv2swf // file //"); <br/> fileconvert fc = new fileconvert4swfimpl (); <br/> try {<br/> FC. doconvertfile (inputfileinst, outputfileinst); <br/>}catch (hdexception e) {<br/> E. printstacktrace (); <br/>}< br/>/** logger */<br/> private log logger = logfactory. getlog (fileconvert4swfimpl. class); <br/>}< br/>

Home Page
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.