13 Replies Latest reply: Jul 11, 2007 6:24 AM by 843810 RSS

    Package does not exist

    843810
      Hi

      I have a java program called "BeerSelect.java" in
      c:\myprojects\beerv1\src\com\example\web directory.

      I have another program called "BeerExpert.java" in
      c:\myprojects\beerv1\src\com\example\model

      In BeerSelect.java, I have a code

      import com.example.model.*;

      at this lne, it gives package does not exist error. I checked the dir .
      Please let me know why. Thanks.
        • 1. Re: Package does not exist
          843810
          Do you have the appropriate package statements in your .java files?

          Here is an example to illustrate. I am using Unix-style pathnames, but
          you should be able to translate back to the DOS style paths you started
          with.
          % cd /c/myprojects/beerv1/src
          /c/myprojects/beerv1/src
          
          % cat -n com/example/web/BeerSelect.java
               1  package com.example.web;
               2
               3  import  com.example.model.*;
               4
               5  public class BeerSelect {
               6      public static void main(String args[]) {
               7          BeerExpert myExpert = new BeerExpert();
               8          myExpert.speak();
               9      }
              10  }
          
          % cat -n com/example/model/BeerExpert.java
               1  package com.example.model;
               2
               3  public class BeerExpert {
               4      public void speak() {
               5          System.out.println ("got beer?");
               6      }
               7  }
          
          % pwd
          /c/myprojects/beerv1/src
          % javac -g com/example/web/BeerSelect.java com/example/model/BeerExpert.java 
          
          % java -classpath .  com.example.web.BeerSelect
          got beer?
          Note the correlation between the package name (a.b.c), the import statement,
          and the directory name a/b/c.

          For more information on working with packages, take a look at this tutorial:
          Creating and Using Packages

          http://java.sun.com/docs/books/tutorial/java/interpack/index.html

          If this does not answer your questions, please post a code sample showing
          what you are doing.
          • 2. Re: Package does not exist
            843810
            The package names make it look an awful lot like you're doing the exercise from Head First Servlets & JSP (Basham, Sierra & Bates).

            I've just had a few hours trying to solve what appears to be the same problem as you and was solved by correctly using the -classpath command at the command line - along with the windows syntax which IS NOT SHOWN in the book -- they're using unix syntax.

            My folder/directory organisation is the same as yours and so long as your import statements are correct in each file then the way to compile from the command line which i used is...

            cd c:\MyProjects\beerV1

            to navigate to the root directory followed by

            javac -classpath "[path to tomcat directory]\common\lib\servlet-api.jar;classes" -d classes src\com\example\web\BeerSelect.java

            Noting that windows uses semi-colons instead of colons to seperate out different classpaths in the above command
            • 3. Re: Package does not exist
              843810
              First of all , package statements should be very clear ,no matter how many packages you use. If you are using any IDE , try creating new package for the project by right clicking it .
              After that in the package create a java class in the same procedure,giving some meaningful name to the class.Once the class is created check for the package name . Compare the name with your class file . By this way,it's easy to find out if you missed anything in naming your package .
              Hope this helps.

              -Jegan
              • 4. Re: Package does not exist
                843810
                You were completely right!

                Your answer helped me to solve exactly the same problem.

                Thank you very much!
                • 5. Re: Package does not exist
                  843810
                  I was referring to Kitsuney's answer, OK?!
                  • 6. Re: Package does not exist
                    843810
                    Thanks timbell..Managed to successfully compile my packaged .java files.
                    • 7. Re: Package does not exist
                      843810
                      Thanx its really helped me to solve the same problem...
                      • 8. Re: Package does not exist
                        843810
                        Hi,

                        Me too hitting the same problem,
                        I have my BeerExpert.class file in this directory,

                        C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\beer-v1\WEB-INF\classes\com\example\model\BeerExpert.class

                        "BeerSelect" under

                        C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\beer-v1\WEB-INF\classes\com\example\web

                        I get errors, compiling BeerSelect.java

                        C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\beer-v1\WEB-INF\
                        classes\com\example\web>javac BeerSelect.java
                        BeerSelect.java:2: package com.example.model does not exist
                        import com.example.model.*;
                        ^
                        BeerSelect.java:23: cannot find symbol
                        symbol : class BeerExpert
                        location: class com.example.web.BeerSelect
                        BeerExpert be = new BeerExpert();
                        ^
                        BeerSelect.java:23: cannot find symbol
                        symbol : class BeerExpert
                        location: class com.example.web.BeerSelect
                        BeerExpert be = new BeerExpert();
                        ^
                        3 errors

                        I have set my "classpath" like C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;C:\Program Files\Java\jdk1.5.0_04\lib;C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\beer-v1\WEB-INF\classes;.

                        Please let me know, where am I going wrong on the path settings?

                        Thanks
                        Sabitha
                        • 9. Re: Package does not exist
                          800560
                          sabithaa wrote:
                          I have set my "classpath" like C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;C:\Program Files\Java\jdk1.5.0_04\lib;C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\beer-v1\WEB-INF\classes;.
                          It may be the spaces in your paths that are causing a problem. Try putting each path inside quotes, like
                          "C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar";"C:\Program Files\Java\jdk1.5.0_04\lib";"C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\beer-v1\WEB-INF\classes";.
                          • 10. Re: Package does not exist
                            843810
                            Hello,
                            With the same example even i am facing the same problem. I even tried compiling with setting the class path according to windows but it still gives me the same problem.
                            The command i used to compile the servlet is :
                            +javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar";classes:.
                            -d "E:\Projects\Beer\classes" "E:\Projects\Beer\src\com\example\web\BeerSelect.java"+

                            It worked when i compiled my servlet which doesn't refer to any model class but directly responds to the user request. I gave a model class and try to compile i am stuck with the exact same errors mentioned here.
                            Can you please help me out?...
                            • 11. Re: Package does not exist
                              800560
                              VirtualVJ wrote:
                              Hello,
                              With the same example even i am facing the same problem. I even tried compiling with setting the class path according to windows but it still gives me the same problem.
                              The command i used to compile the servlet is :
                              +javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar";classes:.
                              -d "E:\Projects\Beer\classes" "E:\Projects\Beer\src\com\example\web\BeerSelect.java"+
                              What is the :. at the end of the classpath? Is that a typo in the post (or the forum software)? I am guessing it should be ;.

                              And the 'classes' entry does not look correct. It should be a full path.
                              • 12. Re: Package does not exist
                                843810
                                This is an interesting question. I have the Heads First book, 2008 version. In this version, they tell us that they are using Unix. But they just say we should use:
                                cd beerV1 
                                
                                javac -classpath /Users/bert/Applications2/tomcat/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java
                                And they, say if we are using WIN OS, we should change "...servlet-api.jar:classes:." to "...servlet-api.jar;classes:." But it doest seem to be the case.

                                In win/OS, I am using exactly the same some guy suggested and it does not work(package does not exists). I tried many variations, I am including the " in the classpath and so on.

                                I have src directory, which contain com\example\web\BeerSelect.java and com\example\model\BeerExpert.class

                                %cd com\example\web (this is where we got BeerSelect.java)

                                %javac -classpath "C:\Program and files.........common\lib\servlet-api.jar;classes" BeerSelect.java

                                Also tried

                                %javac -classpath "C:\Program and files.........common\lib\servlet-api.jar" BeerSelect.java

                                and

                                %javac -classpath "C:\Program and files.........common\lib\servlet-api.jar;classes;." BeerSelect.java

                                Any comments?
                                • 13. Re: Package does not exist
                                  843810
                                  Well, I solved the problem.

                                  Since i was not using the same directory structure as the book, I needed to change the word "classes" to "scr".
                                  It was a bad mistake, my bad =)

                                  The result

                                  %javac -classpath "C:\Program and files.........common\lib\servlet-api.jar;src;." BeerSelect.java