类路径问题
我正在尝试编译 Tomcat 的 webappps/Email/src/Mail 文件夹中的 Servlet 类,但出现编译时错误:
./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
我用来编译该类的命令是
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
: servlet 类:
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();
}
}
}
当我尝试使用以下命令编译它时,它正在正确编译:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps>javac -d ./Email/WEB-INF/classes ./Email/src/Mail/*.java
Mail 文件夹中的 MessageReading 和邮件类。
谁能告诉我为什么第二个命令不起作用第一个命令?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个版本将忽略 CLASSPATH 环境变量,因为您使用 -cp 显式设置类路径。第二个没有。我想您的 CLASSPATH 包含 JavaMail、servlet API 等。在命令行中键入
set classpath
来验证这一点。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.当您没有在命令行上使用 -cp 指定类路径时,将使用环境 CLASSPATH 变量。因此,假设您丢失的 jar 位于 CLASSPATH 变量(servlet.jar、mail.jar 等)中,并且您没有在命令行上指定它们,那么您会在(未完全指定)手动指定的类路径中收到错误你输入-cp。
如果不出意外,将所需的 jar 添加到命令行以帮助了解程序需要运行的内容将是一个很好的练习。添加 servlet.jar 和 mail.jar 后,我相信您会收到一些有关 mail.jar 所需内容的附加错误。这有助于理解依赖关系。
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.
在我回答之前请注意:大多数人不会通过直接调用 javac 来编译 Java 代码。考虑在您的项目中使用 Ant 或 Maven2。
我不明白第二个命令是如何或为什么起作用的,如果它起作用,我会感到惊讶。 “mail”类的 Java 代码引用外部非 JDK 包中的 API;您必须向 javac 提供类路径信息(使用 -cp 选项),以便编译器与这些库“链接”。
在你的第一个命令中,我对 -cp 参数似乎指向源文件这一事实表示怀疑;此选项应指向包含已编译类文件和/或包含类文件的 .jar 文件的目录。
JavaMail API 类和 javax.servlet 类通常包含在 .jar 文件中。我确信 Tomcat 至少有 servlet jar,并且您可以从 java.sun.com 下载 JavaMail。
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.