Java execution cmd command and Linux command

Source: Internet
Author: User
Tags file copy stringbuffer


One: Window under the execution of CMD specified

Program Examples:

/* This method implements the automatic file copy function. Copy the specified file name from the source path to the destination path      * Using system commands automatically create a destination path      */public static Boolean CopyFile if the destination path does not exist
    (String Origpath, String destpath, String filename) throws exception{String osname = System.getproperty ("Os.name");
    Boolean flag = false; /* System command-supported operating system Windows XP, 2003 7*/if (!) ( Osname.equalsignorecase ("Windows XP") | | Osname.equalsignorecase ("Windows 2000") | | Osname.equalsignorecase ("Windows 2003") | |
    Osname.equalsignorecase ("Windows 7")) {return flag;
    Runtime RT = Runtime.getruntime ();
    Process p = null;
    File F = new file (destpath);
    if (!f.exists ()) {f.mkdirs ();
    int exitval;
    p = rt.exec ("cmd exe/c copy" + origpath+filename+ "" +destpath); The export value of the process. 
    By convention, 0 indicates normal termination.
    Exitval = P.waitfor ();
    if (Exitval = = 0) {flag = true;
    }else{flag = false;    
    
return flag; }     public static void Main (string[] args) {        try {            CopyFile ("
D:\\data\\ "," d:\\a\\ "," 131204.txt ");         catch (Exception e) {            //TODO auto-generated catch block           
  E.printstacktrace ();        }               

  }


Second: Execute shell command under Linux

Program Examples:

Package edu.test;
Import Java.io.InputStreamReader;
	 
	Import Java.io.LineNumberReader;
	 /** * Java executes Linux commands in a Linux environment and returns the command return value.
	            * @author Lee */public class Execlinuxcmd {public static Object exec (String cmd) {try {
	            String[] Cmda = {"/bin/sh", "-C", CMD};
	            Process process = Runtime.getruntime (). exec (CMDA);
	            LineNumberReader br = new LineNumberReader (New InputStreamReader (Process.getinputstream ()));
	            StringBuffer sb = new StringBuffer ();
             String Line;
	                while (line = Br.readline ())!= null) {System.out.println (line);
	            Sb.append (line). append ("\ n");
	        return sb.tostring ();
	        catch (Exception e) {e.printstacktrace ();
	    return null;  public static void Main (string[] args) {//TODO auto-generated method stub String pwdstring = EXEC ("pwd"). ToString ();
	         
	        String netsstring = exec ("netstat-nat|grep-i \" 80\ "|wc-l"). ToString ();
	        System.out.println ("========== obtained value =============");
	        System.out.println (pwdstring);
	    System.out.println (netsstring); }
	 
	}
Execution results:



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.