[Solved] JPype Error: FileNotFoundError: [Errno 2] No such file or directory: ‘/usr/lib/jvm’

The project deployed online has been running normally for one year, and this morning suddenly reported FileNotFoundError: [Errno 2] No such file or directory:’/usr/lib/jvm’ error.

JPype is a tool that allows python code to easily call Java code, thus overcoming the shortcomings of python in some areas (such as server-side programming).
The actual operating environment of JPype is still the python runtime, but an embedded jvm is started during the runtime.

Cannot find jvm, probably because the environment variables are not in effect.

Usually you can run java directly on the command line because the bin directory where java is located is added to the PATH, and it takes effect as an environment variable after exporting PATH. But JAVA_HOME is only used as a common variable. When using os.getenv() to get environment variables, JAVA_HOME cannot be found, so it is speculated that you should just add export in front of JAVA_HOME and restart the project.

Add export JAVA_HOME to the global configuration file /etc/profile or personal configuration file ~/.bashrc or ~/.bash_profile. The following is the setting of my /etc/profile:

# set java path
export JAVA_HOME =/usr/local/java/ latest
export PATH =${JAVA_HOME}/ bin:${PATH}
export CLASSPATH =.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/ tools.jar
Other notes: If you use PyCharm remote debugging, if you encounter the same error, you need to set the environment variables before importing pyhanlp, as follows

# Set environment variables
import os
os.environ[ ' JAVA_HOME ' ] = ' /usr/local/java/latest '

# Import JPype again to avoid the problem of not being able to find java
import jpype


Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *