java.lang.ClassNotFoundException:com.mysql.jdbc.Driver(在 jre's 库中) [英] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (in jre's libs)

查看:75
本文介绍了java.lang.ClassNotFoundException:com.mysql.jdbc.Driver(在 jre's 库中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的 mysql-connector-java-5.1.18-bin.jar 添加到 jre 和 jdk 库中.这是:

I have added my mysql-connector-java-5.1.18-bin.jar to jre and jdk libs. And Here it is:

C:\Windows\system32>javap java.io.Bits
    Compiled from "Bits.java"
    class java.io.Bits extends java.lang.Object{
        java.io.Bits();
        static boolean getBoolean(byte[], int);
        static char getChar(byte[], int);
        static short getShort(byte[], int);
        static int getInt(byte[], int);
        static float getFloat(byte[], int);
        static long getLong(byte[], int);
        static double getDouble(byte[], int);
        static void putBoolean(byte[], int, boolean);
        static void putChar(byte[], int, char);
        static void putShort(byte[], int, short);
        static void putInt(byte[], int, int);
        static void putFloat(byte[], int, float);
        static void putLong(byte[], int, long);
        static void putDouble(byte[], int, double);
    }


    C:\Windows\system32>javap com.mysql.jdbc.Driver
    ERROR:Could not find com.mysql.jdbc.Driver

但是当我显示同一个文件的直接类路径时,就可以了.

But when I show a direct classpath to the same file, it is ok.

C:\Windows\system32>javap -classpath "B:\Java\Tools\mysql-connector-java-5.1.18\
mysql-connector-java-5.1.18\mysql-connector-java-5.1.18-bin.jar" com.mysql.jdbc.
Driver
Compiled from "Driver.java"
public class com.mysql.jdbc.Driver extends com.mysql.jdbc.NonRegisteringDriver i
mplements java.sql.Driver{
    public com.mysql.jdbc.Driver()       throws java.sql.SQLException;
    static {};
}

问题开始于我使用 Thilo 的答案 加载动态驱动.没有 IOException.但是在字符串 Class.forName(driver).newInstance() 我有 ClassNotFoundException 异常.将 jar 添加到 jre 后,没有任何变化.这有什么问题?

The problem started when I was using Thilo's answer to load driver dynamically. There is no IOException. But on the string Class.forName(driver).newInstance() I have ClassNotFoundException exception. After adding jar to jre, nothing has changed. What is wrong with that?

推荐答案

将 jar 添加到 jre 之后究竟是什么意思?我担心 mysql-connector-java-5.1.18-bin.jar 没有正确添加到类路径中.ClassNotFoundException 仅在可搜索的类路径中不存在该类时才会抛出.JDK 附带的所有 jar 都是引导程序类,可供 java 加载它们.然而,所有第三方类都需要在可搜索的系统或应用程序级类路径中设置,以便 java 可以加载指定的类参数.

What exactly do you mean by After adding jar to jre? I fear mysql-connector-java-5.1.18-bin.jar is not properly added to classpath. The ClassNotFoundException is only thrown when that class is not present in the searchable classpath. All the jars that come with JDK are bootstrap classes and are available for java to load them. All third party classes however need to be set in a searchable system or application level classpath so that java can load the specified class argument.

尝试在命令提示符下执行以下命令并执行您的 java 类.

Try following commands at your command prompt and execute your java class.

set mysqljar="absolute-path-to\mysql-connector-java-5.1.18-bin.jar"  
set classpath=%classpath%;.;%mysqljar%

只要这个 jar 在可搜索的类路径中可用,所有类加载器都可以从 jar 中找到并加载类.尝试此更改并运行 Thilo 的示例,它应该可以正常工作.

So long as this jar is available in searchable classpath, all class loaders can find and load class from the jar. Try this change and run Thilo's example and it should be working.

另外阅读更多关于类路径的信息,对于命令行,这里.

这篇关于java.lang.ClassNotFoundException:com.mysql.jdbc.Driver(在 jre's 库中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆