Table of Contents

This page is in the 'old' namespace, and was imported from our previous wiki. We recommend checking for more up-to-date information using the search box.

Using Eclipse with Greenstone3

Most users may find the Alternative method for setting up Eclipse for use in Greenstone 3 easier than the method below and we recommend trying this first.

For a walkthrough with screen shots of the steps involved in installing Greenstone 3 by using the Eclipse plugin Subclipse (for checking out Greenstone 3 code from SVN), see Eclipse project of Greenstone 3 code using Subclipse.

NOTE: I suggest that you do not bookmark that link, because it is likely to change in the future. This page will host the correct link to wherever that document may be found in future. Therefore, bookmark this page instead.

In this document, $GS3SRCHOME is the full path to the top-level folder of your Greenstone 3. Wherever you see $GS3SRCHOME on this wiki page, it means you need to replace it with the full path to your top-level Greenstone 3 folder.

See the section Eclipse Java Basics if you want to learn how to

Eclipse project for Greenstone 3 developers

This is for those who will be developing Greenstone 3 source code.

Creating and compiling an Eclipse project by importing Greenstone 3 source files

Greenstone 3 code:

org.greenstone.gsdl3
Rightclick Project > Import > General > File system > Next.
Give it the directory: $GS3SRCHOME/src/java/org/greenstone/gsdl3
Then "select all". 
In the field "Into folder" <project-name>, browse for the newly created package structure. 
Tick "create selected folders only". 
Click Finish. And it will make copies of all the source code.

(General information: the following are some of the jar files that the compilation is dependent on. But as some of them are only generated upon executing the Ant building scripts, they won't all be available at this stage for us to add them to the Build Path of the project.)

- $GS3SRCHOME/web/WEB-INF/lib


- $GS3SRCHOME/lib/jni/


- Also add $GS3SRCHOME/web/xml-apis.jar and $GS3SRCHOME/packages/tomcat/common/lib/servlet-api.jar

If you want to compile and run just the GLI code: Greenstone 3 code comes with the GLI code in its gli folder. However, sometimes you may not want to obtain and compile every part of Greenstone 3 and would just like to work on GLI.

Rightclick Project > Import > General > File system > Next.
Give it the directory $GS3SRCHOME/gli/src/org/greenstone/gatherer
Then "select all". 
In the field "Into folder" <project-name>, browse for the newly created package structure. 
Tick "create selected folders only". 
Click Finish. And it will make copies of all the source code.
$GS3SRCHOME/gli/lib

Creating an Eclipse project of Greenstone 3 source code from SVN using Subclipse

This section deals particularly with Greenstone (3) source code.

Checking out Greenstone 3 code from SVN and creating an Eclipse project with it

http://svn.greenstone.org/main/trunk/greenstone3

Greenstone 3 code needs to be compiled using the Ant build file. See the section Compiling

Compiling

Compiling Greenstone 3 code from within Eclipse

This section is actually thoroughly unnecessary as you could do all this from the command-line as follows:

$ cd $GS3SRCHOME (which is /full/path/to/your/Eclipse/workspace/GS3/greenstone3)
$ ant prepare
$ ant install

To startup, stop and restart the Tomcat web server:

ant start
ant stop
ant restart

For those trying to avoid the command prompt, you can also set all this up so that it works from Eclipse.

To get ant prepare to work:

prepare

Now you've created a build (compile) configuration and will run Greenstone's ant prepare. A dialog will pop up asking you whether the port number for the tomcat web server to listen on is OK. Click OK if 8080 is alright.

You can run the ant prepare build configuration whenever you want to from now on.

To get ant install to work:

install

Now you've created an ant install configuration. The first time you do it, it will call Greenstone 3's ant install. You can repeat it whenever you want by going to Run > External Tools > ANT_INSTALL.

Hopefully you're seeing the pattern.

Let's create similar configurations for ANT_START which starts the Tomcat webserver for viewing Greenstone collections in the browser:

Repeat the above steps to create configurations for ANT_STOP and ANT_RESTART to stop and restart Tomcat. Executing these via Run > External Tools > ANT_<whatever> and then pointing the browser to where Tomcat is listening for Greenstone requests should produce the desired effect.

Compiling just Greenstone 3's GLI

GLI's src folder is checked out with greenstone3 (during ant prepare) and should be visible in Eclipse if you go to greenstone3/gli/src in the Package View on the left. (That is, if you had named your greenstone folder 'greenstone3'.)

Compiling and running gli from the x-term: First go to the greenstone3 folder and set up the environment for Greenstone

$ cd $GS3SRCHOME
$ source gs3-setup.sh

Now go to gli and compile then run it:

$ cd gli
$ ./makegli.sh
$ ./gli.sh

Finally, if you want to update the pre-compiled jar file for gli, containing your updated code, you'd type:

$ ./makejar.sh

Compiling from within Eclipse requires us to go through slightly different steps to what was described in Compiling Greenstone 3 code checked out from SVN in Eclipse when creating a configuration for makegli, because there is no ant file to build it. There is however a regular script, called makegli.sh for Linux and makegli.bat for Windows.

This will make gli. And the configuration will be available whenever you want to use it again in future (Run > External Tools > MAKEGLI).

You can create a MAKEJAR configuration script that runs makejar.sh/makejar.bat in a similar manner.

Before we can run GLI, the Greenstone environment has to be set up. We have to source gs3-setup.sh from the command-line, as I can't find out how to do this from Eclipse:

$ cd $GS3SRCHOME
$ source gs3-setup.sh

Now that the environment is set up for GLI, we can create a configuration in Eclipse for running GLI and run it: Repeat the steps above, but

In future, when you want to run GLI from Eclipse, you just go through Run > External Tools > GLI. (You might have to source gs3-setup.sh from the command prompt before that though.)

Debugging Greenstone with Eclipse (Stepping through code)

By setting up Greenstone3 code in Eclipse and then setting up Eclipse for remote debugging we are able to use the Eclipse debugging perspective to step through the Java code as it executes. In order to do this the build.xml file must be edited and a Remote Java Application Debugging Configuration must be set up.

Editing build.xml

In order to edit build.xml correctly:

<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=$GSDL3HOME -DGSDLOS=$GSDLOS -DPATH=$PATH -Xmx400M "/>
<!-- 
<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=$GSDL3HOME 
	-DGSDLOS=$GSDLOS -DPATH=$PATH -Xmx400M "/>
-->
<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=$GSDL3HOME -DGSDLOS=$GSDLOS -DPATH=$PATH -Xmx400M "/>
<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=$GSDL3HOME 
	-DGSDLOS=$GSDLOS -DPATH=$PATH -Xmx400M ${remote.opts} "/>
<property name="remote.opts" value="-Xdebug -Xrunjdwp:transport=dt_socket,address=8383,server=y,suspend=n"/>

The number 8383 can be any valid port number but will conflict with another application already using the same number, including the Greenstone Server. So try choose a number well clear of anything. Whatever number you choose, remember it as it will be referenced when you create the Remote Debugging Configuration in eclipse.

You should now have a section in the text file that looks very similar to the following:

<property name="remote.opts" value="-Xdebug -Xrunjdwp:transport=dt_socket,address=8383,server=y,suspend=n"/>
<!-- 
<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=$GSDL3HOME 
	-DGSDLOS=$GSDLOS -DPATH=$PATH -Xmx400M "/>
-->
<property name="catalina.opts" value="-Djava.util.prefs.syncInterval=2000000 -DGSDL3HOME=$GSDL3HOME 
	-DGSDLOS=$GSDLOS -DPATH=$PATH -Xmx400M ${remote.opts} "/>

Note: Having the remote.opts property included in the cataline.opts property seems to sometimes cause errors when Greenstone3 server starts and a debugger is not attempting to connect to it. This is why the commented version of the catalina.opts property is present. Whenever you are not debugging using eclipse you should switch the comment over to the version of catalina.opts that references remote.opts. Even if you where to find that the server ran fine even with the remote.opts property being used you should still do this as according to http://wiki.apache.org/tomcat/FAQ/Developing you will still get both security and performance issues.

Creating a Remote Java Application Debugging Configuration

To setup a Remote Debugging Configuration:

Running the Remote Debugger

Once you have made the required modifications to the build.xml file, have created the Remote Debugging Configuration and set break points you are ready to start debugging:

Note: After ant start is run with the remote.opts property being used it will poll for a maximum period of time waiting for Eclipse to attach. If you are not fast enough in running the Remote Debug Configuration than you may miss the window of opportunity. Simply run ant stop and start this step again.

Method 2: Debugging Greenstone 3 with Eclipse

Instructions and modifications to build.xml based on Debugging a servlet with tomcat and Eclipse tutorial and the Stackoverflow question What are the Steps for Debugging Java Web Application?

1. Set up Eclipse by creating a new configuration:

2. There's a new target that's been added to GS3: debug-start-tomcat. This will start up tomcat's catalina for debugging in Eclipse, and its debugger will communicate with Eclipse on host:port localhost:8000 by default. (Again: change build.xml if there are any conflicts with the port and/or host).

ant debug-start-tomcat

You can stop tomcat as usual with: ant stop.

3. Back in Eclipse:

4. In a browser:

5. In Eclipse:

Alternatively, go to menu Window > Open Perspective > Debug. Otherwise, in the top right of Eclipse there are the perspective shortcut buttons, so choose the Debug button to open the Debug perspective there and toggle back and forth at will between the Java Perspectives and Debug Perspective.

6. Once you're done debugging: Go back to your x-term and type:

ant stop

to stop tomcat.

Eclipse project for programs using Greenstone 3 code

OR: What you need to do to be able to import Greenstone 3 classes and packages into your code.

If you write a program that uses methods of Greenstone 3 classes, it may require one or more of the following:

The Greenstone 3 jar files you may need are all located in

- $GS3SRCHOME/web/WEB-INF/lib/
- $GS3SRCHOME/lib/jni

You may also need the following two jar files:

- $GS3SRCHOME/web/xml-apis.jar
- $GS3SRCHOME/packages/tomcat/common/lib/servlet-api.jar (this is if you 
ever need to include java's library for Servlets)

Add any jars the compiler complains about into your Build Path configuration, as described in Build Path and Run Configurations: including jars, paths and files and specifying cmd-line or VM arguments. Not all of them may be necessary. The most important one–containing the Greenstone 3 specific source code–is gsdl3.jar located in $GS3SRCHOME/web/WEB-INF/lib/

The location of the jar files is the same as listed for compiling above. Not all of the jars may be necessary. Add the jar files needed to execute your program to the Run configuration of your executable class(es), as described in Build Path and Run Configurations: including jars, paths and files and specifying cmd-line or VM arguments

Note that run-time errors when certain libraries are missing can be less clear than compile-time errors. But if an error message says something about "wsdl", you'll know that you need to include the similarly named jar for wsdls.

The *.properties files you'll be needing tend to all be located in $GS3SRCHOME/web/WEB-INF/classes

You need to specify the folder in which they are located, not the actual files themselves. See Build Path and Run Configurations: including jars, paths and files and specifying cmd-line or VM arguments for instructions on how to do this.

If executing your class causes log4j "appender" warnings, your executable class' run configuration needs to include the path to log4j.properties. If GS3 terminates "unable to open file global.properties", you need to specify the path to global.properties.

-Djava.library.path=/full/path/to/GS3SRCHOME/lib/jni

See Build Path and Run Configurations: including jars, paths and files and specifying cmd-line or VM arguments

Eclipse Java Basics

Creating an Eclipse Java project

File > New Project > Java > Java Project.
Click Next. 
Give your project a name.
Make sure the correct JRE is set (for example, Java 5/JDK 1.5* or Java 4/JDK 1.4.*).
Click Finish. 
Choose Yes to open Java perspective. (You can open this 
at any time through Window > Open Perspective > Java)
Right click on your project in the Package view on the left
Choose New > Package.
Type a directory structure where the path is all-lowercase and 
separated by full stops '.' rather than directory slashes:
e.g. org.greenstone.myapplicationpackage.mypackage1
Click OK.

For each package you want to create, you can follow the above step.

Rightclick on the package in which you want to create a new class in the Package view on the left.
Choose New > Class.
Give it a name.
  * If it has a main function, tick the check box.
  * If it inherits from something other than Java's Object, browse for it.

Build Path and Run Configurations: including jars, paths and files and specifying cmd-line or VM arguments

To add jars into your Build Path configuration:

Rightclick on your class/package/project in the Package view on the left of the workspace.
Choose Build Path > Configure Build Path
Click on the Libraries tab
Click on Add External Jars
Browse to the location of the library jar files you need to include
Select all the jars in that folder that you want to add and click OK.
Click OK.

Repeat for other folders containing jar files you wish to include.

This specifies the command-line arguments, jar files and paths to folders and files that need to be included in the classpath for an executable Java class. You can have more than one executable class, and configure the run-time configuration for each.

First, rightclick on an executable class in the Package view on the left of the workspace.
Choose Run As > Run...
Decide if it is going to run as a Java Applet or Java Application and double click on one of these.

Now that it's created a basic run-time configuration for you, you need to specify any command-line arguments and any jar files, other files or folders to include in the run-time classpath for your Java executable.

So, in the same Run configuration dialog,

The VM arguments field is for specifying library files like *.so (on Linux), *.dll (on Windows).

Click the Classpath tab in the Run configuration dialog
Click on your project folder name under User Entries
You can now:
  * Click the Add External Jars button and browse to the jars you want to add. Click OK. 
  * Click Advanced > Add External Folder button to browse to a folder path containing
the *.properties files you want to include. Click OK. 
Click Run to execute your program now that your run configuration is set up.

Using the Subclipse plugin in Eclipse to work with SVN

It lets you download code from a code repository, commit changes back into the repository and update local code to reflect changes in the repository.

Installation

Detailed installation instructions

Installation in brief for those behind a firewall:

Name: Subclipse 1.2.x (Eclipse 3.2+)
URL: /full/path/to/site-1.2.4.zip
Name: Subclipse 1.2.x (Eclipse 3.2+)
URL: $SUBCLIPSE/site-1.2.4

See also Eclipse project of Greenstone 3 code using Subclipse for a walkthrough with images.

Creating an Eclipse project of some source code from SVN and compiling it

This section will hopefully help beginners in using Subclipse to import any source code from an SVN repository into Eclipse.

We'll try an example with the Greenstone GLI code repository. To be able to run GLI you actually need more than the source code (you need the script that sets the GS3SRCHOME environment variable, for instance), but we are focussing here on the <i>general case</i>: how to download Java source code from some SVN code repository and get it to compile through Eclipse.

The particulars of getting Greenstone 3 (including GLI) source code via SVN into Eclipse and getting it all to compile and run is explained in Creating an Eclipse project of Greenstone 3 source code from SVN using Subclipse and Compiling

1. With Eclipse open in a new workspace:

In our example here, we'll go with the first. But the process will be similar if you checked out Greenstone 3 code.

2. It will now complain that certain import statements can't be resolved by marking various packages and classes with red crosses in the left-hand pane's Package view. To resolve this, we need to checkout and include the jars in the lib folder:

See also Eclipse project of Greenstone 3 code using Subclipse for a walkthrough with images.

Troubleshooting

Package errors once checked out a SVN trunk?

This section contributed by bjn8:

Subclipse help

Help on Subclipse is available from within Eclipse itself, as stated at the Subclipse documentation page

So go to Eclipse's Help and you'll see that SVN is one of the options in the left-hand pane of help.

The most useful help sections include:

Alternative method for setting up Eclipse for use in Greenstone 3

Method 1

Loading the gs3 eclipse project file into Eclipse

An Eclipse project file is part of the GS3 svn checkout. It consists of a .project file, with associated .classpath file. Once you've checked out a GS3 from SVN and compiled it up with an ant prepare command followed by an ant install command, open up Eclipse.

In Eclipse, create a new workspace, and import this project file

To achieve the same from scratch:

Creating a Greenstone project that can be reused with other fresh Greenstone checkouts from SVN. The src/java and gli/src folders, which contain java code, will be set up for automatic compilation in Eclipse. These instructions are from coderanch Use File > Import and select General > Existing Projects into Workspace. Click next and then browse to the GS3 top level directory as this contains the eclipse project file.

If you have any build errors, it means it's probably missing some newer library jar files. In that case, update the .classpath file.

Build Greenstone before opening Eclipse

In an x-term:

svn co http://svn.greenstone.org/main/trunk/greenstone3 greenstone3

Setting up Eclipse

In an x-term

This will reveal the .project and .classpath files that Eclipse generated and placed in your greenstone 3 installation

Method 2

This method is much simpler than the first method described above but is slightly more restrictive. Rather than needing to use Subclipse to check out Greenstone inside Eclipse, this method allows you to use a pre-existing Greenstone SVN installation.

In order fix all the errors and allow the project to build correctly you will also need to complete the following steps.

Press OK.

You can now edit the Java files and make use of the advanced IDE features as you would normally be able to. This approach does not work very well when you need to change the name of a file or add a new file. In these cases you will probably want to make the modifications directly on the file system and then re-import the specific files affected.

See Useful Eclipse Shortcuts which also links to the autoformatting file we hope to use as a standard for Greenstone Java code.

Creating Run configurations in Eclipse

1. Open the GS3 .project Eclipse project file in Eclipse.

2. In the Java perspective view, navigate down to executable java file and right-click on it. E.g. rightclick on the TransformingLibrary.java in src/java/org/greenstone/gsdl3 http://stackoverflow.com/questions/8625060/store-run-configuration-with-project-in-eclipse

3. Choose Run As > Run Configurations.

4. Set up the env variables and arguments to the executable.

5. Store the Run configuration with the project in Eclipse, if you need to commit the launch configuration to SVN. The instructions as explained at Store run configuration with project in Eclipse:

Open "Run" then "Run Configurations...", and select your configuration. Click on the last tab, "Common". Under "Save As", choose "Shared File" instead of "Local File".
This dialog is a little confusing. The line next to "Shared File" can only be a path, not a file name - and it defaults to the root path of the current project. 
Once you hit "Apply", a file named the [Run Configuration Name] + ".launch" is created within that folder. 
E.g., this results in a "/JavaTest/TransformingLibrary.launch" file. This is something that can be committed to version control.