Krishna Roy
Krishna Roy

Reputation: 203

Error: could not find libjava.so, Error: could not find Java 2 Runtime Environment

I uninstalled java j2sdk1.4.2_12 java on my Linux Server and Install jdk1.6.0_21, but when I am trying to run any script I am getting following Error

Error: could not find libjava.so 
Error: could not find Java 2 Runtime Environment.

I already set classPath and Path like :

In vi ~/.bashrc

export JAVA_HOME=/home/java/jdk1.6.0_21/
export PATH=$PATH:/home/java/jdk1.6.0_21/bin
export set CLASSPATH=/usr/java/j2sdk1.4.2_12/lib/mysql-connector-java-5.1.6-bin.jar:    /home/java/jdk1.6.0_21/jre/lib:/root/mis/mod:$CLASSPATH

ulimit -c unlimited

IN vi ~/.bash_profile

JAVA_HOME=/home/java/jdk1.6.0_21/
PATH=$PATH:$HOME/bin:$HOME/mis/mod:
PATH=/home/java/jdk1.6.0_21/bin:$PATH
CLASSPATH=$CLASSPATH:/home/java/jdk1.6.0_21/lib:.:
export JAVA_HOME
export PATH
export CLASSPATH
unset USERNAME

When I run following Commands

which java

/home/java/jdk1.6.0_21/bin/java

java -version

java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) Server VM (build 17.0-b16, mixed mode)

I am not getting where is the issue. Can Any one help me ?

Upvotes: 1

Views: 25019

Answers (3)

Yan QiDong
Yan QiDong

Reputation: 4441

As a normal Linux process, the java process will load libjava.so before JVM is really started.

There is a special so named libjli.so, which is directly linked to the java executable file. If java can not find the home of JVM, which is not specified by JAVA_HOME in this level, the place of libjli.so will be the lib path of java.

So, set LD_LIBRARY_PATH may solve this problem.


In a Linux Debian (ARM) system where Java 17 installed, let me show you the magic.

  1. Backup the java executable file to /opt/java for convenience.
  2. Move JRE to other place.
  3. Fix it with LD_LIBRARY_PATH.
  4. Break it again with libjli.so moved.

Backup java:

$ sudo cp /usr/bin/java /opt/java
$ /opt/java -version
openjdk version "17.0.11" 2024-04-16
OpenJDK Runtime Environment (build 17.0.11+9-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.11+9-Debian-1deb12u1, mixed mode, sharing)

Break it with jvm directory moved:

$ sudo mv /usr/lib/jvm /usr/lib/jvm.bak
$ /opt/java -version
/opt/java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory

Fix it:

$ export LD_LIBRARY_PATH=/usr/lib/jvm.bak/java-17-openjdk-arm64/lib
$ /opt/java -version
openjdk version "17.0.11" 2024-04-16
OpenJDK Runtime Environment (build 17.0.11+9-Debian-1deb12u1)
OpenJDK 64-Bit Server VM (build 17.0.11+9-Debian-1deb12u1, mixed mode, sharing)

Break it and never fixed:

$ sudo mv /usr/lib/jvm.bak/java-17-openjdk-arm64/lib/libjli.so /srv/
$ /opt/java -version
/opt/java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory

$ sudo ln -s /srv/libjli.so /usr/lib/jvm.bak/java-17-openjdk-arm64/lib/
$ /opt/java -version
Error: could not find libjava.so
Error: Could not find Java SE Runtime Environment.

$ export LD_LIBRARY_PATH=/srv
$ /opt/java -version
Error: could not find libjava.so
Error: Could not find Java SE Runtime Environment.

Upvotes: 0

Vignesh Menon
Vignesh Menon

Reputation: 9

I faced the same issue, and it disppeared by just shutting down the CentOS VM and then starting again. Then type in javac command again to check.

Upvotes: 0

user2829759
user2829759

Reputation: 3512

I encounter this issue before. It turns out there are 2 javac program sitting on my machine. They are in /usr/bin/javac and /bin/javac. My system used the /bin/javac one. The right path for me is /usr/bin/javac. Removing /bin/javac helped

Upvotes: 1

Related Questions