Runtime.exe C () executes the Linux Shell

Source: Internet
Author: User

 

The best way to execute system commands is to write a BAT file or shell script. And then call the API. The modification and implementation are much simpler.

 

Currently, the main method for executing external commands is to call the shell of the platform. CMD is used for Windows, and shell is used for Linux or UNIX, the following example shows how to use shell to call a Linux system, and display the result back to the console, other application classes.
Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> public class runtimetest {<br/>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> process; <br/> // string cmd = "ifconfig "; // OK <br/> // string cmd = "SAR-u 1 1 | awk 'nr = 4 {print $8} '"; // blank. MPs queue does not support <br/> string cmd = "/home/heyutao/workspace/chunkoperator/SH/CPU. sh "; // OK </P> <p> try {<br/> // run the command using runtime to generate the process object. <br/> runtime = runtime. getruntime (); <br/> process = runtime.exe C (CMD); <br/> // output stream for obtaining command results <br/> inputstream is = process. getinputstream (); <br/> // use a read output stream class to read <br/> inputstreamreader ISR = new inputstreamreader (is ); <br/> // read rows using a buffer <br/> bufferedreader BR = new buffe Redreader (ISR); <br/> string line = NULL; <br/> while (line = Br. Readline ())! = NULL) {<br/> system. out. println (line); <br/>}< br/> is. close (); <br/> ISR. close (); <br/> Br. close (); <br/>}catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> 

 

CPU. SH Content:

 

#! /Bin/sh <br/> # top <br/> CPU =$ (SAR-u 1 1 | awk 'nr = 4 {print $8 }') <br/> IP = $ (ifconfig | grep-e 'inet ADDR | Inet address' | grep-V' 127. 0.0.1 '| awk-F': ''{print $2}' | awk '{print $1}') <br/> echo" CPU: $ IP: $ CPU" 

 

The execution result is as follows:

 

CPU: 59.64.158.126: 80.68

Sun's Doc actually shows other usage:
Exec (string [] cmdarray, string [] envp, file DIR)
Executes the specified command and arguments in a separate process with the specified environment and working directory.

The DIR is the working directory of the called program. This sentence is actually very useful.

Program Calling in Windows

Process proc extends runtime.getruntime(cmd.exe C ("exefile ");

In Linux, the calling program must be changed to the following format:

Process proc extends runtime.getruntime(cmd.exe C ("./exefile ");

Call system commands in Windows

String [] cmd = {"cmd", "/C", "Copy exe1 exe2 "};
Process proc extends runtime.getruntime(cmd.exe C (CMD );

To call system commands in Linux, you must change the format below.

String [] cmd = {"/bin/sh", "-c", "ln-s exe1 exe2 "};
Process proc extends runtime.getruntime(cmd.exe C (CMD );

In Windows, call system commands and a command line window is displayed.

String [] cmd = {"cmd", "/C", "Start copy exe1 exe2 "};
Process proc extends runtime.getruntime(cmd.exe C (CMD );

In Linux, to call system commands and bring up the terminal window, you must change it to the following format:

String [] cmd = {"/bin/sh", "-c", "xterm-e ln-s exe1 exe2 "};
Process proc extends runtime.getruntime(cmd.exe C (CMD );

You also need to set the working directory of the calling program

Process proc extends runtime.getruntime(cmd.exe C ("exeflie", null, new file ("workpath "));

 

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.