2
votes

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/zipfs.jar,.,/usr/share/tomcat7/servlet-api.jar]

    2
    You should consider using a build system like maven or ant.user180100
    @RC. I am a beginner yo! :pAn SO User
    It is actually usually easier to use an IDE (like eclipse) or a build system like Ant or Maven. I imagine very few java developers actually invoke javac directly.Tim Bender
    @TimBender I was always taught that whenever you learn anything new you should use the text editor and javac. Community member BalusC thinks the sameAn SO User
    I'd disagree. I think you are running into a peculiarity specific to ubuntu. Also, learning javac will not be the greatest asset to you professionally. You're much better off expending your efforts in learning a build system than you are in struggling with this.Tim Bender

    2 Answers

    4
    votes

    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

    2
    votes

    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.