【问题标题】:psql: command not found when running bash script in eclipsepsql:在 Eclipse 中运行 bash 脚本时找不到命令
【发布时间】:2019-04-15 13:21:02
【问题描述】:

我正在编写一个 Java 方法来使用 Eclipse 运行本地 bash 脚本。代码如下:

public static void setUp() throws IOException, InterruptedException {
        Runtime rt = Runtime.getRuntime();
        Process p;
        String filePath = new File("").getAbsolutePath();
        filePath = filePath.concat("/path/to/the/script");
        String command = String.format("sh %s/setUp.sh", filePath);
        System.out.println(command);
        try {
            p = rt.exec(command);
            final int errorValue = p.waitFor();
            if (errorValue != 0) {
                System.out.println("error detected!");
                InputStream error = p.getErrorStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(error));
                String line = "";
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                p.destroy();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

而且 bash 文件非常简单:

#!/bin/bash
#create a local db and import some data
createdb -U dummy -O dummy -h localhost dbname
psql -h localhost -d dbname --file $1

问题是

line 4: createdb: command not found
line 5: psql: command not found

我可以在终端中运行脚本,我可以在终端中复制第4行和第5行,我可以在终端中运行mvn exec命令来运行该方法。所有作品。

唯一不起作用的是它是在 eclipse 中执行的。

我愿意接受任何意见或建议,如果您需要更多信息,请告诉我。

提前感谢所有帮助!

【问题讨论】:

    标签: java eclipse bash postgresql maven


    【解决方案1】:

    我怀疑用于执行脚本的Runtime 的路径设置不正确。尝试对脚本中的命令使用完整路径,看看是否可行(您可以通过在终端中运行 which createdbwhich psql 找到完整路径)。

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 2012-04-12
      • 2014-03-18
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      相关资源
      最近更新 更多