Refering to a javabean in a package by another class in the same package

Hi,

I have been having problems making use of a javabean in a jar file through Tomcat.

I have a javabean called test.java and it's being used by a class called testclient.java. They both belong to the same package called testpack. testclient class instantiates test class by using the Beans.instantiate() method. Both of these files are put in a jar file called test.jar. I can use the testpack package from a simple class from the command line. But when I try to use the package from tomcat by putting the test.jar in tomcat_home\webapps\mytests\WEB-INF\lib and try to use it in JSP file, it does not work. I mean the JSP file can find the testpack package and can instantiate the testclient class, but then testclient's Beans.instantiate() cannot find the test class.

Here's my code.
----------------------- test.java ----------------------------------
package testpack;

import java.beans.*;
import java.io.Serializable;

public class test implements Serializable {
     protected String myvalue;

     public test() {
          myvalue = "testing worked..";
     }

     public String getMyvalue() {
          return myvalue;
     }
}
-------------------------------------------------------------------

-------------------- testclient.java ------------------------------
package testpack;

import java.beans.*;

public class testclient {

     public static String runtest() {
          try {
               test t = (test) Beans.instantiate(null, "testpack.test");
               return (  t.getMyvalue() );
          } catch (Exception e) {
               return ( e.toString() );
          }
     }
}
-------------------------------------------------------------------

------------------------ test.jsp ---------------------------------
<%@ page errorPage="../errorPage.jsp" %>
<%@ page import="testpack.*"%>

<%
out.println(testclient.runtest());
%>
-------------------------------------------------------------------

The jsp println statement prints out "java.lang.ClassNotFoundException: testpack.test". This is the exception captured from Beans.instantiate().

I sure hope you guys can help me out. I spent a couple of days trying to figure this out, but can't.

Thanks.
DXX
dxxAsked:
Who is Participating?
 
TimYatesConnect With a Mentor Commented:
Or even.

  test t = (test) Beans.instantiate( this.getClass().getClassLoader(), "testpack.test");
0
 
TimYatesCommented:
Does:

  test t = (test) Beans.instantiate( ClassLoader.getSystemClassLoader(), "testpack.test");
 
work any better?

It's shouldn't, but it might...
0
 
girionisCommented:
 Are you sure that the "Bean" class knows about the testpack and therefore the test class?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
dxxAuthor Commented:

To TimYates,

I tried ,

   test t = (test) Beans.instantiate( ClassLoader.getSystemClassLoader(), "testpack.test");

but it didn't work. Let me go and try your second suggestion.



0
 
dxxAuthor Commented:
To girionis,

That's exactly the problem. How do I get the "Beans" class to know about testpack and testpack.test ?

I assumed that "Beans" would know about it because test.jar is in the WEB-INF\lib directory, which is part of the classpath used by Tomcat for the current web application.

I tried messing with the manifest.mf I included in the jar file, but didn't get anywhere. Right now I have a default (empty) manifest.
0
 
girionisCommented:
 Do you define your own classloader in the "instantiate" method of the bean? Are you sure this classloader doesn't conflict with the ones that Tomcat uses? Have you tried moving the jar file up and down the classloader hierarchy?
0
 
dxxAuthor Commented:
To girionis,

That's exactly the problem. How do I get the "Beans" class to know about testpack and testpack.test ?

I assumed that "Beans" would know about it because test.jar is in the WEB-INF\lib directory, which is part of the classpath used by Tomcat for the current web application.

I tried messing with the manifest.mf I included in the jar file, but didn't get anywhere. Right now I have a default (empty) manifest.
0
 
dxxAuthor Commented:
To girionis,

That's exactly the problem. How do I get the "Beans" class to know about testpack and testpack.test ?

I assumed that "Beans" would know about it because test.jar is in the WEB-INF\lib directory, which is part of the classpath used by Tomcat for the current web application.

I tried messing with the manifest.mf I included in the jar file, but didn't get anywhere. Right now I have a default (empty) manifest.
0
 
dxxAuthor Commented:
To girionis,

That's exactly the problem. How do I get the "Beans" class to know about testpack and testpack.test ?

I assumed that "Beans" would know about it because test.jar is in the WEB-INF\lib directory, which is part of the classpath used by Tomcat for the current web application.

I tried messing with the manifest.mf I included in the jar file, but didn't get anywhere. Right now I have a default (empty) manifest.
0
 
dxxAuthor Commented:
TimYates,

Your second suggestion worked... Thanks a lot. Appreciate it.

DXX
0
 
dxxAuthor Commented:
You saved me a lot of time and work. Thanks.
0
 
TimYatesCommented:
No worries :-)

Glad I could help :-)

Odd that it doesn't work with "null"... Tomcat must screw with the System classloader :-/
0
All Courses

From novice to tech pro — start learning today.