简体   繁体   English

在Ubuntu中编译Servlet返回错误

[英]Compiling a Servlet in Ubuntu returns errors

I am attempting to compile source code to be used in a webapp. 我正在尝试编译要在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. 我知道Ubuntu中的情况与Windows中的情况不同,并且永久设置classpath并不是一件好事,因此我决定临时设置该标志。 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 编译包com.example中的所有.java文件
  • send the .class files directly to the appropriate directories .class文件直接发送到适当的目录
  • copy the classes folder to the WEB-INF folder 将classes文件夹复制到WEB-INF文件夹
  • Despite setting the classpath I get the errors: 尽管设置了classpath我还是得到了错误:

    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 用-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 [类文件的搜索路径:/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 /类,/ usr / lib中/ JVM / JAVA -7-的openjdk-I386 / JRE / lib / ext目录/ localedata.jar,/ usr / lib中/ JVM / JAVA -7-的openjdk-I386 / JRE / LIB /分机/把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目录/脉冲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] ipfs.jar。是/ usr /共享/ tomcat7 / servlet的api.jar文件]

    Split the task in bits, and let's just try to compile the java files first. 将任务分成几部分,让我们首先尝试编译Java文件。 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. 该错误确实表明它找不到所需的类,因此该jar无法正确添加到类路径中。 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 还可以尝试从src文件夹运行此文件,并将路径指定为com / example / *。java

    I would try using -classpath and not -cp . 我会尝试使用-classpath而不是-cp The Ubuntu man page for javac does not indicate a -cp option. javacUbuntu手册页未指示-cp选项。 You can also add -verbose to get more output from javac . 您还可以添加-verbosejavac获得更多输出。

    Note: The javac implementation on my mac disagrees, but I'm not on an ubuntu machine atm. 注意:我的Mac上的javac实现不同意,但是我不在ubuntu计算机atm上。

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM