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

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

问题描述

我已经将我的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不能正确地添加到classpath中.仅当可搜索的类路径中不存在该类时,才会引发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的libs中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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