Multiple shell commands are executed in Java, except for the first one that has not been executed __java

Source: Internet
Author: User
Tags readline

The most recent project needs to execute shell commands in Java, in the most common way, with the following code:

public class Shellutil {public
    static string Runshell (String shstr) throws Exception {

        process process;
        Process = Runtime.getruntime (). EXEC (new string[]{"/bin/sh", "-C", shstr});
        Process.waitfor ();
        BufferedReader read = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
        String line = null;
        String result = "";
        while (line = Read.readline ())!=null) {
            result+=line;
        }
        return result;
    }

Called at the following time:

public class Executeshell {public
    static void Main (string[] args) {
        String command = ' some command ';
        String message = Shellutil.runshell (command);
        SYSTEM.OUT.PRINTLN (message);
    }

If you want to execute two commands or multiple commands at the same time, the call will look like the following:

public class Executeshell {public
    static void Main (string[] args) {
        String Command1 = "some command";
        String command2 = "some command";
        String message1 = Shellutil.runshell (Command1);
        String message2 = Shellutil.runshell (Command2);
        System.out.println (message1);
        System.out.println (Message2);
    }

To save performance, it was changed to the following form:

public class Executeshell {public
    static void Main (string[] args) {
        String Command1 = "some command";
        String command2 = "some command";
        String command = Command1 + "&&" + Command2;
        String message = Shellutil.runshell (command);
        SYSTEM.OUT.PRINTLN (message);
    }

The thought would be tricky, but the actual result would be no execution, that is, the whole return was empty, and neither the first nor the second was executed. Solution

Google a bit: http://stackoverflow.com/questions/21908645/command-in-runtime-getruntime-exec-not-working

Follow the above scheme to change the code to

public class Shellutil {

    private static Logger Logger = Loggerfactory.getlogger (shellutil.class);
    public static string Runshell (String shstr) throws Exception {

        process process;
        Process = Runtime.getruntime (). EXEC (new string[]{"/bin/sh", "-C", shstr});
        Process.waitfor ();
        BufferedReader read = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
        String line = null;
        String result = "";
        while (line = Read.readline ())!=null) {
            result+=line;
        }
        return result;
    }

Upcoming Runtime.getruntime (). EXEC ("command"); Change to Runtime.getruntime (). EXEC (new string[]{"/bin/sh", "-C", "Command"});

Note: If it is the Windows operating system, change to Runtime.getruntime (). EXEC (new string[]{"**cmd** exe", "-C", "Command"});

So far, the problem is solved.

Principle Analysis: (to be added--)

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.