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

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

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

问题

我只是尝试从Debian 10上使用Java11.0.8连接到PostgreSQL13.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(&quot;org.postgresql.Driver&quot;);
			
			connection = DriverManager.getConnection(&quot;jdbc:postgresql://localhost:5432/myproj&quot;, &quot;postgres&quot;, &quot;&lt;pwd&gt;&quot;);
			
            System.out.println(&quot;Connection established successfully&quot;);
        } catch (Exception e) {
            System.out.print(&quot;error: \n&quot; + e.toString() + &quot;\n&quot; + 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

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:

确定