简体   繁体   中英

Java: How to Integrate Other Software

I've always been impressed by the StackOverflow hive mind, and was hoping you could point me in the right direction here.

I've taken some courses in Java programming, and understand how to write a fairly complex Java program. However, I've never learned how to integrate others' software into my own programs.

For a new project, I'd like to integrate a part-pf-speech tagger and chunker into my code, but have no idea how to "load" these programs (if load is the correct term).

I'm certainly not looking for step-by-step instructions, but rather a guide for how to go about this sort of issue. If anyone could get me started in the right direction, I would greatly appreciate it.

Thanks, Adam

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.

You'll need their classes on your classpath when compiling, and again when running your program.

It appears that those projects distribute both src and jars. Grab the jars, and make them available on your classpath . Once your classpath is set up you'll need to import any specific classes/packages that you're using.

See this tutorial on managing the classpath . It covers the basics for command-line compilation/execution; if you're using a particular build system or IDE then the instructions will vary.

Also note the specific instructions at the second link for making data files available. For this they're also using the classpath .

Configure your build path to include the .jar files.

If you're using Eclipse, you would right click on the project file in the Project Explorer, then select Configure Build Path. Finally, add external archive (the ones you've downloaded). Now those functions will be readily available in your program.

Or a more robust way is to create a folder called "lib" in your eclipse project and include all jar files in there. Then from the Configure Build Path window you select those jar files in the lib folder. This makes it easy to share projects with other programmers regardless if they are on Windows or Linux (when adding external jar it saves the absolute path so if something on C:\\ will not be found on someone else's PC) Also provides nice integration of dependency libraries on source code managers like GIT, CVS and SVN.

Well typically Java libraries are distributed as a JAR file. Then in your program, you can simply import the new packages and use the provided API.

When you compile and run, you have to make sure that the libraries are included in your class path so they know where to look for the packages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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