" Program Files"命令实习生未找到CMD

时间:2016-08-29 14:43:43

标签: java cmd processbuilder

我使用java来执行命令行来创建数据库,当我执行这段代码时出错:

private final String POSTGRES_PATH = "\"C:\\Program Files\\PostgreSQL\\9.3\\bin\\psql.exe\"";
private final String DATA_BASE = "bd_name";

private void creerDataBase() {
    String command = this.POSTGRES_PATH + " -U postgres -d postgres -c \"CREATE DATABASE " + this.DATA_BASE + "\"";
    System.out.println("command = " + command);
    String creerBDD = executerCommande(command);
    System.out.println("Resultat : " + creerBDD);
}

public String executerCommande(String command) {
    String line;
    String resultat = "";
    try {
        ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command);
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while (true) {
            line = r.readLine();
            if (line == null) {
                break;
            }
            resultat += line + "\n";
        }
    } catch (Exception e) {
        System.out.println("Exception = " + e.getMessage());
    }
    return resultat;
}

我得到了这个结果:

command = "C:\Program Files PostgreSQL\9.3\bin\psql.exe"\ -U postgres -d postgres -c "CREATE DATABASE bd_name"

并出现此错误:

'C:\Program' n'est pas reconnu en tant que commande interne

这意味着该程序不是实习生。

但是当我在CMD中执行此命令时它工作正常吗?

有没有办法构建此路径,因为ProcessBuilder无法识别C:\Program Files

4 个答案:

答案 0 :(得分:2)

一种可能的解决方案是从常量字段中删除路径(带空格)并使用directory方法:

  

设置此进程构建器的工作目录。随后由此对象的C:\Users\dinesh_pundkar\Desktop>python b.py 1. Print phone book 2. add phone book 3. look for number using first name only 4. look by last name 5. exit () type menu selection number - 1 phone book: Phone book file not created. First create it to read it 1. Print phone book 2. add phone book 3. look for number using first name only 4. look by last name 5. exit () type menu selection number - 2 enter first name: "Dinesh" enter last name: "Pundkar" enter phone number: "12345" 1. Print phone book 2. add phone book 3. look for number using first name only 4. look by last name 5. exit () type menu selection number - 1 phone book: first_name,last_name,phone_number Dinesh,Pundkar,12345 1. Print phone book 2. add phone book 3. look for number using first name only 4. look by last name 5. exit () type menu selection number - 3 look up number first name:"Dinesh" First Name is Dinesh Last Name is Pundkar the number is 12345 1. Print phone book 2. add phone book 3. look for number using first name only 4. look by last name 5. exit () type menu selection number - 4 search by last name please enter last name: "Pundkar" First Name is Dinesh Last Name is Pundkar the number is 12345 1. Print phone book 2. add phone book 3. look for number using first name only 4. look by last name 5. exit () type menu selection number - 5 C:\Users\dinesh_pundkar\Desktop> 方法启动的子进程将使用此作为其工作目录。参数可以是start() - 这意味着使用当前Java进程的工作目录,通常是系统属性null命名的目录,作为子进程的工作目录。

将您的代码更改为:

user.dir

答案 1 :(得分:2)

感谢@Aaron他的想法帮助我,所以我用这个来解决这个问题:

android-maps-utils

private final String POSTGRES_PATH = "C:\\PROGRA~1\\PostgreSQL\\9.3\\bin\\psql.exe"; 对此进行了调整:C:\\PROGRA~1

答案 2 :(得分:1)

如果要运行单独的二进制程序,请不要运行cmd.exe cmd.exe适用于*.cmd*.bat

等脚本

使用cmd.exe,您必须将您的命令作为CMD的参数传递,并且您应该管理所有特定于操作系统的陷阱,例如带有空格的长路径,qoutes内的引号等。

相反,您最好自己运行psql ProcessBuilder将命令和所有参数作为单独字符串的列表。并且ProcessBuilder足够聪明,可以用引号和空格自行完成所有必要的魔法。

注意参数列表 - shell用空格分隔参数,而psql可能将字符串序列识别为单个参数。
我们可以假设-U postgresspsql的单个参数,但对于shell(在我们的例子中为cmd.exe),这些是两个独立的参数 - -Upostgress ,所以我们应该将它们分别传递给ProcessBuilder

因此,运行psql的更好方法是直接运行它,类似:

 new ProcessBuilder("C:\\Program Files\\PostgreSQL\\9.3\\bin\\psql.exe", 
                    "-U", "postgres", 
                    "-d", "postgres", 
                    "-c", "\"CREATE DATABASE " + this.DATA_BASE + "\"");

答案 3 :(得分:0)

您可以尝试的是代替程序和文件之间的空间是%20或\ s。 所以喜欢:

command = "C:\\Program%20Files\\PostgreSQL\\9.3\\bin\\psql.exe"

command = "C:\\Program\sFiles\\PostgreSQL\\9.3\\bin\\psql.exe"

我希望其中一个适合你,请让我知道

编辑:使用double \来识别\