Home > Archive > Java Help > December 2004 > Accessing external jar from servlet
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Accessing external jar from servlet
|
|
| Larry Coon 2004-12-28, 3:57 am |
| I'm trying to learn servlets, and am stuck getting the servlet
to use JDBC (specifically, Sybase's JConnect), which I think is
due to my not understanding how to provide the servlet with
access to the JDBC's jar file. I'd like to provide a small,
self-contained example, but I'm not sure that's possible here,
so let me just describe what I'm trying to do.
First, note the following:
-- I can get basic servlets running (with Tomcat) just fine.
-- I can use JDBC just fine with my Java applications.
Where I'm stumped is with this concept: My jdbc classes are in
C:\Java\jConnect-5_5\classes\jconn2.jar
From an _application_ I just import the right classes:
import com.sybase.jdbc2.jdbc.*;
and do the usual Class.forName to load the driver:
Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
and then I can tell the jvm where the jar file is:
java -cp .;C:\Java\jConnect-5_5\classes\jconn2.jar mypackage.MyApp
and all is well. But with a servlet, how do I provide that
connection between the Java code and the jar file? When I invoke
my servlet, Tomcat's log contains the following:
java.lang.ClassNotFoundException: com.sybase.jdbc2.jdbc.SybDriver
(followed by a full stack trace) afterward. The stack trace
indicates it choked during the Class.forName(), i.e., when trying
to load the driver.
The obvious thing (to me) seemed to be the CLASSPATH env variable,
but pointing CLASSPATH to the jar file didn't make any difference.
I tried checking all the docs I could find, but didn't find anything
that helped me (which I assume is a case of information overload on
my part rather than the information not being out there). Any
pointers would be appreciated.
Thanks.
| |
| Java Lover 2004-12-28, 3:57 am |
| You are right, it's the classpath, but add the jar file to the system
classpath variable doesn't help Tomcat to find the library, because it
uses a different path to search libraries.
Copy the JDBC jar file to your Tomcat server's webapps/WEB-INF/lib
directory.
| |
| Juha Laiho 2004-12-28, 4:00 pm |
| lmcoon@nospam_cox.net said:
>I'm trying to learn servlets, and am stuck getting the servlet
>to use JDBC (specifically, Sybase's JConnect), which I think is
>due to my not understanding how to provide the servlet with
>access to the JDBC's jar file.
Servlet containers (or Tomcat at least) pretty much disregard the
classpath.
One way (the fast and simple way) to provide external jar files
to a servlet application is to place the jar into WEB-INF/lib/
directory within the web application. However, if you're running
several independent applications in a single container, this way
you'll soon get several copies of the jar file on disk and in
memory.
The other way is dependent on your servlet container -- to find
out how it supports sharing jar files across several applications
(so, whether there's a place where you can drop jar files intended
to be accessible by all applications, or whether there's a way to
add jar files to the "internal classpath" of the servlet container).
A way specific to database connections would be to use the connection
pooling mechanisms provided by the servlet container -- and again,
configuring this depends on your container.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
| |
| Ryan Stewart 2004-12-28, 4:00 pm |
| "Java Lover" <ilovejava@gmail.com> wrote in message
news:1104217506.217787.239010@f14g2000cwb.googlegroups.com...
> You are right, it's the classpath, but add the jar file to the system
> classpath variable doesn't help Tomcat to find the library, because it
> uses a different path to search libraries.
>
> Copy the JDBC jar file to your Tomcat server's webapps/WEB-INF/lib
> directory.
>
Close, but that directory doesn't exist. And please quote the relevant parts
of the post to which you are replying.
OP: Each J2EE application has a WEB-INF directory which is invisible to the
client. Within this directory are, among other things, a classes directory
and a lib directory. The classes directory is the root of the classpath
where all your personally written classes should go. Since you mentioned
that you can run servlets, I'll assume you're familiar with this directory.
The lib directory is where JARs go. All JARs in WEB-INF/lib are added to the
application's classpath. So you can put your JDBC driver in your
application's WEB-INF/lib to gain access to it. If you'll be writing several
webapps that require the same JDBC driver, you can make it available to all
webapps rather than putting a separate copy in each one. This is what the
$CATALINE_BASE/shared/lib directory is for. Put any JARs here that you want
available to multiple apps.
|
|
|
|
|