Java 如何在 Eclipse 项目中添加 Apache POI 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/605181/
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
How Can I Add the Apache POI Library in and Eclipse Project?
提问by
I download the Apache POI from apache.org but don't know how use it with one of my existing project in Eclipse.
我从 apache.org 下载了 Apache POI,但不知道如何将它与我在 Eclipse 中的现有项目之一一起使用。
回答by jjnguy
You can find the option to add external jars to a project in: Project > Properties > Java Build Path > Libraries tab.
您可以在以下位置找到将外部 jar 添加到项目的选项:项目 > 属性 > Java 构建路径 > 库选项卡。
Click on 'Add External Jar' and find where you have the POI library.
单击“添加外部 Jar”并找到您拥有 POI 库的位置。
回答by Scott Stanchfield
There are several ways, some good, some bad...
方法有很多,有好有坏……
Add external Jar - as jjnguy says will work. However, nota good option... The problem is that if you share your project with someone else and they have the jar in a different spot, they'll get build path errors
Add variable - similar to add external jar, but much more flexible. You can define a classpath variable (under Java prefs) that represents the dir containing jar. In your project, when you add variable, you choose the var you defined and press "extend" to specify the actual jar. As long as you and other developers of your project define the variable, you can have the jar in different spots if you like
Add the jar to your project - create a dir in your project (optional, maybe call it lib) and import the jar into it using File->Import or if your OS supports, drag it as an icon into the folder (On windows I open an explorer window and drag it from there into the folder in eclipse). Then, right-click the jar and select Build-Path->Add to build path.
Add the jar in a separate project and reference it. Add the jar as mentioned in the last bullet to a separate project for that jar. Choose Build-Path->Configure Build path from the project. Go to the "order and export" tab and make sure the jar is checked. Now you can configure your project to reference the new project. This makes things more modular.
Create a user library - under Java prefs you can define a "user library" that contains the jar. This is somewhat similar to a classpath variable, but can reference multiple jars and you only have to add it once to the project.
Create a classpath container plugin. This is more complex, but is a nice option if you are providing a set of plugins for folks. A classpath container can be added like a library to a project and it can manage the jars, even searching for them if you want it to.
添加外部 Jar - 正如 jjnguy 所说的那样会起作用。但是,这不是一个好的选择...问题是,如果您与其他人共享您的项目并且他们将 jar 放在不同的位置,他们将收到构建路径错误
添加变量 - 类似于添加外部 jar,但更加灵活。您可以定义表示包含 jar 的目录的类路径变量(在 Java 首选项下)。在您的项目中,当您添加变量时,您选择您定义的变量并按“扩展”来指定实际的 jar。只要您和项目的其他开发人员定义了变量,您就可以根据需要将 jar 放在不同的位置
将 jar 添加到您的项目中 - 在您的项目中创建一个目录(可选,可以将其称为 lib)并使用 File->Import 将 jar 导入其中,或者如果您的操作系统支持,将其作为图标拖到文件夹中(在 Windows I打开一个资源管理器窗口并将其从那里拖到 eclipse 中的文件夹中)。然后,右键单击 jar 并选择 Build-Path->Add to build path。
在单独的项目中添加 jar 并引用它。将最后一个项目中提到的 jar 添加到该 jar 的单独项目中。从项目中选择 Build-Path->Configure Build path。转到“订购和导出”选项卡并确保选中 jar。现在您可以配置您的项目以引用新项目。这使得事情更加模块化。
创建一个用户库 - 在 Java 首选项下,您可以定义一个包含 jar 的“用户库”。这有点类似于类路径变量,但可以引用多个 jar,您只需将它添加到项目中一次。
创建一个类路径容器插件。这更复杂,但如果您为人们提供一组插件,这是一个不错的选择。类路径容器可以像库一样添加到项目中,它可以管理 jar,如果需要,甚至可以搜索它们。