This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes importing custom class/method into servlet;  servlet and finding classpath? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "importing custom class/method into servlet;  servlet and finding classpath?" Watch "importing custom class/method into servlet;  servlet and finding classpath?" New topic
Author

importing custom class/method into servlet; servlet and finding classpath?

eileen keeney
Ranch Hand

Joined: May 04, 2009
Posts: 51
I wrote a custom class.
I put the class in a package.
I put the path to the package in my CLASSPATH

The code was able to locate the package on compile. (Or at least it does not error).

However, if did error (on compile) if I used the actual classname, and not just the package name, in the import statement.

import packageName.*
worked (well at least it compiled without error, but the class could not be found at runtime)

import packageName.className.*
did not work

There is one class (className) and one method below the class.

Command is constructed
returnValue = className.methodName(arg1, arg2);


I believe my package is created correctly (but since this is the first time I have created a package, maybe I did this incorrectly).
I used
javac -d . classfile.java
and it created the directory for my package (based on the package name I used in the .java file), and put the compiled className.class file within it.

Does the servlet also need to know the classpath, even if it was known at compile time, when the class was compiled?
Is there a way to avoid this need?

How do I get the process, that is running the servlet, to know the classpath.
Even if I start tomcat, as the user that I compile with, it does not execute the .profile, where I have the classpath set.
Is this a configuration somewhere, I can set?

(I am new to java, and servlets)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Tomcat knows nothing about the classpath. You must place the class files in the appropriate location within the web application. One of:
  • class files in the hierarchy under WEB-INF/classes
  • a jar file in WEB-INF/lib



  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    eileen keeney
    Ranch Hand

    Joined: May 04, 2009
    Posts: 51
    Thanks,
    That worked.
    eileen keeney
    Ranch Hand

    Joined: May 04, 2009
    Posts: 51
    ok, it did work, now I can not get any package to work, even when outside of a servlet call.

    No matter what I do, even taking it down to the bare bones of functionality, I get an error.

    file does not contain class
    Please remove or make sure it appears in the correct subdirectory of the classpath.

    The class is in the file, and even shows up in the directory, that is named for the package.




     
     
    subject: importing custom class/method into servlet; servlet and finding classpath?
     
    Similar Threads
    CLASSPATH Environment Variable
    Problem with packages
    PACKAGE
    Error trying to get a simple bare-bones package to work.
    ARGH! Package problem while using *