Java_Home Not Found as Sudo

JAVA_HOME not found as Sudo

By default, sudo will cleanup the environment of the spawned commands. Pass -E to keep it:

sudo -E env

Compare to:

sudo env

JAVA_HOME not found in Ubuntu

Basically, you have stuffed up the "/etc/environment" file:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/>
JAVA_HOME = /usr/lib/jvm/jdk-15

The first line should not end with a > character. And there is a missing ". And it doesn't make sense for the root directory ( / ) to be on the search path.

The second line should not have spaces before and after the =.

However, I don't know exactly what the video tutorial was trying to tell you to do ... or why it even suggested that you should edit that file. (I certainly wouldn't mess with that file!!)

My advice:

  1. Find and >>read<< a Linux tutorial (or book!) on how to use the shell. It will explain what environment variables, how they are set and how they are used. It will also explain what PATH is and what it should contain.

    I do NOT recommend watching videos. My observation is that they are too superficial and will often leave the viewer thinking that they understand, when they don't.

  2. Revert the "/etc/environment" file to what it was before you started.

  3. Either watch the video again and make the changes more carefully ... OR ... don't change it. If you don't change it you can put the environment variables into your shell startup script; e.g. "~/.bash_profile" if your shell is bash; see step 1!

How to set JAVA_HOME in Linux for all users


  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. vim /etc/profile

    Prepend sudo if logged in as not-privileged user, ie. sudo vim

  3. Press 'i' to get in insert mode
  4. add:

    export JAVA_HOME="path that you found"

    export PATH=$JAVA_HOME/bin:$PATH
  5. logout and login again, reboot, or use source /etc/profile to apply changes immediately in your current shell

JAVA_HOME and PATH are set but java -version still shows the old one

While it looks like your setup is correct, there are a few things to check:

  1. The output of env - specifically PATH.
  2. command -v java tells you what?
  3. Is there a java executable in $JAVA_HOME\bin and does it have the execute bit set? If not chmod a+x java it.

I trust you have source'd your .profile after adding/changing the JAVA_HOME and PATH?

Also, you can help yourself in future maintenance of your JDK installation by writing this instead:

export JAVA_HOME=/home/aqeel/development/jdk/jdk1.6.0_35
export PATH=$JAVA_HOME/bin:$PATH

Then you only need to update one env variable when you setup the JDK installation.

Finally, you may need to run hash -r to clear the Bash program cache. Other shells may need a similar command.

Cheers,

Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

As Biswapriyo suggested, you should use WSLENV.

  1. Open PowerShell. Then set JAVA_HOME to the path to your java installation.

  2. In your case, run setx JAVA_HOME "D:\Program Files\Java\jdk-11.0.1"

You should see a message that says "SUCCESS: Specified value was saved".


  1. Then run setx WSLENV "JAVA_HOME/p".

You should see the success message again.


  1. Type 'env' into your WSL bash prompt.

You should see JAVA_HOME correctly set at this point.

Note: If step 2 doesn't work, you might want to changing the path to JAVA_HOME to include the \bin folder.

JAVA_HOME is not defined correctly on Ubuntu?

Add both JAVA_HOME & PATH to your ~/.profile

export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

And, add following to your /etc/profile.d/java.sh

JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
export JAVA_HOME
PATH=${JAVA_HOME}/bin:${PATH}
export PATH
JRE_HOME=/usr/lib/jvm/jre
export JRE_HOME
JAVA_OPTS="-XX:+AggressiveOpts -Xms256m -Xmx512m -XX:MaxPermSize=256m -XX:+DisableExplicitGC"
export JAVA_OPTS

For more info, Refer Documentation

Hope it helps.

JAVA not in path although JAVA_HOME set

Specifically for OpenBSD6.0, add

export JAVA_HOME=/usr/local/jdk-1.8.0/

to your .profile.

This specific version of the jdk, and possibly the basic path itself is subject to change in subsequent and previous versions of OpenBSD, you have been warned.

Failed to find 'JAVA_HOME' environment variable. Try setting it manually

Your $JAVA_HOME is pointing to the correct location. But the path should have $JAVA_HOME/bin directory and not $JAVA_HOME itself.

JAVA_HOME="/opt/jdk1.12.0"
export JAVA_HOME
PATH="$PATH:$JAVA_HOME/bin"

You should consider using the Oracle Java PPA instead. It usually does more than what a manual installation would do. You don't have to worry about setting up the environment variables either. That's what most people use.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Try running java -version and javac -version to verify that the path is set.

Hope it helps.

On mvn install command errors with and without sudo ( JAVA_HOME and --release flag respectively)

When you run a command using sudo, the environment variables from your current (non-privileged) shell are NOT passed through to the environment in which the command is run.

Try this:

$ export FOO=BAR
$ sudo export

and you won't see FOO in the list of variables.

So, when you run sudo mvn ... the JAVA_HOME setting is not being passed to Maven, and it says so.

If you want to pass all of the environment variables, use sudo -E or similar. This is explained in the manual entry for sudo.



Related Topics



Leave a reply



Submit