英文:
error: java.lang.ClassNotFoundException: org.postgresql.Driver
问题
我只是尝试从Debian 10上使用Java(11.0.8)连接到PostgreSQL(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("成功建立连接");
} catch (Exception e) {
System.out.print("错误:\n" + e.toString() + "\n" + e.getMessage());
}
}
}
我运行了以下命令:
CLASSPATH=/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
javac -verbose cli_test.java
java cli_test
我已验证该jar文件位于该位置并且设置为755权限。
我得到了这个错误:
java.lang.ClassNotFoundException: org.postgresql.Driver
我已经花了几个小时在网上搜索,并找到许多遇到此问题的其他人。我找到的其他解决方案都没有解决这个问题。我想在没有Java集成开发环境的情况下完成这个,只使用一个文本编辑器。
英文:
I'm just trying to connect to postgres (13.0.1) from java (11.0.8) on Debian 10 and get the error below:
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.
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.
答案1
得分: 1
以下是您要求的翻译内容:
这不是Java的问题,而是您错误地使用了bash。设置 CLASSPATH=..
不会使这个环境变量在程序调用后对 java
可用。尝试使用以下命令:
export CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
javac -verbose cli_test.java
java cli_test
或者
CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar java cli_test
英文:
That's not a Java problem but you use bash in a wrong way. Setting CLASSPATH=..
will not make this environment variable available to java
in the program call afterwards. 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论