Java Class Library - include as package?

I am used to using Lotuscript libraries in Agents, and I can understand java packages in “normal” java where it’s compiled from .java files in an IDE, but I’m struggling with trying to make a class library and use it in a java agent in a Notes database. Unfortunately most Lotus related magazine articles on Java do not provide examples of structured programming, much less object oriented - they give you stuff all written inline inside the JavaAgent class.

If I have a java library (classes I typed and compiled in Domino Designer - in the script library and projects dialog of the database they are displayed as .java) in my database. In my agent, how do I refer to these classes - do I use an import statement as if they were in a package?

Subject: Java Class Library - include as package?

You can package classes in the “Library” design element the same way you would in a jar file – they’re almost analogous. To use a “Library” element in your agent, just click on the “Edit Project” button (which should bring up this dialog):

Then browse (top left the left side) to “Shared Java Libraries” and pick the one you need. Then you’ll need to import the classes you need access to, just like a jar file.

dgg

Subject: Java Class Library - Much Simpler Explanation

Charles,

I don’t agree with Dallas’ explanation. He suggests that it is okay to have spaces in the name of your Java library as he demonstrates with his “Unused Java GUI Testing” example which is incorrect. He also tells you that you have to do some kind of importing of each class you need to use. Maybe that has worked for him, but I find this suggestion to be too complicated if you ask me.

It is very simple to reference methods in your Java library from your Java agent.

Let’s suppose you called your Java library “UtilitiesJava” and let’s suppose you have a method in there you want to access called “getCustomerName()”. Here is how you would configure your agent:

  1. Click the “Edit Project” button and add your Java library UtilitiesJava to your agent.

  2. Type the following line somewhere at the top of the code in the agent…

UtilitiesJava uj = new UtilitiesJava();

…to establish an object (uj) that references the UtilitiesJava library.

  1. Then to call the getCustomerName() method in that library, you just code uj.getCustomerName().

Ken

Subject: RE: Java Class Library - Much Simpler Explanation

Don’t confuse library names with package names.

Subject: RE: Java Class Library - Much Simpler Explanation

Thanks to all!

Subject: RE: Java Class Library - Much Simpler Explanation

He suggests that it is okay to have spaces in the name of your Java library as he demonstrates with his “Unused Java GUI Testing” example which is incorrect.

Why do you think it’s incorrect to have spaces in the name of a Java library? It works just fine.

He also tells you that you have to do some kind of importing of each class you need to use. Maybe that has worked for him, but I find this suggestion to be too complicated if you ask me.

What I actually wrote is that you can package classes within a Java Library. Packaging is a good idea for Java code whether it’s written in Notes or anywhere else. It helps to organize the code and, when done properly, can help protect it from being accessed inappropriately by other code. There are tons of respected individuals and companies (including Sun) that “suggest” packaging. As you observed, you don’t necessarily have to package your code, but then again you don’t have to use “Option Declare” in LotusScript either – even though it’s a recognized best practice.

So, if you agree with the rest of us that organizing your Java source code by packaging it is a good idea, then you have two ways to use packaged code once it’s in your classpath:

by the qualified class name – for example:

public class MyClass {

org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger()

public MyClass() {

}

public void logText(String pstrText) {

this.logger.info(pstrText);

}

}

or by importing the class – for example:

import org.apache.log4j.Logger;

public class MyClass {

Logger logger = Logger.getRootLogger()

public MyClass() {

}

public void logText(String pstrText) {

this.logger.info(pstrText);

}

}

If that’s “too complicated” for you, do it your way – don’t use packaging. But there are a lot of knowledgeable people who would advise you otherwise.

dgg

Subject: RE: Java Class Library - Much Simpler Explanation

to NOT use packages is just amatuerish and will certainly cause problems in the long term, one way or another