Java 加载 postgreSQL JDBC 驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19216072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:08:40  来源:igfitidea点击:

Loading the postgreSQL JDBC driver

javapostgresqljdbc

提问by ivantxo

I am trying to load a JDBC postgreSQL driver for a Java program. I know this is all over the Internet. I have tried many solutions, but none of them have worked for me.

我正在尝试为 Java 程序加载 JDBC postgreSQL 驱动程序。我知道这在互联网上无处不在。我尝试了很多解决方案,但没有一个对我有用。

The problem is that I get this error:

问题是我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError:    
classes/com/freire/test/JDBCExample/class
Caused by: java.lang.ClassNotFoundException: classes.com.freire.test.JDBCExample.class
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

And my code looks like this:

我的代码如下所示:

package com.freire.test;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class JDBCExample 
{
    public static void main(String[] argv) 
    {
        System.out.println("JDBC Connection Testing");
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("PostgreSQL JDBC Driver not included!");
        }
    }
}

And the structure of my project looks like this:

我的项目结构如下所示:

myProject
 src
   com
     freire
       test
         JDBCExample.java
 classes
   com
     freire
       test
         JDBCExample.class
 lib
   postgresql-9.2-1003.jdbc3.jar

Compiling works fine:

编译工作正常:

java -d classes/ src/com/freire/test/JDBCExample.java

But executing produces the error:

但是执行会产生错误:

java classes/com/freire/test/JDBCExample

Worth to say that I am working on a OS X Mountain Lion.

值得一提的是,我正在开发 OS X Mountain Lion。

Any help will be appreciated.

任何帮助将不胜感激。

采纳答案by Juned Ahsan

Firstly you need to mention the package names using .instead of /while running the java program:

首先,您需要在运行 java 程序时使用.而不是提及包名称/

Go to your classes directory and run JDBCExample as :

转到您的类目录并运行 JDBCExample 为:

java com.freire.test.JDBCExample

But it will now cry for the postgres driver class not found because postgres jar is missing in the classpath.So you need to use the classpath option while running the program and add your postgres jar to the classpath:

但是现在它会因为找不到 postgres 驱动程序类而哭泣,因为类路径中缺少 postgres jar。所以你需要在运行程序时使用 classpath 选项并将你的 postgres jar 添加到类路径中:

for windows:

对于窗户:

java -cp .;../lib/postgresql-9.2-1003.jdbc3.jar com.freire.test.JDBCExample

for linux:

对于Linux:

java -cp .:../lib/postgresql-9.2-1003.jdbc3.jar com.freire.test.JDBCExample

回答by MadProgrammer

You need to ensure that the postgresql-9.2-1003.jdbc3.jar is within the class path when you compile and run the program

编译运行程序时需要保证postgresql-9.2-1003.jdbc3.jar在类路径内

Try using

尝试使用

javac -cp lib/postgresql-9.2-1003.jdbc3.jar -d classes/ src/com/freire/test/JDBCExample.java

to compile the application and

编译应用程序和

java -cp lib/postgresql-9.2-1003.jdbc3.jar;./classes com.freire.test.JDBCExample

to run it...

运行它...

nbAs Juned has pointed, technically, you don't need the lib/postgresql-9.2-1003.jdbc3.jarreferences in the classpath, but consider it an demonstration of how to included compile time dependencies within the complication process ;)

nb正如 Juned 所指出的,从技术上讲,您不需要lib/postgresql-9.2-1003.jdbc3.jar类路径中的引用,但将其视为如何在复杂过程中包含编译时依赖项的演示;)

回答by Rohit_chd

On Linux execute following:

在 Linux 上执行以下操作:

javac -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest.java

javac -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest.java

java -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest

java -cp '.:postgresql-9.1-901.jdbc4.jar' postgresjavatest

It will jdbc drivers for you. make sure jar file is on same location

它将为您提供 jdbc 驱动程序。确保 jar 文件在同一位置