?
Solved

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

Posted on 2003-03-12
12
Medium Priority
?
183 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:dxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8120445
Does:

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

It's shouldn't, but it might...
0
 
LVL 35

Expert Comment

by:girionis
ID: 8120447
 Are you sure that the "Bean" class knows about the testpack and therefore the test class?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 1000 total points
ID: 8120468
Or even.

  test t = (test) Beans.instantiate( this.getClass().getClassLoader(), "testpack.test");
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:dxx
ID: 8120890

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
 

Author Comment

by:dxx
ID: 8120930
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
 
LVL 35

Expert Comment

by:girionis
ID: 8121001
 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
 

Author Comment

by:dxx
ID: 8121037
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
 

Author Comment

by:dxx
ID: 8121244
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
 

Author Comment

by:dxx
ID: 8121354
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
 

Author Comment

by:dxx
ID: 8121360
TimYates,

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

DXX
0
 

Author Comment

by:dxx
ID: 8121373
You saved me a lot of time and work. Thanks.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8121430
No worries :-)

Glad I could help :-)

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question