1

This is my code:

public static void main(String[] args) throws Exception { String[] cmd = {"which", "hive"}; Process proc = Runtime.getRuntime().exec(cmd); BufferedReader reader = new BufferedReader( new InputStreamReader( proc.getInputStream() ) ); String line = ""; while((line = reader.readLine()) != null) { System.out.print(line + "\n"); } proc.waitFor(); } 

In my terminal:

which hive:/home/as/hive/bin/hive which gcc:/usr/bin/gcc 

But when I run this java code:

which hive:no result which gcc:/usr/bin/gcc 

I've added $HIVE_HOME into ~/.bashrc (I'm using Ubuntu 14.04 64 bit and Java 8), so what should I do now?

2
  • If you want to see errors, I suggest you read the error stream. Or you can combine the two streams into one to make it easier to read. Commented Dec 13, 2014 at 13:48
  • 2
    How are you running Java? Are you sure hive is in your PATH when you run Java? Try printing System.out.println(System.getenv().get("PATH")); Commented Dec 13, 2014 at 13:52

2 Answers 2

1

The command which looks in all the directories specified in the environment variable PATH. On Linux, the directories are separated by a colon (:) and on Windows by a semicolon (;).

You can check what PATH is being passed to which by doing a

System.out.println(System.getenv("PATH")); 

And you can pass a different path to Runtime.exec:

String[] env = { "PATH=/home/as/hive/bin" }; Process proc = Runtime.getRuntime().exec(cmd, env); 

If you do that, it which will find the hive command (but it will not longer find gcc unless you also add /usr/bin to the PATH, as in PATH=/home/as/hive/bin:/usr/bin)

Sign up to request clarification or add additional context in comments.

3 Comments

as@ubuntu:~$ echo $PATH; /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/as/bin:/home/as/jdk1.8.0_25/bin:/home/as/hadoop/bin:/home/as/hadoop/sbin:/home/as/bin:/home/as/jdk1.8.0_25/bin:/home/as/sqoop-1.4.5.bin__hadoop-2.0.4-alpha/bin:/home/as/hbase/bin:/home/as/hive/bin
in java: System.out.println(System.getenv().get("PATH")); result: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/as/bin:/home/as/jdk1.8.0_25/bin
On my machine, the java binary takes the path that was in $PATH when you start Java. If it's different on your machine, then you have a Unix (probably Linux) issue, possibly with the way you start Java, or maybe you started your application from your .profile before you set the PATH. It's hard to tell without a lot more information (and probably out of scope for SO). But as an immediate resolution/workaround, you can add to your PATH within Java (by passing an env variable to exec)
0

in my terminal:
as@ubuntu:~$ echo $PATH;
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/as/bin:/home/as/jdk1.8.0_25/bin:/home/as/hadoop/bin:/home/as/hadoop/sbin:/home/as/bin:/home/as/jdk1.8.0_25/bin:/home/as/sqoop-1.4.5.bin__hadoop-2.0.4-alpha/bin:/home/as/hbase/bin:/home/as/hive/bin
but in java:
System.out.println(System.getenv().get("PATH"));

Result:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/as/bin:/home/as/jdk1.8.0_25/bin
so this is $PATH of my account with $PATH of root?
so how to fix it?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.