Reputation: 925
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?
Upvotes: 1
Views: 382
Reputation: 925
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?
Upvotes: 0
Reputation: 31279
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
)
Upvotes: 1