• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Development: trouble with import and classpath

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
     
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Ranch Hand
    Posts: 247
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Ulrich Vormbrock
    Ranch Hand
    Posts: 73
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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!
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic