eclipse中導入jena

eclipse中導入jena:

Step 1 create a Java project

Eclipse organizes files into projects, so we need a project for this tutorial. Depending on the plugins installed, Eclipse may show a large number of different types of project. A plain Java project is fine for a basic Jena application. If you want to, for example, develop a Java servlet that uses Jena, then choose a suitable project type.

For clarity, I'm starting with an empty Eclipse workspace:

Screenshot 0:

I need to create a new Java project:

Screenshot 1:

Screenshot 2:

Next I need to set up the internal structure of the project. There are many schools of thought on this. I usually use a basic structure that is taken from Maven. Source code is in src/main, with Java code in src/main/java, ontologies insrc/main/owl, etc. Compiled Java class files go into target/classes. So when I create a new project, I edit the defaults to reflect these preferences. However, if the defaults suit you, or you don't know enough to care about those details yet, just accept the default settings. Alternatively, if your project or educational institution has suggested guidelines, then go with those.

Screenshot 3:

Screenshot 4:

Step 2: create the hello world class

Java code is stored in files that correspond to the declaration of a Java class, so I need to create a class for my hello world example. In the project explorer pane (which is on the left by default, but you can move it around in Eclipse), I select the project's Java source folder and right-click to get the context menu to show:

Screenshot 5:

Screenshot 6:

Step 3: adding the Jena libraries

OK, so now let's write some Jena code. The first thing I'll need to work with is a Model: a container for RDF statements. TheModel class is in package com.hp.hpl.jena.rdf.model, so I'll first import that class, and then create an instance of it.

Screenshot 7:

OK, so why the red wavy lines? These are Eclipse's way of indicating a problem. If I tried to compile this code using javacon the command line, I'd get an error saying that the package com.hp.hpl.jena.rdf.model can't be found anywhere, and that the class Model isn't defined. On the command line, I would fix this by setting the Java classpath. Essentially, that's what I do in Eclipse too, but Eclipse makes it rather easier to do. Notice that I haven't actually said anywhere yet that this is a Jena project. All I've said is that it's a Java project. What's the difference? Simply this: Eclipse has to know where to find the Jena classes I would like to refer to from my program. Eclipse calls the locations where it can find the supporting code I want to refer to as the build path.

There are actually a few different ways of setting the build path in Eclipse. One way I could do it is to create a lib directory in my project top-level folder, then copy the Jena .jar files there, and then link that directory to my project's build path. That works, but there's a better way: defining a user library. A user library is a declaration of a library (collection of supporting code) that I can reference from any project. Once I have this set up once, I can use the same library definition in multiple different projects. Moreover, if I subsequently update Jena to a new release, then once the library is updated every project in my Eclipse workspace will see the new version. With the copy-files-to-the-lib-folder method, I have to re-copy to every project that uses Jena. Here's how I create the Jena user library, starting from the Preferences menu:

Screenshot 8:

Screenshot 9:

Click new to create a new user-library:

Screenshot 10:

Now I click on add jars to add the .jar files from Jena. .jar files contain the compiled Java libraries that Jena uses, together with the Jena code itself in jena.jar. The JAR selection dialogue that pops up allows me to select which .jarfiles are in my user-library. I have selected all of the .jar files in the lib/ directory of my Jena install directory:

Screenshot 11:

Result:

Screenshot 12:

That's actually enough to allow me to use Jena in Eclipse, but there's a couple of additional optional steps that make programming a bit easier. I can tell Eclipse where to find the source code and the javadoc for the Jena classes (I'll show how that's helpful later on). Next to the jena.jar entry in the user library, there's a little + icon. Clicking that expands the details of the .jar entry:

Screenshot 13:

I can tell Eclipse that the source code is in the src/ folder of my Jena install directory. I click on the Source attachment line, then the Edit... button. In the source attachment configuration dialogue, I click the External folder button and then browse to the right location:

Screenshot 14:

Similarly, I can notify Eclipse of the location of the Javadoc by first selecting the Javadoc location line of the library entry, then following a similar process. Notice here that the location path is a URL (it starts file:). This is because the location can also be a Javadoc web site, though I'm not using that capability here.

Screenshot 15:

With the Jena user library configured, I click OK to close the library configuration dialogue.

Step 4: Finishing the hello world program

Now I can go back to my project, and configure the Java build path to use the library I just created. To start, I right-click on the project node in the explorer window to bring up the project properties menu, navigate to the build path menu option and add the library:

Screenshot 16:

Screenshot 17:

Screenshot 18:

Having updated the build path, Eclipse will automatically rebuild the project (i.e. recompile the Java code). With that, some of the errors will go away, since the import statement can now find the class to be imported, and so the Model class name is meaningful to the compiler.

Screenshot 19:

However, there is still a remaining error because ModelFactory is not defined. What is needed is a suitable importstatement. This is easily fixed in Eclipse, either by clicking on the error symbol (the red 'x' on the left margin), or by positioning the cursor just after the ModelFactory class name, and pressing ctrl-space, which triggers Eclipse to show the possible completions for the name:

Screenshot 20:

When I select the first of the presented options (i.e. ModelFactory rather than ModelFactoryBase), Eclipse will fill in the import statement automatically:

Screenshot 21:

Look, ma, no errors!

Eclipse's auto-complete feature is also useful when adding code. For example, if I type Resou followed by ctrl-space I get the possible completions that match that name:

Screenshot 22:

Notice the Javadoc comment in yellow on the candidate completion (Resource in this case). This extra information comes from having added the source code and javadoc locations when I specified the library in step 2, above. If you miss out specifying the source code and javadoc, Eclipse can't be so helpful in describing possible auto-completions. It also will affect the debugging view, though I'm not discussing that in this article.

Here is the completed hello world program:

Screenshot 23:

Step 5: running the hello world program

To run this program within Eclipse, I use the run menu, accessed from the button showing a white triangle on a green circle. Since I haven't run any code yet, I have to tell Eclipse what program to run. Click on the drop-down menu to the right of the run button, and tell Eclipse to run HelloRDFWorld as a Java application:

Screenshot 24:

Which gives the following output in the Eclipse console:

Screenshot 25:

And that's it

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章