简体   繁体   中英

When I am running java program manually from terminal it is working fine but it is not working when i am running from eclipse

My program is not running from eclipse but it is running through terminal in ubuntu.

Below is the shell script that i am running in java

#!/usr/bin/env bash

# Running sqoop commands

s="$(sqoop help)"

echo "$s"

Below is the java code

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();
        }
    }
}

error message :

/home/avinash/sqoop.sh: line 5: sqoop: command not found

The error message is from your script. Not from Eclipse.

Eclipse (or the JVM to be more precise) does not know about the environment variables or working directory of the script. In contrast: If you run the script from command line environment variables (eg PATH) or working directory are known.

You can use method Runtime.exec(String command, String[] envp, File dir) to specify this in your Java code. So I guess this should work:

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

Try with the command "sh /home/avinash/sqoop.sh". I feel since the ubuntu isn't aware what kind of file it is,It's clearly throwing command not found error.

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