Java,如何在Netbeans中添加库文件?

25

我对Netbeans IDE和Java很新。我有一个Java项目,显示了许多编译错误:

can not import "org.apache.commons.logging.Log"

有人能帮我解决这些错误吗?我该如何在Netbeans IDE中添加库文件?

4个回答

47

NetBeans 6.8 快速解决方案。

在项目窗口中右键单击缺少库的项目名称 -> 属性 -> 项目属性窗口打开。 在类别树中选择“库”节点 -> 在项目属性窗口右侧点击“添加JAR /文件夹”按钮 -> 选择所需的jar包。

您还可以查看我的简短视频教程


2
视频帮了我 :)谢谢 - Tomas Bisciak
4
如果您选择包含所有 jar 文件的文件夹而不是每个 jar 文件单独选择,那么它们就不会被加载。 - Gurnard
我们刚刚创建的新库保存在哪里? - wib

7
如何将commons库导入NetBeans中。
  1. Evaluate the error message in NetBeans:

    java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    
  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

二进制文件不包含源代码,因此您将无法深入了解并查看调试时发生了什么。作为程序员,您应该下载apache commons的源代码并从源代码编译,自己生成jar文件,并导入那些有经验的jar。您应该足够聪明,能够理解和纠正您导入的源代码。这些古老版本的apache commons可能是在较旧版本的Java下编译的,因此如果您回溯得太远,它们甚至可能无法编译,除非您在古老的Java版本下编译它们。

4

在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文件。单击“打开”并单击“确定”。库已加载!


1

针对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   
}

保存、清理构建并运行应用程序。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接