• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can a servlet in Tomcat use Java class outside of Tomcat?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using Apache Jakarta Tomcat (ver. 4.1.29) to accept an HTTPS call in order to call some Java classes. I know Java , but I only know Tomcat a little. Is it possible for a servlet running inside Tomcat to call Java classes that are NOT inside of Tomcat's directory structure? If so, where would I set up the CLASSPATH for that?

Kindly reply if you have the bandwidth.

Thanks,
Rica
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is possible.

Suppose there is a class that you are refering in ony of your servlets deployed in tomcat and the jar containing that class is registered with the system classloader (means that has been included in the CALSSPATH ) So for making a object of that class you request your context's classloader.As from JDK1.4 java follows a delegating model for classloading.Your classloader delegates the call to the commons classloader , that in turn delegates the call to the commons classloader and that in turn delegates the call to the system classloader and the class finally gets loaded from the system classloader and its residing somewhere outside the tomcat.

This is how I think classloading happens in tiomcat.
please let me know if this works.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can do it by defining it in your systems classpath or place it under jre/lib or.... But it would be the worst thing to do. Better place that class file under your "your-webapp/WEB-INF/classes" or place it under "your-web-app/lib" as jar may be. It would make your application independent to any of the external resources.
[ September 21, 2006: Message edited by: Adeel Ansari ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel Ansari.

I agree with what you stated.Just wanted to clarify that whatever I have mentioned is correct or there is something wrong.I have never tried this myself.Just read it from tomcat classloader how to.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomcat ignores your system classpath.
Servlet containers look for classes in a pre-determined set of directories.

If you want to use classes outside of these directories, you would need to create your own classloader.

I haven't tried (and don't think it would be a good idea) but you may be able to load classes in the JAVA_HOME/lib directory from a webapp running within Tomcat.

Why do you want to do this?
Let us know and we may be able to suggest a better alternative.
[ September 21, 2006: Message edited by: Ben Souther ]
 
richa Shrivastava
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for your fast response. That definitely helped me, but like you suggested I'll pass you the exact situation that I have. The Java "application" in question is a tool that accesses one of the clients legacy systems for some information. I am trying to solve 2 problems:

1. This Java application is shared by the servlet in Tomcat and a completely separate client/server application that does not go through the web server at all.

2. We have different testing environments which could, during development phases, contain different copies of this Java application.

I was trying to figure out the most efficient way to handle these situations without having 2 identical class libraries for each testing environment (one in Tomcat and one outside of it). The client/server application can get to all of the development and testing environments, so I thought it would be great if the servlet could hit the same classes by somehow dynamically setting the CLASSPATH based on an input parameter.

I could probably have the client/server app call the Java classes in the web server, but those directories are already set up. The only thing I was thinking about was trying Runtime.exec() from the servlet but was trying to see if there is a better way to do this.

Thanks again!

[ September 22, 2006: Message edited by: richa Shrivastava ]
[ September 22, 2006: Message edited by: richa Shrivastava ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ant.
Or whatever build tool you like.

Keep one copy of the source for the shared components but have your build script copy it to each project at build time.

This way, you only have to maintain one copy but still have the benefit having the classes in both places. The servlet spec (since 2.3, I think) really pushes the idea of fully self contained webapps, even if it means duplicating files.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic