简体   繁体   中英

Executing a command in Java

I'm trying to write a Java class that can execute vagrant from the command line:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

public class Hello {

    public static void deploy() {
        String path = System.getProperty("user.home") + "/ansible/solr/";
        try {
            String command = "vagrant up";
            ProcessBuilder builder = new ProcessBuilder(command);
            System.out.println("------------------------------------------------");
            System.out.println("Executing " + command + " in directory " + path);
            System.out.println("Current path: " + Paths.get(".").toAbsolutePath().normalize().toString());
            builder.directory(new File(path));
            Process child = builder.start();
            watch(child);
            System.out.println("------------------------------------------------");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    private static void watch(final Process process) {
        new Thread() {
            @Override
            public void run() {
                BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null;
                try {
                    System.out.println("------------------------------------------------");
                    System.out.println("Executing Ansible");
                    while ((line = input.readLine()) != null) {
                        System.out.println(line);
                    }
                    System.out.println("------------------------------------------------");
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }.start();
    }

    public static void main(String[] args) {
        deploy();
    }
}

Inside the /ansible/solr directory I have:

-rw-r--r--  1 ubuntu  staff   2.1K Sep 17 08:36 README.md
-rw-r--r--  1 ubuntu  staff   840B Sep 17 08:36 Vagrantfile
drwxr-xr-x  4 ubuntu  staff   136B Sep 17 08:36 provisioning/
-rw-r--r--  1 ubuntu  staff    80B Sep 17 08:36 requirements.yml

and the directory /home/ubuntu/ansible/solr clearly exists. When I execute the program above, I'm getting:

------------------------------------------------
Executing vagrant up in directory /home/ubuntu/ansible/solr
Current path: /home/ubuntu
Cannot run program "vagrant up" (in directory "/home/ubuntu/ansible/solr"): error=2, No such file or directory

Why is that I'm getting this error?

According to ProcessBuilder comment you should separate command and it's arguments:

/**
 * Constructs a process builder with the specified operating
 * system program and arguments.  This is a convenience
 * constructor that sets the process builder's command to a string
 * list containing the same strings as the {@code command}
 * array, in the same order.  It is not checked whether
 * {@code command} corresponds to a valid operating system
 * command.
 *
 * @param command a string array containing the program and its arguments
 */
public ProcessBuilder(String... command) {
    this.command = new ArrayList<>(command.length);
    for (String arg : command)
        this.command.add(arg);
}

so try:

ProcessBuilder builder = new ProcessBuilder("vagrant", "up");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM