0

I use openjdk for my work, but sometimes I need oracle's JRE. I thought that I could use 'env' command for this, but can't figure out why it's not working. I have openjdk in /usr/lib/jvm/java-1.8.0-openjdk-amd64 and oracle's jdk in /usr/lib/jvm/jdk-8-oracle-x64 when I'm running: env JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-x64 java -version i get:

java version "1.7.0_75" OpenJDK Runtime Environment (IcedTea 2.5.4) (7u75-2.5.4-3) OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)

but if i try: env JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-x64 env|grep JAVA I see:

JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-x64

I have 'feeling' that this is somehow related to process forking and inheritance. I know that i can simply use export name=variable command and etc. But would appreciate get explanation is it possible with 'env' command and how. Thank you! (I am using debian linux, unstable repo)

0

It is possible to do that using the env command, however you have to use a little work around and call sh, see the following code snippets:

# env var=bla echo $var
>
# env var=bla sh -c 'echo $var'
> bla
# echo $var
>

You can find more information using on info coreutils 'env invocation'

Unfortunatelly I can't give you any further explanations why it only works using sh -c ''.

It seems env has a rather non-intuitive behavior...

# env PATH= echo $PATH
> env: echo: No such file or directory
# env PATH= /bin/echo $PATH
> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • Thank you for your answer! I've found one misleading in my question: java -version depends on /usr/bin/java link destination. So, if i would like 'fully' switch java environment for specific task i should change JAVA_HOME and /usr/bin/java or /etc/alternatives/java link destination. And I really intersted what is smartest way to do this. Should I open new question? – drozdik Jun 25 '15 at 12:26
  • I've started new question unix.stackexchange.com/questions/212139/… – drozdik Jun 25 '15 at 14:03
  • The reason is that "$var" gets expanded to the contents of that variable before the command is run. So 'env' saw an argument list of the previous value of $var. – dataless Jul 6 '18 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.