Java calls Shell

Source: Internet
Author: User

When writing a program, you sometimes need to call the shell script in a Java program. You can use the exec method of runtime to call the shell program and run the script.

Each Java application hasRuntimeClass instance, so that the application can be connected to the running environment. You can use the runtime object to return the running environment, including the number of CPUs and the memory size of the virtual machine. You can also use the exec method to call and execute commands. You can usegetRuntimeMethod to obtain the current runtime instance.

 

public boolean ExeShell(){  
    Runtime rt = Runtime.getRuntime();  
    try {  
        Process p = rt.exec(checkShellName);  
        if(p.waitFor() != 0)  
            return false;  
    } catch (IOException e) {  
Syslog. Error ("no detection script found ");
        return false;  
    } catch (InterruptedException e) {  
        e.printStackTrace();  
        return false;  
    }  
    return true; 
}

The P. waitfor () statement is used to wait for the completion of the sub-process. The return value is the Process Termination exit code.

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.