-1

I am annoyed trying this but all I am trying to do is set JAVA_HOME in /etc/environment file in Bash shell on Windows which is being offered now with Windows. My local Java is at C:\Program Files\Java\jdk1.8.0_91 so in my /etc/environment I am putting

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
export JAVA_HOME="/mnt/c/Program\ Files/Java/jdk1.8.0_91/"

Read online that to specify a space, one should use \. On doing this:

cd /mnt/c/Program\ Files/Java/jdk1.8.0_91/

I can navigate to directory but on putting the same in /etc/environment file and sourcing it, if I run

echo $JAVA_HOME
/mnt/c/Program\ Files/Java/jdk1.8.0_91/

I can tell that it is incorrect as it is reading it as \, and which is why doing

cd $JAVA_HOME

fails

Ubuntu 14.04.5 LTS

If someone can please suggest as to how to solve this will be greatly appreciated. Thank you.

  • 1
    Try export JAVA_HOME=/mnt/c/Program\ Files/Java/jdk1.8.0_91/ (note the lack of quotes) – Charles Green Aug 17 '17 at 13:58
  • @CharlesGreen sorry I forgot to put them in my question.. I do have them in file – user3044240 Aug 17 '17 at 15:23
  • 1
    @user3044240 You miss my point, which is echoed in an answer below. Either do not escape the space, or do not use the quotes, but do not do both! – Charles Green Aug 17 '17 at 15:43
1

To use a space in the Linux world you need to do one (and only one) of the following:

  1. Quote all the things:

    JAVA_HOME="/mnt/c/Program Files/Java/jdk1.8.0_91/"
    cd "$JAVA_HOME"
    
  2. Escape the space:

    JAVA_HOME=/mnt/c/Program\ Files/Java/jdk1.8.0_91/
    cd "$JAVA_HOME"
    

But not both. The issue here is that quoting protects the quoted string and makes it be interpreted literally. So while foo\ bar means foo, a space and then bar, "foo\ bar" means foo, a backslash, a space and then bar.

Also, whenever you use a variable, you should pretty much always put it inside double quotes otherwise the command you are passing it to will read two arguments. You can see what went wrong if you enable bash debugging by running set -x (run set +x to turn it off):

$ set -x
$ cd $JAVA_HOME
+ cd /mnt/c/Program Files/Java/jdk1.8.0_91/
bash: cd: too many arguments

What you ran was cd /mnt/c/Program Files/Java/jdk1.8.0_91/. Since whitespace separates arguments, this means you passed cd two separate arguments: /mnt/c/Program and Files/Java/jdk1.8.0_91/. Quoting causes cd (or any other tool) to read the whole thing as a single argument.

Further reading:

4

Don't use export in /etc/environment, it's just a simple file and not a script. It is reading the lines one by one and is setting the variables that way.

Again, it will not run commands.

  • I did this PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/mnt/c/Program\ Files/Java/jdk1.8.0_91/" – user3044240 Aug 17 '17 at 15:27
  • You should really just use ~/.profile for that. – Ziazis Aug 17 '17 at 15:35
  • I did this and sourced the environment file and it sets JAVA_HOME but on running java -version it fails.. I appended it to PATH as well PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" JAVA_HOME="/mnt/c/Program\ Files/Java/jdk1.8.0_91/" – user3044240 Aug 17 '17 at 15:35
  • Argh! You're absolutely right and I even knew that, dammit. Sorry, serious PEBKAC there. My apologies. – terdon Aug 17 '17 at 15:53
0

Now, you have to set a JAVA_HOME variable for the environment.

Do the following things:

  1. Find the path where your java is installed. (Most likely in /usr/lib/jvm/{JAVA_VERSION}).

  2. Now on the other terminal, open sudo vi /etc/environment

  3. You have to enter: JAVA_HOME="/usr/lib/jvm/java-9-oracle"

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.