如何在 Java 中使用 3rd 方包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6374161/
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 to use 3rd party packages in Java
提问by n0pe
I'm developping my first Java application which actually needs a 3rd party package and now I'm lost as to how to actually use it. The packages I need are from VLCJso that I can embed a media player in my GUI.
我正在开发我的第一个 Java 应用程序,它实际上需要一个 3rd 方包,现在我不知道如何实际使用它。我需要的包来自VLCJ,以便我可以在我的 GUI 中嵌入媒体播放器。
Usually, I can just import packages and classes, but is this possible with 3rd party packages? They have a .jar file to download at their website, are the packages stored in that? And if so, how do I go about using them in my own application?
通常,我只能导入包和类,但是这可以用于 3rd 方包吗?他们有一个 .jar 文件可以在他们的网站上下载,包是否存储在其中?如果是这样,我该如何在我自己的应用程序中使用它们?
采纳答案by planetjones
You just need the third party JAR to be on your project's classpath. What IDE are you using? In Eclipse you would do:
您只需要第三方 JAR 位于您项目的类路径中。你用的是什么IDE?在 Eclipse 中,您将执行以下操作:
Go to Package Explorer window on the left. Select the Java project you are working on and right click. Click Properties. Then click Java Build Path. Click Add External Jars.
转到左侧的 Package Explorer 窗口。选择您正在处理的 Java 项目并右键单击。单击属性。然后单击 Java 构建路径。单击添加外部罐子。
Or you could modify your system wide CLASSPATH to include the JAR. Or you can do it on the command line e.g.
或者您可以修改系统范围的 CLASSPATH 以包含 JAR。或者您可以在命令行上执行此操作,例如
java -classpath C:\java\thirdpartjars\thirdparty.jar MyProgram
(you can use the argument with javac
too).
(您也可以使用参数 with javac
)。
There are many ways to crack this nut.
破解这个坚果的方法有很多。
回答by planetjones
Yes, the JAR file you download is an archive (basically a .zip file) of compiled .class files which you can then import into your own application.
是的,您下载的 JAR 文件是已编译的 .class 文件的存档(基本上是 .zip 文件),然后您可以将其导入到您自己的应用程序中。
The only thing is you need to add the .jar file to your application's classpath for you to use it before you can import it.
唯一的问题是您需要将 .jar 文件添加到应用程序的类路径中,以便在导入之前使用它。
I would suggest looking at a good Java book or tutorial (for example, the official Java tutorial) as this is all stuff that should be covered.
我建议看一本好的 Java 书籍或教程(例如,官方 Java 教程),因为这些都是应该涵盖的内容。
回答by antlersoft
You need to add the jar file to the search path of javac when you compile your project; and you need to make the jar available at runtime-- it needs to be in the classpath of the java process that runs your program.
编译项目时需要将jar文件添加到javac的搜索路径中;并且您需要在运行时使 jar 可用 - 它需要位于运行您的程序的 java 进程的类路径中。
If you are using an IDE, you usually update these paths in the project settings.
如果您使用的是 IDE,通常会在项目设置中更新这些路径。