File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Development: trouble with import and classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Development: trouble with import and classpath" Watch "Development: trouble with import and classpath" New topic
Author

Development: trouble with import and classpath

Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hello,

in order to become familiar with "javac [options] [source files]", I've prepared the following Java files in the following directories:
  • dev/source/dir1/dir2/dir3/Test.java
  • dev/source/dir1/dir2/dir3/sub/Library.java

  • Please notice that "dev" is not the root.

    My Java files are quite simple - the main issue consists in performing an import in class Test, as you can see below:



    First of all, I'm not sure which import I should use in class Test:
    the fully qualified name of the Libray class is "dir1.dir2.dir3.sub.Library" due to the package declaration "package dir1.dir2.dir3.sub", so the class name is atomic.

    When I try to compile the Test class with the destination directory (-d) "source/dir1/dir2/dir3 and with different classpath settings, I always get the same error:

    source\dir1\dir2\dir3\Test.java:3: package dir1.dir2.dir3.sub does not exist
    import dir1.dir2.dir3.sub.Library;
    ^
    source\dir1\dir2\dir3\Test.java:12: cannot find symbol
    symbol : class Library
    location: class dir1.dir2.dir3.Test
    Library lib = new Library();
    ^
    source\dir1\dir2\dir3\Test.java:12: cannot find symbol
    symbol : class Library
    location: class dir1.dir2.dir3.Test
    Library lib = new Library();
    ^
    3 errors

    Same thing, when I use the "import sub.Library" commented out in line 3.

    My javac command (executing from the dev directory) is the following:
    $ javac -classpath classes/dir1/dir2/dir3/sub -d classes source/dir1/dir2/dir3/Test.java

    I try to tell the compiler that the required class file (Library class, for the import) can be found under classes/dir1/dir2/dir3/sub.
    In fact, the physical file is really located there, I've checked it before!

    Besides, I've tried out several combinations concerning paths and imports, but nothing seems to work out.
    Any idea?


    SCJP 6 (88%), SCWCD (89%)
    lakshmanan anand
    Greenhorn

    Joined: Jan 13, 2009
    Posts: 22

    You have mistaken the meaning of flag -d

    flag -d is used for destination path for your java classes after compilation.

    The error message clearly shows that it cannot find Library.java itself. See if you have put Library.java in the right path


    http://luckydev07.blogspot.com
    Larry Chung
    Ranch Hand

    Joined: Feb 02, 2010
    Posts: 247
    Ulrich Vormbrock wrote:
    First of all, I'm not sure which import I should use in class Test:
    the fully qualified name of the Libray class is "dir1.dir2.dir3.sub.Library" due to the package declaration "package dir1.dir2.dir3.sub", so the class name is atomic.


    That's fine Ulrich, your import of the Library class is correct.

    My javac command (executing from the dev directory) is the following:
    $ javac -classpath classes/dir1/dir2/dir3/sub -d classes source/dir1/dir2/dir3/Test.java

    I try to tell the compiler that the required class file (Library class, for the import) can be found under classes/dir1/dir2/dir3/sub.
    In fact, the physical file is really located there, I've checked it before!

    Besides, I've tried out several combinations concerning paths and imports, but nothing seems to work out.
    Any idea?


    Command line compilations is one of the trickiest thing about Java.
    You did not mention in your post what was your current directory when you tried to compile with $ javac -classpath classes/dir1/dir2/dir3/sub -d classes source/dir1/dir2/dir3/Test.java. Your current directory partly determines how your specify your classpath.

    Please take another review of Chapter 10 of the K&B book, especially the paragraph that begins with "Remember when using a classpath, the last directory in the path must be the super-directory of the root directory for the package. .."
    HINT 1: The root directory of your packages is "dir1".
    HINT 2: You should be sure to be in the super directory of classes directory before trying to compile.

    SCJP 6
    Ulrich Vormbrock
    Ranch Hand

    Joined: Apr 15, 2010
    Posts: 73
    Hi,

    sorry for my delay - in the meantime, I'd another trouble concerning Cygwin - as I've learned a couple of minutes ago,
    I need to escape the delimiter ";" (classpath separator for UNIX, written in red letters) with a backslash, provided that I use Cygwin with Java for Windows.

    Now I've tried the following command, and it works out fine:
    $ javac -classpath dir1/dir2/dir3/sub\;. -d ../classes dir1/dir2/dir3/Test.java

    In addition, I've performed this command from the source-, and not from the dev-directory.
    Below, please find again my path structure:

    (1) dev/source/dir1/dir2/dir3/sub -> dir3 contains "Test.java", sub contains "Library.java"
    (2) dev/classes/dir1/dir2/dir3 -> target directory

    Performing the above mentioned command, a new subdirectory "sub" is created automatically under (2) and Library.class is placed into "sub".

    The only thing I don't understand is the fact that I'm obliged to add the current directory (.) into the classpath - otherwise, I always get the same error message as mentioned before.
    In fact, the missing class (Library) is located under "dir1/dir2/dir3/sub" and not in the current directory!

    Maybe, I've misunderstood something - in any case, I'll take another review on chapter 10 from K&B.

    Thank you all for your hints!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Development: trouble with import and classpath