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

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

问题描述

我正在尝试使用java在windows7上连接到mysql数据库。尽管在CLASSPATH中添加了完整的jdbcdriver jar文件url,但是抛出了java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
。有人可以告诉我我在这里失踪了吗如果我在项目库中添加了jar文件,但是我想通过CLASSPATH本身来执行它,它将起作用。
我的类路径看起来像这样 -
C:\jython2.5.1\javalib\mysql-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.1\javalib\mysql-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 环境变量仅< c> c> 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.

环境变量在现实世界中也被认为是一种糟糕的做法,因为它破坏了可移植性。即程序X将运行成功,而程序Y不会运行而不更改 CLASSPATH 。对于 Sun Oracle来说,这只是有用的,以防止初学者厌倦在 -cp 或<$ c中一次又一次输入相同的类路径$ c> -classpath 参数下面的Java教程。在现实世界中,批处理/ 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项目中,它代表了 compiletime和运行时类路径。您可以在项目的属性中进行配置。您可以添加完整的文件夹,可以添加个人/外部JAR文件,可以链接项目等。利用它忘记使用 CLASSPATH 环境变量。这是Sun的一个错误。他们认为说服起动器,但最终只能让起动器更加混乱,因为他们错误地将该环境变量解释为 类路径。

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.

  • How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib

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

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