[Scala] Java Execution Shell command

Source: Internet
Author: User
Tags first string

Methods

Both the Processbuilder.start () and the Runtime.exec () methods are used to create an operating system process (to perform command-line operations) and return an instance of the process subclass that is used to control the status of processes and obtain relevant information.

    • The Runtime.exec(String) method takes a single command string, the IT splits into a command and a sequence of arguments.
     Process process = Runtime.getruntime (). EXEC ("C:\DoStuff.exe-arg1-arg2");
    • The ProcessBuilder constructor takes a (varargs) array of strings. The first string is the command name and the rest of them is the arguments.
New Processbuilder ("C:\DoStuff.exe", "-arg1", "-arg2"); // or alternatively list<string> params = java.util.Arrays.asList ("C:\DoStuff.exe", "-arg1", "-arg2"new  = Builder.start ()
Examples
def execute = {context = =val Command=Shell (context) var result:try[string]=NULLvar process:process=NULL      Try{Val Builder=NewProcessbuilder (scala.collection.JavaConversions.seqAsJavaList (command)) Builder.redirectoutput ( ProcessBuilder.Redirect.INHERIT) Builder.redirecterror (ProcessBuilder.Redirect.INHERIT) process=Builder.start () process.waitfor () Val ExitCode=Process.exitvalue ()if(ExitCode! = 0) {result= Failure (NewIllegalmonitorstateexception (S "Exit Code of process is no zero, but: $exitCode"))        }Else{result= Success (S "Successfully executed command $command")        }      }Catch{         CaseE:exception = result =Failure (e)}finally {        if(process!=NULL) {Process.destroy ()}} result}

[Scala] Java Execution Shell command

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.