我对Netbeans IDE和Java很新。我有一个Java项目,显示了许多编译错误:
can not import "org.apache.commons.logging.Log"
有人能帮我解决这些错误吗?我该如何在Netbeans IDE中添加库文件?
我对Netbeans IDE和Java很新。我有一个Java项目,显示了许多编译错误:
can not import "org.apache.commons.logging.Log"
有人能帮我解决这些错误吗?我该如何在Netbeans IDE中添加库文件?
NetBeans 6.8 快速解决方案。
在项目窗口中右键单击缺少库的项目名称 -> 属性 -> 项目属性窗口打开。 在类别树中选择“库”节点 -> 在项目属性窗口右侧点击“添加JAR /文件夹”按钮 -> 选择所需的jar包。
您还可以查看我的简短视频教程。
Evaluate the error message in NetBeans:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
NoClassDeffFoundError means somewhere under the hood in the code you used, a method called another method which invoked a class that cannot be found. So what that means is your code did this: MyFoobarClass foobar = new MyFoobarClass()
and the compiler is confused because nowhere is defined this MyFoobarClass. This is why you get an error.
To know what to do next, you have to look at the error message closely. The words 'org/apache/commons' lets you know that this is the codebase that provides the tools you need. You have a choice, either you can import EVERYTHING in apache commons, or you could import JUST the LogFactory class, or you could do something in between. Like for example just get the logging bit of apache commons.
You'll want to go the middle of the road and get commons-logging. Excellent choice, fire up the google and search for apache commons-logging
. The first link takes you to http://commons.apache.org/proper/commons-logging/. Go to downloads. There you will find the most up-to-date ones. If your project was compiled under ancient versions of commons-logging, then use those same ancient ones because if you use the newer ones, the code may fail because the newer versions are different.
You're going to want to download the commons-logging-1.1.3-bin.zip
or something to that effect. Read what the name is saying. The .zip means it's a compressed file. commons-logging means that this one should contain the LogFactory class you desire. the middle 1.1.3 means that is the version. if you are compiling for an old version, you'll need to match these up, or else you risk the code not compiling right due to changes due to upgrading.
Download that zip. Unzip it. Search around for things that end in .jar
. In netbeans right click your project, click properties, click libraries, click "add jar/folder" and import those jars. Save the project, and re-run, and the errors should be gone.
在Netbeans 8.2中
1. 从网站下载二进制文件。 Apache Commos 在以下网址:[http://commons.apache.org/components.html][1] 在这种情况下,您必须在Components菜单中选择“Logging”,然后按照Releases部分中的链接下载。直接URL: [http://commons.apache.org/proper/commons-logging/download_logging.cgi][2] 对我来说,正确的下载文件是Binaries中的commons-logging-1.2-bin.zip。
2. 解压已下载的内容。现在,您可以看到从zip文件创建的目录中有几个jar文件。
3. 将库添加到项目中。右键单击项目,选择Properties,然后点击左侧的Libraries。单击“Add Jar/Folder”按钮。转到先前未压缩的内容并选择适当的jar文件。单击“打开”并单击“确定”。库已加载!
针对Netbeans 2020年9月版本,JDK 11。
(仅建议在Gradle项目中使用)
1. 在项目的src/main/java
文件夹中创建一个libs
文件夹
2. 将所有库jar文件复制粘贴到其中
3. 在项目根目录下的files
选项卡中打开build.gradle
4. 更正主类(我的是mainClassName = 'uz.ManipulatorIkrom'
)
5. 在dependencies
中添加下一个字符串:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'
description = 'testing netbeans'
mainClassName = 'uz.ManipulatorIkrom' //4th step
repositories {
jcenter()
}
dependencies {
implementation fileTree(dir: 'src/main/java/libs', include: '*.jar') //5th step
}