尽管使用了 CLASSPATH 环境变量,但 java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2591505/
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-13 09:35:18  来源:igfitidea点击:

java.lang.ClassNotFoundException in spite of using CLASSPATH environment variable

javaenvironment-variablesclasspathclassnotfoundexception

提问by Nitin Garg

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

我正在尝试在 windows7 上使用 java 连接到 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 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.

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

采纳答案by BalusC

The CLASSPATHenvironment variable is onlyused by the java.execommand and even then only when used withoutany of the -cp, -classpath, -jararguments. It is ignoredby IDEs like Eclipse, Netbeans and IDEA.

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

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 SunOracle to prevent that starters get tired of typing the same classpath again and again in the -cpor -classpatharguments when following Java tutorials. In real world, batch/shell files are preferred where just the entire command with -cp/-classpathargument is specified.

该环境变量在现实世界中也被认为是一种糟糕的做法,因为它破坏了可移植性。即程序 X 将成功运行,而程序 Y 在不更改CLASSPATH. 对于SunOracle 来说,它只是“有用”,可以防止初学者在遵循 Java 教程时厌倦在-cpor-classpath参数中一遍又一遍地键入相同的类路径。在现实世界中,批处理/shell 文件是首选,其中只指定了带有-cp/-classpath参数的整个命令。

In your case you're using an IDE. The classpath is there called the "Build Path". In plain Java projects, it represents boththe 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 CLASSPATHenvironment variable. It was a mistake by SunOracle. They thought to convince starters, but it ended up to be only more confusing to starters as they incorrectly interpret that environment variable as theclasspath.

在您的情况下,您使用的是 IDE。类路径在那里称为“构建路径”。在普通的Java项目,它代表双方在编译时和运行时类路径。您可以在项目的属性中对其进行配置。您可以添加一个完整的文件夹,您可以添加单个/外部 JAR 文件,您可以链接项目等。好好利用它。忘记使用CLASSPATH环境变量。这是SunOracle的错误。他们认为说服首发,但最终仅被更混乱到起动机,因为他们错误地解释这个环境变量类路径。

See also:

也可以看看:

回答by ValGe

What finally helped me out was to copy the mysql-connector-java-5.1.15-bin.jar to \jre\lib and to \jre\lib\ext both(!) even though I did all the classpathing circus Java offers :) Environment was pure notepad/commandline though.

最终帮助我的是将 mysql-connector-java-5.1.15-bin.jar 复制到 \jre\lib 和 \jre\lib\ext both(!) 即使我做了所有类路径马戏 Java 提供的: ) 环境是纯粹的记事本/命令行。

回答by nandan

What worked with me using Netbeans was: Run > Set Project Configuration > Customize. Under Libraries > Add Library. Added MySQL JDBC Driver (I assume it appeared in list because I copied the jar file to the jre\lib\ext folder. And it worked seamlessly.

使用 Netbeans 对我有用的是:运行 > 设置项目配置 > 自定义。在库 > 添加库下。添加了 MySQL JDBC 驱动程序(我假设它出现在列表中,因为我将 jar 文件复制到了 jre\lib\ext 文件夹。它无缝地工作。

I tried setting classpath but that did not work. I am using Netbeans 7.0

我尝试设置类路径,但这不起作用。我正在使用 Netbeans 7.0

回答by omid

simply do a right click on your project in "Netbeans" select properties then click on "libraries " then click on "add library..." button then select "MySQL JDBC Driver" and click on "add library" button then on "OK" button

只需在“Netbeans”中右键单击您的项目,选择属性,然后单击“库”,然后单击“添加库...”按钮,然后选择“MySQL JDBC 驱动程序”并单击“添加库”按钮,然后单击“确定” “ 按钮

回答by Dharmik Chauhan

In Netbeans IDE just Check the properties of Project on which you working on,in properties window go to 'library' tag, in diolog box just add your mysql-connector-java-**.jar file.

在 Netbeans IDE 中,只需检查您正在处理的项目的属性,在属性窗口中转到“库”标签,在对话框中添加您的 mysql-connector-java- **.jar 文件。

回答by JPB

I had this same problem in Netbeans. Because I was using a tomcat connection pool as defined in context.xml I needed to add the jdbc jar to both the project (Properties->Libraries) and to the lib/ folder within my Tomcat server so it could be seen on startup.

我在 Netbeans 中遇到了同样的问题。因为我使用的是 context.xml 中定义的 tomcat 连接池,所以我需要将 jdbc jar 添加到项目 (Properties->Libraries) 和 Tomcat 服务器中的 lib/ 文件夹中,以便在启动时可以看到它。

回答by Jay Patel

  1. Open Netbeans IDE
  2. Right-click your Project.
  3. Select Properties.
  4. On the left-hand side click Libraries.
  5. Under "Compile" tab - click Add Jar/Folder button.
  6. Select Downloaded "mysql-connector-java-5.1.25-bin.jar" file (Download Connector/Jfrom dev.mysql.com)
  7. Click OK
  8. Run Again... Its work.
  1. 打开 Netbeans IDE
  2. 右键单击您的项目。
  3. 选择属性。
  4. 在左侧单击库。
  5. 在“编译”选项卡下 - 单击添加 Jar/文件夹按钮。
  6. 选择下载的“mysql-connector-java-5.1.25-bin.jar”文件(从dev.mysql.com下载Connector/J
  7. 单击确定
  8. 再次运行...它的工作。

回答by Kyle

I also had this problem before, but after I put/added mysql-connector-java-5.1.34-bin.jar (Download it from here) into the apache-tomcat-8.0.15\lib folder, and then ran my project, it really did work.

我之前也遇到过这个问题,但是在我将 mysql-connector-java-5.1.34-bin.jar(从这里下载)放入/添加到 apache-tomcat-8.0.15\lib 文件夹中,然后运行我的项目之后,它确实奏效了。

Note : Even after adding the jar file the error persists, then restart the Tomcat server and rerun you project again.

注意:即使添加 jar 文件后错误仍然存​​在,然后重新启动 Tomcat 服务器并再次重新运行您的项目。

回答by Searene

If you are using maven, add the dependency to pom.xmlshould solve the problem.

如果您使用的是 maven,添加依赖pom.xml应该可以解决问题。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.36</version>
</dependency>