UDF unable to load Java class. What do I need to do to make this work?

Hello all,
Continuing my UDF adventure started two days ago, I have managed to get the UDF registered but having problem getting the Java code to work. I need assistance to get this Double Metaphone Java UDF to work pretty quickly. Here is what I tried to run and the error message that resulted:
                SQL
select nm_frst, NM_LST, DB2ADMIN.DMPHONEJAVASCALAR(NM_LST)
from FSNST201.BR_DOH_BIRTH_RECORD
WHERE NM_FRST = 'SKYLER';

SQL4304N  Java stored procedure or user-defined function "DB2ADMIN.DMPHONEJAVASCALAR", specific name "DMPHONEJAVASCALAR" could not load Java class "/home/db2inst1/sqllib/fun", reason code "".  SQLSTATE=42724

SQL4304N  Java stored procedure or user-defined function "DB2ADMIN.DMPHONEJAVASCALAR", specific name "DMPHONEJAVASCALAR" could not load Java class "/home/db2inst1/sqllib/fun", reason code "".

Explanation:
The Java class given by the EXTERNAL NAME clause of a CREATE PROCEDURE or CREATE FUNCTION statement could not be loaded. The reason codes are:
 
 1 The class was not found on the CLASSPATH.  

 2 The class did not implement the required interface ("COM.ibm.db2.app.StoredProc" or  "COM.ibm.db2.app.UDF") or lacked the Java "public" access flag.  

 3 The default constructor failed or was unavailable.  

 4 Could not load driver for "jdbc:default:connection".  

 5 Could not establish default context.  

User Response:

Ensure that the compiled ".class" file is installed in the CLASSPATH, for example under "sqllib/function".  Ensure it implements the required Java interfaces and is "public".  
sqlcode :  -4304
sqlstate : 42724
SQLCODE: -4304

I will appreciate it if someone can guide me to determine if at all I am properly set up to invoke a javaUDF, what do I need to look for, and how I can go about setting up my environment and the UDF to execute successfully.
I fyou any question that you need answered please do not hesitate to ask as I will be monitoring this forum for the best part of this evening.

Thanks

Enuda
   
EnudaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

momi_sabagCommented:
hi
if you want to learn about what you need to do in order to write a java stored procedure you can look it up in the db2 manuals (which you can download from the ibm website) or in the udb information center

the error message here is pretty detailed and it gives you the possible reasons for your error:
1 The class was not found on the CLASSPATH.  
you can find out what the classpath is by looking at the envrionment variables of the user db2inst1 (i assume this is the user that runs your instance), the best way i see of checking if this is really your problem is just coping the jar files / class files of your java code to the jdbc driver directory, because it is in the CLASSPATH. the jdbc driver under AIX if i'm not mistaken will be
/home/db2inst1/sqllib/java

 2 The class did not implement the required interface ("COM.ibm.db2.app.StoredProc" or  "COM.ibm.db2.app.UDF") or lacked the Java "public" access flag.  

 3 The default constructor failed or was unavailable.  

 4 Could not load driver for "jdbc:default:connection".  

 5 Could not establish default context.  

make sure all of the above possible errors does not happen in your envrionment and we can take it from there
if you need furthur elaboration i'd be gald to help
0
EnudaAuthor Commented:
Momi_sabq,
Thanks for your response. I'm sure I will be reading more of java docs in the future. Right now I find myself in a position of doing it first, and learn later!
(1) Yes, db2inst1 runs the instance. First, the CLASSPATH. I tried to find it using the command "Which java" and got  "/usr/bin/java" which is supposed to be my java home. Is this the same as CLASSPATH?
When I looked in this directory, I found this "java ---->/etc/alternatives/java". So I navigated to this path, found and once again I found this "java -> /usr/lib/jvm/jre-1.4.2-gcj/bin/java". Once again, I navigated to this directory and found two scripts named "java"  and "rmiregistry". I browsed the member called java and found this in the script the following lines that was of interest:

        params="-Djava.home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre $params"
        clp="$CLASSPATH:/usr/share/java/libgcj-$GCC.jar"  

The above seem to point to the real classpath, right? How can I be sure of this? As I said, there are only the two members above in this dirctory. Nothing else. I have attached the actual java code as I have it. Please take a look.

problem is just coping the jar files / class files of your java code to the jdbc driver directory, because it is in the CLASSPATH. the jdbc driver under AIX if i'm not mistaken will be
/home/db2inst1/sqllib/java

On error item #2, how can I find out if a class did or did not implement the required interface ("COM.ibm.db2.app.StoredProc" or  "COM.ibm.db2.app.UDF") or lacked the Java "public" access flag.?
Is there any config or init file that I can look into to find out? Any environment variable?

 3 The default constructor failed or was unavailable.  What is this? How can I tell if it is available? Is this a system code or microcode of some sort?

 4 Could not load driver for "jdbc:default:connection".  Now, drivers I can understand but don't know how to find out if it is available and loaded at startup. Anyway to find out?

 5 Could not establish default context. What could this be or mean?

momi_sabaq, I trully appreciate your continuing effort to help with this problem.
Thanks




-------  Begining of Java in Script ------
exec $COMMAND
chmod +x $COMMAND
echo "exec /usr/bin/gij $params" >>$COMMAND
echo "rm $COMMAND" >$COMMAND
COMMAND=`mktemp /tmp/javaXXXXXX`
export CLASSPATH="$clp"
fi
        clp="$clp:/usr/share/java/libgcj-$GCC.jar"
        # We NEED this
if ! echo $clp |grep -q /usr/share/java/libgcj-$GCC.jar; then
 
fi
    params="-Djava.version=1.4.2 $params"
if [ -z "$HAVE_JAVA_VERSION" ]; then
 
fi
    params="-Djava.home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre $params"
if [ -z "$HAVE_JAVA_HOME" ]; then
 
done
        shift
        fi
                fi
                        HAVE_APPNAME=yes
                        params="$params \"`echo $1 |sed -e 's,",\\\\",g;s,/,.,g'`\""
                else
                        params="$params \"`echo $1 |sed -e 's,",\\\\",g'`\""
                if [ -n "$HAVE_APPNAME" ]; then
        else
                echo "Warning: $1 not understood. Ignoring." >&2
                # Yuck. Unknown parameter. Let's pretend nothing happened.
        elif [ -z "$HAVE_APPNAME" -a "`echo $1 |cut -b1`" = "-" ]; then
                params="$params \"`echo $1 |sed -e 's,",\\\\",g'`\""
                fi
                    HAVE_JAVA_VERSION=yes
                elif [ "`echo $1 |cut -b1-15`" = "-Djava.version=" ]; then
                    HAVE_JAVA_HOME=yes
                elif [ "`echo $1 |cut -b1-12`" = "-Djava.home=" ]; then
                        export LD_LIBRARY_PATH=`echo $1 |cut -b21-`:$LD_LIBRARY_PATH
                if [ "`echo $1 |cut -b1-20`" = "-Djava.library.path=" ]; then
        elif [ "`echo $1 |cut -b1-2`" = "-D" ]; then
                clp="`echo $1 | sed \"s~.*/lib/rt.jar~/usr/share/java/libgcj-$GCC.jar~\"`"
                        params="$params \"`echo $1 |sed -e 's,",\\\\",g;s,/,.,g'`\""
                else
                        params="$params \"`echo $1 |sed -e 's,",\\\\",g'`\""
                if [ -n "$HAVE_APPNAME" ]; then
        else
                echo "Warning: $1 not understood. Ignoring." >&2
                # Yuck. Unknown parameter. Let's pretend nothing happened.
        elif [ -z "$HAVE_APPNAME" -a "`echo $1 |cut -b1`" = "-" ]; then
                params="$params \"`echo $1 |sed -e 's,",\\\\",g'`\""
                fi
                    HAVE_JAVA_VERSION=yes
                elif [ "`echo $1 |cut -b1-15`" = "-Djava.version=" ]; then
                    HAVE_JAVA_HOME=yes
                elif [ "`echo $1 |cut -b1-12`" = "-Djava.home=" ]; then
                        export LD_LIBRARY_PATH=`echo $1 |cut -b21-`:$LD_LIBRARY_PATH
                if [ "`echo $1 |cut -b1-20`" = "-Djava.library.path=" ]; then
        elif [ "`echo $1 |cut -b1-2`" = "-D" ]; then
                clp="`echo $1 | sed \"s~.*/lib/rt.jar~/usr/share/java/libgcj-$GCC.jar~\"`"
                shift
        elif [ "$1" = "-classpath" -o "$1" = "-cp" ]; then
                gcj --version 1>&2
                echo 'java version "1.4.2"' 1>&2
        elif [ "$1" = "-showversion" ]; then
                exit 0
                echo 'java full version "gcj-1.4.2"' 1>&2
        elif [ "$1" = "-fullversion" ]; then
                exit 0
                gcj --version 1>&2
                echo 'java version "1.4.2"' 1>&2
        if [ "$1" = "-version" ]; then
while [ "$#" != 0 ]; do
unset HAVE_APPNAME || :
fi
        clp="/usr/share/java/libgcj-$GCC.jar"
else
        clp="$CLASSPATH:/usr/share/java/libgcj-$GCC.jar"
if [ -n "$CLASSPATH" ]; then
GCC=`gcj --version |head -n1 |awk '{ print $3; }'`
# (c) 2002-2003 Bernhard Rosenkraenzer <bero@arklinux.org>
# Emulate the jdk java command using gcj
#!/bin/sh

Open in new window

0
momi_sabagCommented:
hi
i don't understand what is the code you have attached
can you try to put your classes under /home/db2inst1/sqllib/java ? i

regarding - On error item #2, how can I find out if a class did or did not implement the required interface ("COM.ibm.db2.app.StoredProc" or  "COM.ibm.db2.app.UDF") or lacked the Java "public" access flag.?
Is there any config or init file that I can look into to find out? Any environment variable?

who wrote the java code ? if you wrote it, you need to know if you have implemented the specified interfaces, and regarding the PUblic flag, i believe it means your class should be declared as public
i think you can look at example under the /sqllib/sample/java diectory

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tliottaCommented:
Minor note... the posted code is script for a Linux (or UNIX) shell. It clearly is intended to be used _with_ some Java, but definitely isn't Java.

Tom
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.