views:

59

answers:

3

Hi,

I'm trying to compile the Servlet class in Tomcat's webappps/Email/src/Mail folder, but I'm getting compile time errors:

./Email/src/Mail/Mail.java:7: package javax.mail does not exist 

import javax.servlet.http.*;

./Email/src/Mail/Mail.java:9:package javax.servlet does not exist
import javax.servlet.*;


./Email/src/Mail/Mail.java:12:cannot find symbol
symbol: class HttpServlet
public class mail extends HttpServlet{
                          ^
./Email/src/Mail/Mail.java:14: cannot find symbol
symbol: class HttpServletRequest
location: class Mail.mail
public void doPost(HttpServletRequest request,HttpServletResponse response)
                   ^

./Email/src/Mail/Mail.java:14: cannot find symbol
symbol: class HttpServletRequest
location: class Mail.mail
public void doPost(HttpServletRequest request,HttpServletResponse response)
                                              ^

./Email/src/Mail/Mail.java:20: cannot find symbol
symbol: class MessageReading
location: class Mail.mail
          MessageReading mr=new MessageReading();

7 errors

The command which I'm using to compile the class is:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps>javac -d ./Email/WEB-INF/classes -cp ./Email/src/Mail/. ./Email/src/Mail/mail.java

Here's the servlet class:

package Mail;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.http.*;
import javax.servlet.*; 
import Mail.*;

public class mail extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response)
{

    try{


        MessageReading mr=new MessageReading();

        }
    catch (Throwable e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        }

    }

}

It's compiling correctly, when I'm trying to compile it by using the command:

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps>javac -d ./Email/WEB-INF/classes ./Email/src/Mail/*.java

The MessageReading and mail class in Mail folder.

Could anyone please tell me why the second command worked not the first?

+1  A: 

The first version will be ignoring the CLASSPATH environment variable as you're using -cp to set the classpath explicitly. The second one doesn't. I imagine your CLASSPATH contains JavaMail, the servlet API etc. Type set classpath at the command line to verify this.

Jon Skeet
+1  A: 

The environment CLASSPATH variable will be used when you don't specify the classpath on the command line with -cp . So assuming your missing jars are in your CLASSPATH variable (servlet.jar, mail.jar, etc etc), given that you're not specifiying them on the command line, you get an error with your (incompletely specified) manually specified classpath when you type -cp.

If nothing else, it'd be a good exercise to add the required jars to the command line to help understand what your program needs to run. After you add servlet.jar and mail.jar I believe you'll get a few additional errors for things required by mail.jar. This is helpful for understanding dependencies.

Steve B.
Ah yes -- Steve B. is right; your environment CLASSPATH variable is used when -cp isn't specified. I recommend leaving your machine's CLASSPATH blank, however, because it can lead to problems down the road (working on multiple projects) that are difficult to debug or reproduce on your colleagues' machines.
Drew Wills
I actually want classpath header to search for all the classes in Mail folder, like MessageReading, without specifying it's name at the time of compiling mail class. How could I do if above not gonna work?
Dusk
A: 

One note before my answer: most folks do not compile Java code by calling javac directly. Consider using Ant or Maven2 with your project.

I don't understand how or why the 2nd command works, and I'm surprised if it does. The Java code for the 'mail' class references APIs in external, non-JDK packages; you must provide classpath information to javac (using the -cp option) in order for the compiler to "link up" with these libraries.

In you're 1st command, I'm suspicious about the fact that the -cp argument seems to be pointing to the source files; this option should point to directories with compiled class files and/or .jar files that contain class files.

The JavaMail API classes and the javax.servlet classes are typically contained in .jar files. I'm sure Tomcat has at least the servlet jar, and you can download JavaMail from java.sun.com.

Drew Wills