java.lang.ClassNotFoundException 尽管使用了 CLASSPATH 环境变量 [英] java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable

查看:22
本文介绍了java.lang.ClassNotFoundException 尽管使用了 CLASSPATH 环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 windows7 上使用 java 连接到 mysql 数据库.尽管在 CLASSPATH 中添加了 jdbcdriver jar 文件的完整 url,java.lang.ClassNotFoundException: com.mysql.jdbc.Driver被抛出.谁能告诉我我在这里缺少什么?如果我在项目库中添加 jar 文件,它会起作用,但我想通过 CLASSPATH 本身来完成.我的类路径看起来像这样-C:jython2.5.1javalibmysql-connector-java-5.1.12-bin.jar

I am trying to connect to mysql database using java on windows7. In spite of adding the complete url of jdbcdriver jar file in CLASSPATH, java.lang.ClassNotFoundException: com.mysql.jdbc.Driver is thrown. Could anyone tell me what i am missing here? It works if I add the jar file in project library but I want to do it by CLASSPATH itself. My classpath looks like this- C:jython2.5.1javalibmysql-connector-java-5.1.12-bin.jar

我想明确表示这不是我正在从事的实际项目.我实际上将 Django 与 Jython 一起使用,这需要 JDBC 驱动程序才能访问数据库.这就是为什么我必须只使用 CLASSPATH 的原因.

I want to make it clear that this is not the actual project i am working on. I am actually using Django with Jython, which requires the JDBC driver to access the database. That is the reason why I have to do it using CLASSPATH only.

推荐答案

CLASSPATH 环境变量java.exe 使用命令,甚至只有在没有任何-cp-classpath-jar参数时使用.它被 Eclipse、Netbeans 和 IDEA 等 IDE忽略.

The CLASSPATH environment variable is only used by the java.exe command and even then only when used without any of the -cp, -classpath, -jar arguments. It is ignored by IDEs like Eclipse, Netbeans and IDEA.

该环境变量在现实世界中也被认为是一种糟糕的做法,因为它破坏了可移植性.IE.程序 X 将成功运行,而程序 Y 在不更改 CLASSPATH 的情况下将无法运行.它只是有用"的Sun Oracle 防止初学者在学习 Java 教程时厌倦在 -cp-classpath 参数中一遍又一遍地输入相同的类路径.在现实世界中,批处理/shell 文件是首选,其中只指定了带有 -cp/-classpath 参数的整个命令.

That environment variable is in real world also considered a poor practice since it breaks portability. I.e. program X will run successfully while program Y won't run without altering the CLASSPATH. It's only "useful" for Sun Oracle to prevent that starters get tired of typing the same classpath again and again in the -cp or -classpath arguments when following Java tutorials. In real world, batch/shell files are preferred where just the entire command with -cp/-classpath argument is specified.

就您而言,您使用的是 IDE.类路径在那里称为构建路径".在普通 Java 项目中,它表示编译时和运行时类路径.您可以在项目的属性中对其进行配置.您可以添加一个完整的文件夹,您可以添加单个/外部 JAR 文件,您可以链接项目等.好好利用它.忘记使用 CLASSPATH 环境变量.这是Sun Oracle 的错误.他们想说服初学者,但结果却让初学者更加困惑,因为他们错误地将该环境变量解释为 类路径.

In your case you're using an IDE. The classpath is there called the "Build Path". In plain Java projects, it represents both the compiletime and runtime classpath. You can configure it in the project's properties. You can add a complete folder, you can add individual/external JAR files, you can link projects, etcetera. Make use of it. Forget about using the CLASSPATH environment variable. It was a mistake by Sun Oracle. They thought to convince starters, but it ended up to be only more confusing to starters as they incorrectly interpret that environment variable as the classpath.

这篇关于java.lang.ClassNotFoundException 尽管使用了 CLASSPATH 环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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