Link to home
Start Free TrialLog in
Avatar of dxx
dxx

asked on

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
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Does:

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

It's shouldn't, but it might...
 Are you sure that the "Bean" class knows about the testpack and therefore the test class?
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dxx
dxx

ASKER


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.



Avatar of dxx

ASKER

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.
 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?
Avatar of dxx

ASKER

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.
Avatar of dxx

ASKER

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.
Avatar of dxx

ASKER

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.
Avatar of dxx

ASKER

TimYates,

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

DXX
Avatar of dxx

ASKER

You saved me a lot of time and work. Thanks.
No worries :-)

Glad I could help :-)

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