Executing linux commands with Java code

Source: Internet
Author: User

Premise:

The Java code is written under Windows, packaged and placed under Linux, and executes a script.

Java code:

Try  {
//The code that works is actually the following line, the parameter is the code to execute in Linux Runtime.getruntime (). EXEC ("sh generator.sh"). WaitFor (); Catch (IOException e) { e.printstacktrace (); Catch (interruptedexception e) { e.printstacktrace (); }

Transferred from: https://www.cnblogs.com/tohxyblog/p/6501396.html

Java in enterprise-level project development, whether it is mandatory function needs, or for the sake of simple Java implementation, you need to invoke the server command script to execute. In Java,Runtime.getruntime (). EXEC ( ) implements this function.

Usage:

Executes the specified string command in a separate process
public Process exec (String command)
Executing specified commands and variables in a separate process
public Process exec (string[] cmdarray)
Executes the specified command and variable in a separate process in the specified environment
public Process exec (String command,string[] envp)
Executes the specified command and variable in a separate process in the specified environment
public Process exec (string[] cmdarray,string[] envp)
Executes the specified string command in a separate process with the specified environment and working directory
public Process exec (String command,string[] Encp,file dir)
Executes the specified commands and variables in a separate process in the specified environment and working directory
public Process exec (string[] cmdarray,string[] envp,file dir)

Example:

1.

Commands that are directly called/started/searched for programs and files under Windows, such as
Runtime.getruntime (). EXEC (String command); Open Notepad under Windows

2.

 Public Process exec (string[] cmdarray);
Linux under
Runtime.getruntime (). EXEC (New string[]{/bin/sh ","-C "});
Windows under
Runtime.getruntime (). EXEC (new string[]{"cmd", "/C", Cmds});

Depth:

Several methods of process

1, Destroy (): Kill the child process

2, Exitvalue (): Returns the exit value of the child process, the value 0 indicates normal termination

3, Geterrorstream (): Gets the error stream of the child process

4, getInputStream (): Gets the input stream of the child process

5, Getoutputstream (): Gets the output stream of the child process

6. WaitFor (): Causes the current thread to wait and, if necessary, waits until the process represented by that process object has been terminated. If the child process has been terminated, this method returns immediately. If the child process is not terminated, the calling thread will be blocked, knowing that the exit child process, according to management, 0 indicates normal termination.

Note: In Java, calling runtime threads to execute scripts is very resource-intensive, so remember not to use them frequently!

When calling runtime to execute a script, the JVM actually opens a sub-thread to invoke the JVM's system command, which opens three channels: the input stream, the output stream, the error stream, where the output stream is the channel that the child thread is going to call.

As we all know, waitfor is waiting for the child thread to execute the command after the end of the access, but in runtime, open the program's command if not closed, no operator thread end, such as the following code.

Private Static NULL  = Runtime.getruntime (). EXEC ("notepad.exe");p. WaitFor (); System.out.println ("---------------I was carried out");

In the above code, open Notepad in Windows, if we do not manually close Notepad, then the output statement will not be executed, this thing need to understand.

Executing linux commands with Java 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.