For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2005 > Tomcat: importing a package..









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 Tomcat: importing a package..
Frances Del Rio

2005-04-25, 8:59 am

I really don't understand what I'm doing wrong here..

I'm on chapter 8 of the Java Servlet & JSP Cookbook..

I dl'd the package he says (com.jar) and put it in <webappRoot>/lib..

servlet is here..
C:\tomcat\webapps\file\WEB- INF\classes\com\jspservletcookbook\Uploa
dServlet.class

com.jar is here.. C:\tomcat\webapps\file\WEB-INF\lib

in servlet:

package com.jspservletcookbook;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import java.util.Enumeration;

public class UploadServlet extends HttpServlet {

// ... etc...

now in this package I dl'd he has it "broken down", so I can see
structure, docs, src, etc in jar file
(cos- 05Nov2000\classes\com\oreilly\servlet\mu
ltipart).. this is right
code to import that package..

yet when I compile the servlet I get all these errors..

UploadServlet.java:5: package com.oreilly.servlet does not exist
import com.oreilly.servlet.MultipartRequest;
^
UploadServlet.java:6: package com.oreilly.servlet.multipart does not exis
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
^
UploadServlet.java:22: cannot resolve symbol
symbol : class MultipartRequest
location: class com.jspservletcookbook.UploadServlet
MultipartRequest mpr = new MultipartRequest(request,webTempPath,
^
UploadServlet.java:22: cannot resolve symbol
symbol : class MultipartRequest
location: class com.jspservletcookbook.UploadServlet
MultipartRequest mpr = new MultipartRequest(request,webTempPath,
^
4 errors

(I had to recompile b/c had to change this line..

webTempPath = getServletContext().getRealPath("/") + "data";

to

webTempPath = getServletContext().getRealPath("\\") + "data";

make sense? I'm running Tomcat on Windows 2000 (for now... later will
want to make this work on my website server, on unix..)

would appreciate any leads.. thank you.. Frances

Juha Laiho

2005-04-26, 8:58 pm

Frances Del Rio <fdr58@yahoo.com> said:
>I really don't understand what I'm doing wrong here..
>
>I'm on chapter 8 of the Java Servlet & JSP Cookbook..
>
>I dl'd the package he says (com.jar) and put it in <webappRoot>/lib..

....
>now in this package I dl'd he has it "broken down", so I can see
>structure, docs, src, etc in jar file
>(cos- 05Nov2000\classes\com\oreilly\servlet\mu
ltipart).. this is right
>code to import that package..


That won't work for a library jar file.

In a library jar file, the directory hierarchy within the jar should
exactly match the package/class hierarchy -- so, for example

>UploadServlet.java:5: package com.oreilly.servlet does not exist
>import com.oreilly.servlet.MultipartRequest;
> ^


.... this 'com.oreilly.servlet.MultipartRequest' class file should be
at com/oreilly/servlet/MultipartRequest.class within the jar file.

So, at the minimum you'll need to 'jar xf' (extract) the file you
now have, and 'jar cf' (create) a new jar file, starting from just
below the com/ directory in which the class files are, to get a real
library jar file.
--
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)
Frances Del Rio

2005-04-27, 3:59 am

Juha Laiho wrote:
> Frances Del Rio <fdr58@yahoo.com> said:
>

I made a mistake here.. of course I meant <webappRoot>/WEB-INF/lib..
(which is what I did.....;)
[color=darkred]
> ...


>
> That won't work for a library jar file.
>
> In a library jar file, the directory hierarchy within the jar should
> exactly match the package/class hierarchy -- so, for example
>
>
> ... this 'com.oreilly.servlet.MultipartRequest' class file should be
> at com/oreilly/servlet/MultipartRequest.class within the jar file.


but it is, Juha... in com\oreilly\servlet is a class called
MultipartRequest.class...

in that 'servlet' dir are a bunch of classes AND a dir called 'multipart'...

I "unpacked" jar file in DOS shell, pls see screen-shot here..
http://www.francesdelrio.com/jar.html

so: in <appRoot>WEB-INF/lib should be all these files/dirs that we see
in this screen-shot, right?

if you import a package from a servlet, does Tomcat autom. know to look
for it in WEB-INF/lib?

so if in a servlet that is here..

<appRoot>\WEB-INF\classes\com\jspservletcookbook

I do this..

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

it should find the package, right?

thank you very much for yr response, Juha.. Frances

Frances Del Rio

2005-04-27, 3:59 am

Juha, just as an experiment I packaged a simple helper class I have in
an app.. class is sH.class (simple class that I use just to store vars
to generate HTML code..) I put this class in WEB-INF\lib\com, then
converted this 'com' into a jar.. ('jar cf sH.jar com' in WEB-INF\lib)
so now I have WEB-INF\lib\sH.jar (sH.jar = com\sH.class..) and if in
WEB-INF\lib I do 'jar tf sH.jar' I get

META-INF/
META-INF/MANIFEST.MF
com/
com/sH.class

so far so good, right?

so in servlet (which is in WEB-INF\classes\hm) I now do

import com.sH; but when compiling servlet get told that
'package com.sH' does not exist..

I must be missing something here..
(again, Tomcat knows to look for packages in WEB-INF\lib, right? Juha,
thanks again.. Frances


Juha Laiho wrote:
> Frances Del Rio <fdr58@yahoo.com> said:
>
>
> ...
>
>
>
> That won't work for a library jar file.
>
> In a library jar file, the directory hierarchy within the jar should
> exactly match the package/class hierarchy -- so, for example
>
>
>
>
> ... this 'com.oreilly.servlet.MultipartRequest' class file should be
> at com/oreilly/servlet/MultipartRequest.class within the jar file.
>
> So, at the minimum you'll need to 'jar xf' (extract) the file you
> now have, and 'jar cf' (create) a new jar file, starting from just
> below the com/ directory in which the class files are, to get a real
> library jar file.

Juha Laiho

2005-04-27, 8:59 pm

[ I'm reordering the message a bit - to keep some things better together ]

Frances Del Rio <fdr58@yahoo.com> said:
>Juha Laiho wrote:
>
>I made a mistake here.. of course I meant <webappRoot>/WEB-INF/lib..
>(which is what I did.....;)
>
>
....[color=darkred]
>
>but it is, Juha... in com\oreilly\servlet is a class called
>MultipartRequest.class...

....
>I "unpacked" jar file in DOS shell, pls see screen-shot here..
>http://www.francesdelrio.com/jar.html


[slight grumble about presenting textual info as graphics..]

Ok; I was thrown off by the path you wrote earlier (the one quoted above,
starting with cos-05Nov2000\... ) -- I was assuming that to be the
directory structure within the jar file. What your screen shot showed
was a correct library jar structure.


And now thta I read your other messages as well, you're having problems
at compile time, right? If so, Tomcat is not involved at that point.

So, when compiling a servlet, you'll have to do it manually - and at that
time you need to explicitly tell javac where your libraries are. So,
javac -classpath D:\path\to\some.jar;F:\path\to\other.jar SomeServlet.java
or something like that.

At runtime, Tomcat will find the jar files needed by (compiled) classes,
and at that point it is sufficient to just leave them at WEB-INF\lib.

Was this of any help?
--
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)
Frances Del Rio

2005-04-27, 8:59 pm


> [slight grumble about presenting textual info as graphics..]
>
> Ok; I was thrown off by the path you wrote earlier (the one quoted above,
> starting with cos-05Nov2000\... ) -- I was assuming that to be the
> directory structure within the jar file. What your screen shot showed
> was a correct library jar structure.
>
>
> And now thta I read your other messages as well, you're having problems
> at compile time, right? If so, Tomcat is not involved at that point.
>
> So, when compiling a servlet, you'll have to do it manually - and at that
> time you need to explicitly tell javac where your libraries are. So,
> javac -classpath D:\path\to\some.jar;F:\path\to\other.jar SomeServlet.java
> or something like that.
>
> At runtime, Tomcat will find the jar files needed by (compiled) classes,
> and at that point it is sufficient to just leave them at WEB-INF\lib.
>
> Was this of any help?


oh yes, it was.. thank you so much.. I had to put path to package in
batch file I use to compile.. (also, had missed fact that needed to
alter class in package to put 'package com' at the top (b/c class is now
in WEB-INF\lib\com (instead of in WEB-INF\classes\<package> ) and then
had to do a new batch in WEB-INF\lib\com to compile class in package
that will become jar file.. and then after compiling remove src for
class and batch from 'com' dir before I convert to jar.. geeees...
this path-thing to everything can get pretty iffy... ;) now I see why
people use ant and stuff like that, I mean for large apps you can go
crazy here.. (I dl'd ant once but couldn't figure out how to use it..
I guess eventually I will have to..) thank you so much for your help..
Frances


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com