Compiling a Servlet in Ubuntu returns errors

I am attempting to compile source code to be used in a webapp. I know things in Ubuntu are not like things in Windows and that setting classpath permanently is not a good thing to do so I decided to set the flag temporarily. Here is my command in terminal:

littlejavachild@ubuntu:~/LittleJavaChild/ServletProjects/beerV1$ javac -cp "/usr/share/tomcat7/servlet-api.jar" -d classes src/com/example/*.java

What I am trying to achieve is:

  • compile all the .java files in the package com.example
  • send the .class files directly to the appropriate directories
  • copy the classes folder to the WEB-INF folder
  • Despite setting the classpath I get the errors:

    src/com/example/ListenerTester.java:3: error: package javax.servlet does not exist
    import javax.servlet.*;
    ^
    src/com/example/ListenerTester.java:4: error: package javax.servlet.http does not exist
    import javax.servlet.http.*;
    ^
    src/com/example/ListenerTester.java:7: error: cannot find symbol
    public class ListenerTester extends HttpServlet{
                                        ^
      symbol: class HttpServlet
    src/com/example/ListenerTester.java:8: error: cannot find symbol
        public void doGet(HttpServletRequest request,
                          ^
      symbol:   class HttpServletRequest
      location: class ListenerTester
    src/com/example/ListenerTester.java:9: error: cannot find symbol
                    HttpServletResponse response) throws IOException, ServletException {
                    ^
      symbol:   class HttpServletResponse
      location: class ListenerTester
    src/com/example/ListenerTester.java:9: error: cannot find symbol
                    HttpServletResponse response) throws IOException, ServletException {
                                                                      ^
      symbol:   class ServletException
      location: class ListenerTester
    src/com/example/MyServletContextListener.java:2: error: package javax.servlet does not exist
    import javax.servlet.*;
    ^
    src/com/example/MyServletContextListener.java:4: error: cannot find symbol
    public class MyServletContextListener implements ServletContextListener{
                                                     ^
      symbol: class ServletContextListener
    src/com/example/MyServletContextListener.java:5: error: cannot find symbol
        public void contextInitialized(ServletContextEvent event){
                                       ^
      symbol:   class ServletContextEvent
      location: class MyServletContextListener
    src/com/example/MyServletContextListener.java:12: error: cannot find symbol
        public void contextDestroyed(ServletContextEvent event){
                                     ^
      symbol:   class ServletContextEvent
      location: class MyServletContextListener
    src/com/example/ListenerTester.java:14: error: cannot find symbol
            Dog dog = (Dog) getServletContext().getAttribute("dog");
                            ^
      symbol:   method getServletContext()
      location: class ListenerTester
    src/com/example/MyServletContextListener.java:6: error: cannot find symbol
            ServletContext sc = event.getServletContext();
            ^
      symbol:   class ServletContext
      location: class MyServletContextListener  
    

    I know these errors occur when the classpath is not set properly. Please help me with this. Tell me what is wrong and how do I go about correcting it and how to avoid it in future.

    Update with -verbose

    [search path for class files: /usr/lib/jvm/java-7-openjdk-i386/jre/lib/resources.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rt.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/sunrsasign.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jsse.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jce.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/charsets.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/netx.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/plugin.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rhino.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jfr.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/classes,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/localedata.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/sunjce_provider.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/dnsns.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/java-atk-wrapper.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/pulse-java.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/sunpkcs11.jar,/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/z ipfs.jar,.,/usr/share/tomcat7/servlet-api.jar]


    Split the task in bits, and let's just try to compile the java files first. Later check for how to move the class file.

    The error indeed says that it didn't find the required class, so the jar isn't added correctly to the classpath. Check this

    javac -classpath .:/usr/share/tomcat7/servlet-api.jar src/com/example/*.java
    

    Ensure the jar is present at the given location. Also try running this from the src folder and giving the path as com/example/*.java


    I would try using -classpath and not -cp . The Ubuntu man page for javac does not indicate a -cp option. You can also add -verbose to get more output from javac .

    Note: The javac implementation on my mac disagrees, but I'm not on an ubuntu machine atm.

    链接地址: http://www.djcxy.com/p/73816.html

    上一篇: Java SunPKCS11访问USB加密

    下一篇: 在Ubuntu中编译Servlet会返回错误