[英]error: java.lang.ClassNotFoundException: org.postgresql.Driver
I'm just trying to connect to postgres (13.0.1) from java (11.0.8) on Debian 10 and get the error below:我只是想从 Debian 10 上的 java (11.0.8) 连接到 postgres (13.0.1) 并得到以下错误:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class cli_test {
public static void main(String[] args) {
try {
Connection connection = null;
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/myproj", "postgres", "<pwd>");
System.out.println("Connection established successfully");
} catch (Exception e) {
System.out.print("error: \n" + e.toString() + "\n" + e.getMessage());
}
}
}
I run these commands:我运行这些命令:
CLASSPATH=/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
javac -verbose cli_test.java
java cli_test
I have verified that the jar file exists in that location and is set to 755 permissions.我已验证该 jar 文件存在于该位置并设置为 755 权限。
I get this error:我收到此错误:
java.lang.ClassNotFoundException: org.postgresql.Driver
I have spent hours searching online and found many others with this problem.我花了几个小时在网上搜索,发现很多其他人也有这个问题。 No other solutions I found have fixed it.
我找到的其他解决方案都没有修复它。 I'm trying to do this without a java ide, just a text editor.
我正在尝试在没有 java ide 的情况下执行此操作,而只是一个文本编辑器。
That's not a Java problem but you use bash in a wrong way.这不是 Java 问题,而是您以错误的方式使用 bash。 Setting
CLASSPATH=..
will not make this environment variable available to java
in the program call afterwards.设置
CLASSPATH=..
不会使此环境变量在之后的程序调用中可供java
使用。 Try尝试
export CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
javac -verbose cli_test.java
java cli_test
or或者
CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar java cli_test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.