安装 Java 库
由于我对 Java 很陌生,我想知道安装新库的正确过程(那些在我的 linux dist 存储库中不可用)。 我应该把它们放在哪里?以及如何安装它们? 例如,我下载了 openCsv (http://opencsv.sourceforge.net/),但我没有知道如何安装它。
As I'm quite new to Java, I would like to know the proper procedure of installing new libraries (those that are no available in my linux dist repositories).
Where should I place them? and how to install them?
For instance, I downloaded openCsv (http://opencsv.sourceforge.net/), and I have no idea how to install it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将它们存储在任何您想要的地方。您可以将它们存储在 JRE 分发目录中,但我不建议这样做。
相反,我会按每个项目存储它们(这样您就可以轻松地为每个项目拥有不同的版本 - 有些库的每个版本有不同的名称,有些则没有)并采用诸如
lib/
目录。这样你就可以拥有可以以相同方式操作的标准构建脚本(Ant 等)(如果你使用 Maven,那么每个项目都有一个标准位置 -src/main/resources
)You can store them wherever you wish. You can store them within the JRE distribution directories, but I wouldn't recommend that.
Instead I would store them per-project (so you can have different versions for each project easily - some libraries have different names for each version, some don't) and adopt a standard such as a
lib/
directory. That way you can have standard build scripts (Ant etc.) that can operate in the same way (if you're using Maven, then there's a standard place per-project -src/main/resources
)您可以使用 Maven 来管理对这些库的任何依赖项。
Maven会自动下载所有需要的JAR文件并将它们放在本地存储库中(位置是可配置的)。
这使得升级到各种库的新版本变得非常容易,因为您只需声明您想要的版本,Maven 就会完成其余的工作。
请注意:Maven 是需要习惯的东西,并且最初的学习曲线很陡峭。
如果你把一切都设置正确并且 Maven 负责编译、打包、分发、站点创建、发布管理等等,那么回报就会到来。
You could use Maven to manage any dependencies to those libraries.
Maven will automatically download all needed JAR files and put them in a local repository (the location is configurable).
This makes upgrading to new versions of various libraries very easy as you just declare the version you want and Maven does the rest.
Beware: Maven is something to get used to and the initial learning curve is steep.
The rewards come if you have everything set up properly and maven takes care of compiling, packaging, distribution, site creation, release management etc. etc. etc.
Java 库并不需要像其他应用程序那样“安装”。您需要做的就是将 jar 文件放在特定位置,并将 jar 文件添加到类路径中。如何执行此操作取决于您使用的 Linux 发行版。如果您在 eclipse 中制作 Web 应用程序,则可以将 .jar 文件放入 WebRoot/web-inf/lib 文件夹中,它将与您的项目捆绑在一起。
Java libraries don't really need to be 'installed' like other applications. All you need to do is put the jar file in a specific location, and add the jar file to your classpath. How you do that depends on the linux distro you are using. If you are making a web application in eclipse, you can drop the .jar file in the WebRoot/web-inf/lib folder, and it will be bundled in with your project.
确保在 $CLASSPATH 环境变量中设置放置库的路径。
对于 Eclipse:项目 ->属性-> Java 构建路径 ->添加 JAR...
Be sure that the path, which you place the libaries at, is set in the $CLASSPATH Environment Variable.
For Eclipse: Project -> Properties -> Java Build Path -> Add JARs...
这实际上取决于您 - 我使用
/opt/javalib
,但您也可以考虑使用/usr/local
中的目录。It's up to you really - I use
/opt/javalib
, but you might consider a directory in/usr/local
as well.