当我从终端手动运行java程序时它工作正常,但是当我从eclipse运行时它无法正常工作

时间:2017-04-09 06:14:22

标签: java eclipse bash sqoop

我的程序不是从eclipse运行的,而是在ubuntu中通过终端运行。

下面是我在java中运行的shell脚本

#!/usr/bin/env bash

# Running sqoop commands

s="$(sqoop help)"

echo "$s"

以下是java代码

package flexibility;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Flex {

    public static void main(String args[]) throws Exception {

        String s = null;
        String line = "";
        String sqoopCommand = "sqoop help";

        try {

            Process p = Runtime.getRuntime().exec("/home/avinash/sqoop.sh");
            p.waitFor();

            StringBuffer output = new StringBuffer();
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            while ((line = stdInput.readLine()) != null) {
                output.append(line + "\n");

            }
            while ((line = stdError.readLine()) != null) {
                output.append(line + "\n");

            }
            System.out.println("### " + output);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

错误消息:

/home/avinash/sqoop.sh:line 5:sqoop:找不到命令

2 个答案:

答案 0 :(得分:0)

错误消息来自您的脚本。不是来自Eclipse。

Eclipse(或更准确的JVM)不了解脚本的环境变量或工作目录。相反:如果从命令行运行脚本,则环境变量(例如PATH)或工作目录已知。

您可以使用方法Runtime.exec(String command, String[] envp, File dir)在Java代码中指定它。所以我想这应该有用:

Process p = 
    Runtime.getRuntime().exec("/home/avinash/sqoop.sh", null, new File("/home/avinash/"));

答案 1 :(得分:0)

尝试使用“sh /home/avinash/sqoop.sh”命令。我觉得既然ubuntu不知道它是什么类型的文件,它显然是命令找不到错误。

相关问题