您如何使用 Java 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/825521/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:55:32  来源:igfitidea点击:

How do you use a Java Library?

javajar

提问by Jenny

I'm trying to use an open source java library to visualize nodes and edges in a graph, but I'm completely lost.

我正在尝试使用开源 Java 库来可视化图中的节点和边,但我完全迷失了。

I have a bunch of jar files in a folder. Clicking on some of the jar files makes java swing windows pop open with graphs displayed. Clicking other jar files does nothing.

我在一个文件夹中有一堆 jar 文件。单击某些 jar 文件会使 java swing 窗口弹出并显示图形。单击其他 jar 文件没有任何作用。

If I figured that out, would I just stick the jar files in there with the other ones, or would that still not work?

如果我想通了,我是将 jar 文件和其他文件一起放在那里,还是仍然不起作用?

And if I ever figure out how to use these files, does that mean that I have to include them if I transfer my java project to another computer? How would I go about doing that?

如果我想出如何使用这些文件,是否意味着如果我将我的 Java 项目转移到另一台计算机,我必须包含它们?我该怎么做?

采纳答案by Tom

Have you included those libraries in your classpath?

您是否在类路径中包含了这些库?

If you are using eclipse, you could

如果您使用的是eclipse,则可以

Project - > properties -> Java build path ->addJar.

项目 -> 属性 -> Java 构建路径 -> addJar。

And the Jar file should be placed in a directory inside your workspace (lib/ for example)

并且 Jar 文件应该放在你的工作空间内的一个目录中(例如 lib/)

If you have to take your project to another computer, you could take these steps

如果您必须将项目带到另一台计算机,则可以执行以下步骤

  1. Before doing anything, export your project (as a Jar file, for example).
  2. Save it into your favorite drive (cd / usb drive/ diskette/ tape).
  3. On "the other" computer, you can import this project into your workspace
  1. 在做任何事情之前,导出您的项目(例如,作为 Jar 文件)。
  2. 将其保存到您最喜欢的驱动器(CD / USB 驱动器 / 软盘 / 磁带)。
  3. 在“另一台”计算机上,您可以将此项目导入您的工作区

回答by Jonathan Adelson

I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. Figuring out the classpath can be confusing, but you can just set it when you start your jvm. Your IDE may have options for it, too.

我相信如果你将 jars 放在你的类路径中,你可以像导入和使用标准库一样导入和使用类。弄清楚类路径可能会令人困惑,但是您可以在启动 jvm 时设置它。您的 IDE 也可能有它的选项。

Most java problems are classpath problems.

大多数java问题都是类路径问题。

回答by Yishai

You use it by including it in the classpath of your java application, that way you can reference it from your code. Hereis a starter document. The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). It is definitely a little complicated, but it is very worth knowing.

您可以通过将它包含在 Java 应用程序的类路径中来使用它,这样您就可以从代码中引用它。是一个入门文档。JDK 1.6 有一些更简单的选项(例如将多个 jar 文件指定为 *.jar)。这肯定有点复杂,但非常值得了解。

回答by JeeBee

You should have documentation for these Jars. Some sounds like examples, but one must be the core graph modelling and rendering Jar. Hopefully the examples have source included.

你应该有这些 Jars 的文档。有些听起来像例子,但其中一个必须是核心图建模和渲染 Jar。希望示例包含源代码。

Just add that Jar to your project in Eclipse (e.g., in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. You can also use Eclipse to look inside the Jar file.

只需将该 Jar 添加到 Eclipse 中的项目中(例如,在项目中的 /lib 文件夹中,然后将其添加到构建路径中)并使用文档来使用代码。您还可以使用 Eclipse 来查看 Jar 文件的内部。

Unless there is no alternative, it probably isn't worth using a load of third party code that isn't documented at least on the API level, and without any source examples definitely not.

除非别无选择,否则可能不值得使用至少在 API 级别没有记录的第三方代码负载,并且没有任何源示例绝对不值得。

回答by McDowell

In Eclipse, you need to add libraries to the project build path.

在 Eclipse 中,您需要将库添加到项目构建路径中

In general, you need to provide dependencies via the classpath mechanisms at compile time and runtime. The precise mechanisms vary, but, for example, if you used the javaccompiler, you would provide your libraries on the command line:

通常,您需要在编译时和运行时通过类路径机制提供依赖项。确切的机制各不相同,但是,例如,如果您使用javac编译器,您将在命令行上提供您的库:

javac -classpath C:\dir\lib1.jar;C:\dir\lib2.jar foo/MyClass.java

These dependencies would also be required to invoke the app:

调用应用程序也需要这些依赖项:

java -classpath C:\dir\lib1.jar;C:\dir\lib2.jar;. foo.MyClass

This pagegives some good info, though googling for the term "classpath" should provide alternative sources.

这个页面提供了一些很好的信息,虽然谷歌搜索术语“类路径”应该提供替代来源。