Java servlet will not compile - cannot find javax.servlet

懵懂的女人 提交于 2019-12-02 06:52:58

问题


Im having big problems compiling java servlets. As far as i can see, ive done everything i need to do, ive installed tomcat 7 correctly, and tomcat is working. As i understand, i need to add servlet.jar package to my classpath. There doesnt seem to be a servlet.jar on my system but from what i can understand from the tomcat docs, its now servlet-api.jar.

Ive done this, by editing the classpath at /etc/environment:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" CLASSPATH="/usr/share/tomcat7/lib/servlet-api.jar"

Unfortunately, still no luck, i cant compile java servlets, and im still get warnings about missing symbols for javax.servlets.

Im using ubuntu 11.10 x64. Any ideas?


回答1:


Ive done this, by editing the classpath at /etc/environment:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" CLASSPATH="/usr/share/tomcat7/lib/servlet-api.jar"

That looks fine. The PATH is irrelevant to the particular problem though. It's not been used by Java in any way. It's only used by the operating system platform to look for executabeles. The CLASSPATH is similar, but only used by Java to look for classes which are to be used during compile and/or runtime.

Your problem is most likely caused because you used -cp or -classpath argument of javac command. In that case, the CLASSPATH environment variable will be ignored (this also applies to java command by the way, also when -jar argument is used).

You need to specify the classpath by only either the CLASSPATH environment variable or the -cp or -classpath argument. The general recommendation is to forget about the CLASSPATH environment variable at all as that's considered a poor practice whenever you want to do a bit more than just "Hello World". You can specify multiple paths in the -cp or -classpath argument using :.

$ cd /path/to/package/root/of/your/servlet/code
$ javac -cp .:/path/to/servlet-api.jar com/example/YourServlet.java

If you get tired of repeating typing this everytime, just put it in a .sh script, or use a build tool such as Ant which allows configuration by a XML file, or just use an IDE like Eclipse, Netbeans or IntelliJ which will do it all automagically when you just save the source file.




回答2:


You shouldn't have a CLASSPATH environment variable.

A better way to do it is to use the -classpath argument to javac.exe when you compile and java.exe when you run. Add the Tomcat servlet-api.jar that way.

Another suggestion is to learn Ant. It's an XML-driven, Java-based build tool. It's easier to learn and use than Maven. I'd start with that.




回答3:


javax.servlets is defined in servlet-api.jar so your issue must be with your config somehow. What are you using to compile? Are you issuing javac commands directly or using a build tool like maven, or ant, or event eclipse? I would recommend maven because it handles dependencies for you with a single configuration file. Really easy to pick up too.

In maven, you would need to start by getting maven installed/configured, and start your project with a set file structure (this is simplified but sufficient):

/src/main/java - The root of your java files
/src/main/webapp - The root of your webapp
/pom.xml - Maven configuration file

So a complete file list for a servlet might be:

/src/main/java/com/mycompany/myapp/MyServlet.java
/src/main/webapp/WEB-INF/web.xml
/src/main/webapp/index.jsp
/pom.xml

The first 3 lines (other than the root file structure) are the same as the would be for any web app, the last is your config file which would look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany</groupId>
  <artifactId>myapp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <type>jar</type>
      <scope>provided</scope>
    </dependency>
  </dependencies>
</project>

The important part here is that the dependency on servlet-api is handled by maven, so there is no need to download it, or set up classpaths or anything. Once you have set this file structure, and edited your pom, you just navigate a console to the root and type mvn package. That will download your dependencies, compile your code, and package your war. There is a LOT more that maven offers with very minimal modifications. In contrast, ant would require this:

to be added



来源:https://stackoverflow.com/questions/8107632/java-servlet-will-not-compile-cannot-find-javax-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!