简体   繁体   English

你如何使用Java库?

[英]How do you use a Java Library?

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. 我在一个文件夹中有一堆jar文件。 Clicking on some of the jar files makes java swing windows pop open with graphs displayed. 点击一些jar文件会弹出java swing窗口并显示图形。 Clicking other jar files does nothing. 单击其他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? 如果我弄清楚如何使用这些文件,这是否意味着如果我将我的java项目转移到另一台计算机,我必须包含它们? How would I go about doing that? 我该怎么做呢?

I believe if you put the jars in your classpath, you can import and use classes just like you would a standard library. 我相信如果你把jar放在你的类路径中,你可以像标准库一样导入和使用类。 Figuring out the classpath can be confusing, but you can just set it when you start your jvm. 弄清楚类路径可能会令人困惑,但您可以在启动jvm时设置它。 Your IDE may have options for it, too. 您的IDE也可以选择它。

Most java problems are classpath problems. 大多数java问题都是类路径问题。

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). 在执行任何操作之前,请导出项目(例如,作为Jar文件)。
  2. Save it into your favorite drive (cd / usb drive/ diskette/ tape). 将其保存到您最喜欢的驱动器(CD / USB驱动器/软盘/磁带)。
  3. On "the other" computer, you can import this project into your workspace 在“另一台”计算机上,您可以将此项目导入工作区

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 javac compiler, 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 page gives some good info, though googling for the term "classpath" should provide alternative sources. 这个页面提供了一些很好的信息,虽然谷歌搜索术语“类路径”应该提供替代来源。

You use it by including it in the classpath of your java application, that way you can reference it from your code. 您可以通过将其包含在Java应用程序的类路径中来使用它,这样您就可以从代码中引用它。 Here is a starter document. 是一份入门文件。 The JDK 1.6 has some easier options (such as specifying multiple jar files as *.jar). JDK 1.6有一些更简单的选项(例如将多个jar文件指定为* .jar)。 It is definitely a little complicated, but it is very worth knowing. 这绝对有点复杂,但非常值得了解。

You should have documentation for these Jars. 你应该有这些罐子的文件。 Some sounds like examples, but one must be the core graph modelling and rendering Jar. 有些听起来像是例子,但必须是核心图形建模和渲染Jar。 Hopefully the examples have source included. 希望这些例子包括来源。

Just add that Jar to your project in Eclipse (eg, in a /lib folder in your project, then add it to the build path) and use the documentation to use the code. 只需将Jar添加到Eclipse中的项目中(例如,在项目的/ lib文件夹中,然后将其添加到构建路径中)并使用文档来使用代码。 You can also use Eclipse to look inside the Jar file. 您还可以使用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级别上没有记录的第三方代码,并且没有任何源代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM