Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Source: https://jena.apache.org/tutorials/using_jena_with_eclipse.html Preamble Jena is a programmer's API (application programming interface) for Java semantic web applications. It isn't a program or tool that you run - if that is what you are looking for, I would suggest perhaps TopBraid Composer as a good option. So the primary use of Jena is to help you write Java code that handles RDF and OWL documents and descriptions. I use Eclipse as my Java development of choice, other environments are available, but I don't use them and so the experience I can pass along is based on Eclipse. Prerequisites You will need to: 1. Download and install eclipse. This article was written with Eclipse 3.3.1, but the exact version number should not matter as the main Eclipse user interface has been stable for a while. 2. Download and unzip Jena. At the time of writing, the current version of Jena is 2.5.5, but again it won't matter if you use a different version. I'm going to use Windows for this tutorial, but the same steps apply to Linux and, I presume, MacOS. In fact, on my Windows machine I use a directory structure similar to Linux: rather than c:\Documents and Settings\ijd as the base directory, I use c:\home\ijd. This is a personal choice that makes it easier to write scripts that run on Linux and Cygwin on Windows, but there's no special reason for you to do the same. However, it is often a good idea to avoid path names with spaces in them if you can. So my Jena installation is in c:\home\ijd\projects\jena2. Wherever you see this path in the notes below, substitute the location where you have installed your own copy of Jena. Step-by-step guide This tutorial really is aimed at Eclipse newcomers, so I'm going to take it a step at a time. Feel free to skip ahead if you have some experience with Eclipse already. 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: I need to o create a new Java project: Next I ne eed to set up the e internal structu ure of the projecct. There are many schools of thought on this. I usually u use a ba asic structure tha at is taken from Maven. Source ccode is in src/m main, with Java code c in src/mai in/java, ontolog gies insrc/main n/owl, etc. Compiled Java class s files go into target/classes. So when I create a new project, I edit the defaults s to reflect these e preferences. H However, if the defaults suit you, or you don't kno ow enough to ca are about those details ernatively, if yourr project or educcational institutio on has suggested guidelines, the en go with those e. yet, just accept the default settings. Alte Step 2: create the hello world class c Java cod de is stored in filles that correspo ond to the declaration of a Java class, so I need d to create a clas ss for my hello world w example. In the project explorer pane (wh hich is on the lefft by default, butt you can move it around in Eclipse), I select the e project's Java source folder an nd right-click to get the context menu to show: Step 3: adding the e Jena libraries OK, so n now let's write so ome Jena code. The first thing I'll need to work w with is a Model: a container for RDF statements s. TheModel clas ss is in package e com.hp.hpl.jena.rdf.model l, so I'll first impo ort that class, an nd then create an instance of it. OK, so why the red wavy lines? These are Eclipse's way of indicating a problem. If I tried to compile this code using javac on 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: Click new w to create a new w user-library: Now I cliick on add jars tto add the .jar files from Jena. .jar files conta ain the compiled Java libraries th hat Jena uses, to ogether with the e Jena code itself in jena.jar. The JAR sselection dialogu ue that pops up a allows me to select which .jar files are in my user-library. I hav ve selected all of o the .jar files in i the lib/ / directory of my y Jena install dire ectory: Result: That's acctually enough to allow me to us se Jena in Eclipsse, but there's a couple of additiional optional ste eps that make programming p a bit b easier. I can tell t Eclipse w where to find the e source code and the javadoc ffor the Jena classses (I'll show ho ow that's helpfull later on). Next to the jena.jar r entry in the use er library, there's a little + icon. Clicking that expan nds the details o of the .jar entryy: I can telll Eclipse that the e source code is s in the src/ fold der of my Jena in nstall directory. I click on the So ource attachmen nt line, then the Edit... E button. In the source a attachment configuration dialogu ue, I click the Exxternal folder buttton and then bro owse to the right location: Similarlyy, I can notify Ecclipse of the loca ation of the Javad doc by first selecting the Javado oc location line of o the library entry, then following a similar proce ess. Notice here that the loca ation path is a URL (it starts fil le:). This is because the locatio on can also be a Javadoc web siite, though I'm not using that capability here. With the Jena user library configured, I click c OK to close e the library configuration dialog gue. Step 4 4: Finishing tthe hello worrld program Now I ca an go back to myy project, and co onfigure the Java use the library I just created. To start, I right-click k on the project node in the explorer a build path to u window tto bring up the p project properties menu, navigatte to the build pa ath menu option and add the library: Having u updated the build d path, Eclipse will w automaticallyy rebuild the pro oject (i.e. recomp pile the Java cod de). With that, so ome of the errorrs will go away, since the impo ort statement ca an now find the class to be impo orted, and so the e Model class na ame is meaningfful to the compiler. However, there is still a remaining error because ModelFactory is not d defined. What is s needed is a suitable importsta atement. This is easily fixed in Eclipse, E either byy clicking on the error symbol (th he red 'x' on the left margin), or b by positioning th he cursor just aftter the ModelFac ctory class nam me, and pressing g ctrlspace, w which triggers Ecclipse to show th he possible com mpletions for the name: When I sselect the first off the presented options o (i.e. Mod delFactory rath her than ModelFa actoryBase), Ec clipse will fill in the t import statem ment automatica ally: Look, ma a, no errors! Eclipse'ss auto-complete feature is also useful when add ding code. For exxample, if I type Resou followed by ctrl-space I g get the possible completions tha at match that nam me: Notice th he Javadoc com mment in yellow on o the candidate e completion (Re esource in this case). c This extra a information com mes from having g added the source code and java adoc locations w when I specified the t library in step 2, above. If yo ou miss out spec cifying the sourc ce code and java adoc, Eclipse can't be so helpful in describin ng possible auto o-completions. Itt also will affect tthe debugging vview, though I'm not discussing that t in this article e. Here is tthe completed hello world progra am: Step 5 5: running the e hello world d program To run th his program with hin Eclipse, I use e the run menu, accessed from the button show wing a white trian ngle on a green circle. c Since I ha aven't run any co ode yet, I have to ttell Eclipse whatt program to run n. Click on the drrop-down menu to the right of th he run button, an nd tell Eclipse to run HelloRDFWorld as a Java application: Which gives the followin ng output in the Eclipse E console:: And thatt's it: from emptyy Eclipse to a wo orking RDF hello o-world application. Obviously th here is lot's more e to learn (see th he tutorial links at the top for som me jumping--off points), but h hopefully you ca an now have fun n developing new w semantic web applications!