Java packages & multiple sites

Discussion in 'OT Technology' started by Astro, Sep 4, 2003.

  1. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I've built 1 Java site (using Tomcat as the server). It works. Now its time to build a 2nd one. I think my thought process is flawed because this isn't working as I thought it might. For site #2, I made a new package directory. I then placed many of the files from site #1's package directory in site #2's package directory. I've updated site #2's web.xml file to reflect the new naming scheme.

    Here's where there's problems:

    I was hoping I could use the same class names, with a lot of the same functionality, but add some tweaks to fit site #2. Example: I have one class that focuses on building nav menus for a site. The class its in is called "Display" and its public. I've tossed the Display java and class files over into site #2's package directory and it compiles fine. If I reference this class from any classes in site #2's package, I get a compile error:

    I'm thinking this is a multi-part problem fueled by my lack of Java experience.

    I have a few files that won't need modifying that are more like library files. Is there a spot I can park them so all my web apps have access to them? I've tried doing this before but without any luck (I'm guessing I just need to know what XML file I need to tweak to make it work). The Dispatcher class mentioned above might be a good candidate for this.

    The Display class on the other hand needs to be global for the site, but not global for all sites (so all the classes of site #1 should see site #1's version of the Display class while site #2 has its own version it can see).

    Right now these classes are all being defined as public. I'm starting to think "public" may not be the best fit (it works for site #1, but from my research so far, public will make the class available to other sites - which I don't quite want).

    So, I think I'm missing some fundamental Java development techniques. Is there some books that cover these type of concepts? I've got a couple here, but they're all about deployment for one site on one server.
     
  2. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I really haven't done any Java, but I find that usually in C++ when I get an "unresolved symbol" error it's a linker problem... Not sure how that works in Java (heh, I'm even a little sketchy on how it works in C++), but maybe it'll help.
     
  3. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I'm sure its a linker problem. The real problem is with 1) organization of the code and 2) tweaking the right files and system variables so Java can find everything.
     
  4. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I'm not really sure where you are, so I'll just start from the beginning on how I manage my web apps as far as structure goes. :)

    I'm Tomcat, you should create two folders in your webapps directory (we'll say site1 and site2). So something like this: C:\Java\Tomcat\webapps\site1 and C:\Java\Tomcat\webapps\site2.

    Next, you dump all of your jsp's into their repective directories. Each project should have a directory structure similar to this:

    Code:
    site1
      ~ index.jsp
      WEB-INF
        ~web.xml
        classes
          com, etc. (this includes your package structure)
        lib
          ~ servlet.jar (plus any other .jars your specfic app needs)
    
    As you can see above, to have application specific classes (or jar libs) all you need to do is place them in this directory structure. Site1's classes won't be seen by Site2's classes.

    Creating jar files of commonly used logic is a good idea, just make sure you package everything correctly. I know you can place a jar file in a certain directory (I think its tomcat\shared\lib) and all of your applications can use it, but I'm not sure which folder it is.

    Also, I wouldn't worry about your classes being public. If you have a seperate directory structure for each application they won't see each other.

    I hope this helps some. :dunno:
     
  5. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I've got the dir structure setup as you mentioned already. Problem is I'm getting errors immediately on compiling the classes at site 2. The only thing I can think of is its a class naming conflict. To test this, I took a fresh class and referenced the local package to site 2. It failed on compiling. I then switched the package to the same package name used by site 1. It compiled without errors. This has me concerned because if what you say is true, I would have extected an error when referencing package 1 and no error for package 2.

    I was a bit toasted today, but I've had some more time to think about this. I'm thinking I'm going to go and revisit the package issue and build two skeleton classes that talk to each other. If they can't do that, then there's a site 2 config issue. If I can do that, then I'll just copy the guts of one of my site 1 classes. If that works, then I'll rename that class to one used in site 1. If that works, then I'll write it off as a caching issue or a Java quirk. If it doesn't work, then I'll assume Java takes it personally what you name your classes...
     
  6. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I don't think I'm understanding you correctly. The way I'm reading this is you have two different applications that are in two different directories under webapps and that do not dip into a common lib pool for classes. What you want to do is have two copies of some of the common classes. Is that correct? Or are you trying to share some classes between both applications by only having one copy? Are you trying to see a class in site1 from site2's directory structure? If so, it won't happen if they are in different webapp directories. You need to have a second copy, one for each application. Or you can put a jar file with these classses in a common directory under the Tomcat structure.

    About the error you posted above, I assume you have a Display class in your com.url.weblog package in the same application you're trying to reference it from. Is that correct? You won't be able to reference that class in site1 if it is in the site2 directory structure. Other than that, I'm unsure of where your error may be coming from. :dunno:
     
  7. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    That is correct. I don't understand why when site 2 references site 1's package, it compiles ok, but when it references site 2's copy, it says it doesn't know what it is (I'll get the word for word error tomorrow - but its very generic and uber-helpful - NOT!)
     
  8. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Do you have the source in two different projects (projects in your IDE) as well? If so, compile it in there and see what your errors are. You really don't want to have cross referencing classes/packages. It will be easier to track down your issue in the IDE rather than worring about application scope in Tomcat.
     
  9. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    :uh:

    After testing, I figured out I needed to add the top level package directory to the CLASSPATH. In researching this, I also forgot to add a context to server.xml. Ugh... I knew it was something some what simple!

    On another note, while researching cool places to put files, the consensus seems to be to put the servlets under WEB-INF of the site's root directory and ideally in a package. Putting them in Tomcat's shared class/lib directory is ok if the site is strictly going to be staying with Tomcat, but other web servers do not follow this practice. But they do all tend to follow the WEB-INF directory structure. So thats the route I took.

    Whew! This was driving me nuts...
     

Share This Page