August 16, 2007

Developing Java for Domino in Eclipse

I try to develop all agents and script libraries in Java rather than lotusscript nowadays. The notes java (from the lotus.domino library) covers many of the functions that lotusscript offers and besides that you have the whole Java library itself which many times makes it more powerful than lotusscript.

The notes client is not the best Java editor though. The type ahead functionality is not there as it is for lotusscript and the lack of color coding makes the code a bit difficult to read. Once you have tried to develop Java using Eclipse you get spoiled and it really is a great thing to be able to develop java applications (like agents and script libraries) for Domino in the Eclipse client.

Here's how to do it:
- Assuming you have a functional Eclipse client, create a new project (for example 'Notes Java') and open the properties for that project.
- Click on 'Java Build Path'
- Click on tab 'Libraries' and press button 'Add External JARs'
- Find 'Notes.jar' and click on open to add the special notes java classes to the build path, which makes it possible to use them in the Eclipse client. (I have the notes 7 client installed and the path was 'C:\Program\lotus\notes\jvm\lib\ext\Notes.jar')



- Create a package in the project and under the package create a class called for example 'JavaAgent'.
- Change 'Superclass' to 'lotus.domino.AgentBase'.



- Now start writing Java code in the Eclipse client as you normally would in the notes client. Copy and paste the skeleton code that always comes with a new java agent. Here's a test agent that uses a notes view to loop through all documents and prints a field value for each document.

package com.ekakan.NotesJava.agents;
import lotus.domino.*;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {
Session session = getSession();

AgentContext agentContext = session.getAgentContext();

Database db = agentContext.getCurrentDatabase();
View view = db.getView("actions");
Document doc = view.getFirstDocument();

while (doc != null) {
if (doc.isValid()) {
System.out.println(doc.getItemValueString("action"));
}
doc = view.getNextDocument(doc);

}

} catch(Exception e) {
e.printStackTrace();

}
}
}


- To run the code, first compile it in the Eclipse client to create a class-file, i.e. 'JavaAgent.class'.
- Import the class file to a new notes agent using the 'Imported Java' option.



- Save and use in what way you would like to. Whenever a change is made to the java file in the eclipse client the class file has to be imported into the notes client again of course.
- Another option instead of importing the class file is simply to copy and paste the code from eclipse to notes.

One thing that you might come across when trying to run the agent in the notes client is an unpleasant error message on the Java console saying something like this:

java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0

This means that the JDK used to compile with is of a newer version than the version that the application is running against. Which in this case means that your Eclipse proabably uses version 5.0 or 6.0 while Notes uses 1.4. Check on the Domino console for the JVM version running using the command 'Show JVM'.
In Eclipse, open the properties for the project and click on 'Java Compiler'. In the JDK section, set the correct compiler compliance level, in this case 1.4.





Technorati tags:
, , ,


1 comment:

Anonymous said...

A better way would be to bring in the source from your Eclypse workspace by choosing Java instead of Imported Java and then click on the Edit Project button and navigate to the source that you have stored on your Workspace. When you save the agent, it compiles with whatever java runtime that the Domino server is using.