0

StackOverflow 的蜂巢思维一直给我留下深刻印象,并希望您能在这里为我指明正确的方向。

我参加了一些 Java 编程课程,并且了解如何编写一个相当复杂的 Java 程序。但是,我从来没有学会如何将别人的软件集成到我自己的程序中。

对于一个新项目,我想将part-pf-speech 标记器和分块器集成到我的代码中,但不知道如何“加载”这些程序(如果 load 是正确的术语)。

我当然不是在寻找分步说明,而是寻找如何解决此类问题的指南。如果有人能让我朝着正确的方向开始,我将不胜感激。

谢谢,亚当

4

4 回答 4

3

It looks like the externals you want to use are themselves in Java. This means you're in luck - you can use pure java language features to make it work.

There are two things to it:

1) your source files that interact directly with the external libraries have to be imported, or otherwise you'll have to refer to them using the fully qualified classname. Importing is done with the import statement. These statements should appear right before your class declaration, like so:

import foo.*;       //import all classes from the package foo
import foo.bar.Baz; //import only the Baz class from the package foo.bar

public class MyClass {
    Baz myBaz = null;               //declare a member of type Baz class from package foo.bar
    foo.bar.BazBaz myBazBaz = null; //by using a fully qualified classname, I didn't need to write an import statement for foo.bar.BazBaz  
}

2) when you compile your sources, the java compiler needs to know where to look for classes you referenced in your source. This is done via the classpath.

The classpath can be a list of just .class files (compiled java classes), but also .jar files (java archives) and .zip files. Typically a project will package all classes it needs in one or more .jar files.

The location of these classes have no bearing on the way you interact with them in java code. It's the compiler's job to read these jars and class files and locate the classes you referred to in your code. If the compiler can't locate the classes you're referring to, you will get a compile time error and you can't compile your program.

You can specify the classpath as an argument to the java compiler command line (http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#options). However, this becomes unwieldy very rapidly.

Instead, you should use a build tool like ant to do this work for you. The best way to get started is to read this page: http://ant.apache.org/manual/index.html.

From there, go to "Using apache ant" and then to "Writing a simple build file" in its entirety, they explain how to set up the classpath very well there.

于 2012-06-11T20:52:00.497 回答
2

classpath编译时和运行程序时都需要它们的类。

这些项目似乎同时分发了 src 和 jar。抓住罐子,让它们在你的classpath. 设置类路径后,您将需要使用的import任何特定类/包。

请参阅教程以管理classpath. 它涵盖了命令行编译/执行的基础知识;如果您使用的是特定的构建系统或 IDE,那么说明会有所不同。

另请注意第二个链接中有关使数据文件可用的特定说明。为此,他们还使用classpath.

于 2012-06-11T20:42:01.247 回答
1

配置构建路径以包含 .jar 文件。

如果您使用的是 Eclipse,您可以在 Project Explorer 中右键单击项目文件,然后选择 Configure Build Path。最后,添加外部存档(您已下载的存档)。现在,这些功能将在您的程序中随时可用。

或者更健壮的方法是在 eclipse 项目中创建一个名为“lib”的文件夹,并在其中包含所有 jar 文件。然后从 Configure Build Path 窗口中选择 lib 文件夹中的那些 jar 文件。这使得与其他程序员共享项目变得容易,无论他们是在 Windows 还是 Linux 上(添加外部 jar 时,它会保存绝对路径,因此如果 C:\ 上的某些内容不会在其他人的 PC 上找到)还提供了很好的依赖集成GIT、CVS 和 SVN 等源代码管理器上的库。

于 2012-06-11T20:43:33.903 回答
0

Java 库通常作为 JAR 文件分发。然后在您的程序中,您可以简单地导入新包并使用提供的 API。

当您编译和运行时,您必须确保库包含在您的类路径中,以便它们知道在哪里查找包。

于 2012-06-11T20:41:45.000 回答