error: java.lang.ClassNotFoundException: org.postgresql.Driver

huangapple go评论109阅读模式
英文:

error: java.lang.ClassNotFoundException: org.postgresql.Driver

问题

  1. 我只是尝试从Debian 10上使用Java11.0.8连接到PostgreSQL13.0.1),并且出现了以下错误
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. public class cli_test {
  6. public static void main(String[] args) {
  7. try {
  8. Connection connection = null;
  9. Class.forName("org.postgresql.Driver");
  10. connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/myproj", "postgres", "<pwd>");
  11. System.out.println("成功建立连接");
  12. } catch (Exception e) {
  13. System.out.print("错误:\n" + e.toString() + "\n" + e.getMessage());
  14. }
  15. }
  16. }
  17. 我运行了以下命令
  18. CLASSPATH=/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
  19. javac -verbose cli_test.java
  20. java cli_test
  21. 我已验证该jar文件位于该位置并且设置为755权限
  22. 我得到了这个错误
  23. java.lang.ClassNotFoundException: org.postgresql.Driver
  24. 我已经花了几个小时在网上搜索并找到许多遇到此问题的其他人我找到的其他解决方案都没有解决这个问题我想在没有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:

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. public class cli_test {
  5. public static void main(String[] args) {
  6. try {
  7. Connection connection = null;
  8. Class.forName(&quot;org.postgresql.Driver&quot;);
  9. connection = DriverManager.getConnection(&quot;jdbc:postgresql://localhost:5432/myproj&quot;, &quot;postgres&quot;, &quot;&lt;pwd&gt;&quot;);
  10. System.out.println(&quot;Connection established successfully&quot;);
  11. } catch (Exception e) {
  12. System.out.print(&quot;error: \n&quot; + e.toString() + &quot;\n&quot; + e.getMessage());
  13. }
  14. }
  15. }

I run these commands:

  1. CLASSPATH=/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
  2. javac -verbose cli_test.java
  3. java cli_test

I have verified that the jar file exists in that location and is set to 755 permissions.

I get this error:

  1. 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 可用。尝试使用以下命令:

  1. export CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
  2. javac -verbose cli_test.java
  3. java cli_test

或者

  1. 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

  1. export CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
  2. javac -verbose cli_test.java
  3. java cli_test

or

  1. CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar java cli_test

huangapple
  • 本文由 发表于 2020年10月23日 04:28:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64490071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定