Java Environment Variables and Error: ENOENT; A love-hate relationship.
You’ve probably been setting Java Environment Variables wrong this whole time.
Error trying to find JVM: Error: ENOENT: no such file or directory, lstat ‘/usr/lib/jvm/...’
If you ever encountered one of these, god bless you, but, these are quite a hassle to fix.
Originating from how we (wrongly) setup the Java environment variables and with almost no viable fix on the internet, this is a nightmare. And a newbie is expected to setup the variables by him/herself? Hello…?
Why this happens?
Lets break this down.
Error trying to find JVM
The system cannot find the JVM to run/build your Java Projects.
Error: ENOENT
Its basically an error meaning the system cannot find the required file.
no such file or directory, lstat ‘/usr/lib/jvm/…’ or ‘/usr/java’
Now this one is interesting. Why is it looking in those specific directories? Well someone (you) told it to look in those places for the required files.
Fundamentally, you’ve specified the wrong environment variables. The locations or your installation of JDK is just messed up. That’s all this error boils down to.
It’s simple but can make you lose it altogether.
Where internet guides failed me?
Now, we know why it happens, we know where the problem lies, so it should be easy to fix. Right?
WRONG.
JAVA_HOME is not meant to be set to /<installation dir>/jre or just any specified directory, but many guides on the internet are literally asking you to set it so.
How to setup Java Environment Variables:
You can setup the basic JAVA_HOME & PATH variables but since I also found setting something as JRE_HOME, I’ll also be including it too.
- JAVA_HOME : It is to point to your JDK installtion directory. (usually
/usr/lib/jvm/java-<version>-<distribution>
) - JRE_HOME: Points to the JRE directorty in the JDK (
/usr/lib/jvm/java-<version>-<distribution>/jre
) - PATH : To include the bin folders in the evironment PATH variable.
Terminal Commands: These are for linux, but can easily be translated to MacOS:
Replace /usr/lib/jvm/java-11-oracle
with your distribution’s installation directory.
- Setting up JAVA_HOME:
export JAVA_HOME=/usr/lib/jvm/java-11-oracle
- For JRE_HOME:
export JRE_HOME=/usr/lib/jvm/java-11-oracle/jre
- PATH:
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
Guides for installing JDK
While writing this blog, I found complete guides on properly installing any JDK and setting up the variables (On Linux).
While following the guide from DigitalOcean, remember to set JAVA_HOME to /usr/lib/jvm/java-<version>-<distribution>
and not to /usr/lib/jvm/java-<version>-<distribution>/jre/java.