Package & Import in Windows

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Package & Import in Windows

  1. #1
    Join Date
    Feb 2004

    Question Package & Import in Windows


    I just installed XEmacs & J2SDK1.4.2_03 on my WindowsXP Home Edition.

    I decided to test it out using some working java programs I had written before using XEmacs on Unix...but when I try to run them in WindowsXP I am getting loads of errors because Java is not recognizing the package and also not recognizing importing a local folder.

    Here's my package and import statements.

    package viewController;

    import model.*;

    The compiler is not recognizing either.

    viewController is the directory(folder) that I'm working in and compiling from and model is in viewController.

    So the complete path is this:


    Anyone know what's going wrong? I'm sure it's a simple problem, but i've been looking online for a couple hours with no any help would be appreciated!!


  2. #2
    Join Date
    Feb 2004
    i wrote a whole post on this then scrapped it. this one should be easier to understand:

    think of the CLASSPATH as a declaring a "root drive in a java file system"

    this root is used by the compiler, when it looks for files.

    if you set your classpath = %classpath%;"C:\prog\MineSweeper"

    then the %classpath% means (the current value of the classpath) - so C:\prog\MineSweeper is added

    the java compiler then uses this. when you type:


    it looks in every entry in the class path (all those roots) for
    eventually it finds in in "C:\prog\MineSweeper\

    the underline = the classpath entry

    if is in a folder instead, you have to specifiy that folder name too:

    javac viewController\

    eventually it finds it in: "C:\prog\MineSweeper\viewController\


    the compiler ignores PACKAGE statements, but uses import statements. the import statements MUST provide access to the needed classes, when the classpath is pre-pended:


    in this case, you type:

    javac //it has an "import model.hello;" statement ..
    the compiler tries to load:


    what happens if, like before, you make a subdirectory and compile from the parent? it fails! heres what it tries to load:

    javac viewController\
    LOAD: c:\prog\MineSweeper\viewController\
    LOAD: c:\prog\MineSweeper\model\

    the IMPORT statements ALWAYS import from the CLASSPATH root.. no matter how you enter directories in the javac statement


    getting it?

    so what of PACKAGE?

    well PACKAGE is used by java, not javac. it is used basically to say that "relative to the classpath root, you should find me in this path, on disk"

    so say your is in the viewController cdirectory (which is in the dir specified in the classpath), but he doesnt have a PACKAGE statement.

    this compiles it:

    c:\prog\MineSweeper> JAVAC viewController\

    (the bit in bold is the dos prompt) no problems.
    this runs it?

    c:\prog\MineSweeper> JAVA viewController\MineSweeper

    nope.. it fails with the "wrong name" - it is in directory viewController, relative to the classpath, BUT it doesnt have a "package viewController" statement at the top!



    decide what you want your ROOT classpath to be.
    Every file in that directory tree, MUST have a PACKAGE statement describing exactly WHERE it is relative to the root classpath, it is.
    IMPORTs are made using the ROOT of the classpath, not the current file location (java doesnt care for directory structures; it cant be allowed to because it is multi-platform)


    package abc.def.ghi;
    import abc.def.AnotherFile;

    package abc.def;


    the 2 files MUST exist at these locations, if the classpath is "...whatever...;c:\blahblah" :


    the should be compiled like this:

    C:\blahblah> javac abc\def\ghi\
    (myfile refers to anotherfile - automatic compilation of AnotherFile)

    they should be run like this:

    c:\blahblah> java abc.def.ghi.MyFile


    using that info i think you can work out what you need to do.. ?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center