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
    Posts
    1

    Question Package & Import in Windows

    Hi-

    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:

    C:\prog\Minesweeper\viewController
    C:\prog\Minesweeper\viewController\model

    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 luck...so any help would be appreciated!!

    thanks
    jason

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    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:

    javac MineSweeper.java..

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

    the underline = the classpath entry


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

    javac viewController\MineSweeper.java

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


    -

    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:

    CLASSPATH+IMPORT_STATEMENT -> java file

    in this case, you type:

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

    c:\prog\MineSweeper\MineSweeper.java
    c:\prog\MineSweeper\model\hello.java

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

    javac viewController\MineSweeper.java
    LOAD: c:\prog\MineSweeper\viewController\MineSweeper.java
    LOAD: c:\prog\MineSweeper\model\hello.java

    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 MineSweeper.java 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\MineSweeper.java

    (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!

    -

    solutions:

    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)

    so:

    MyFile.java------

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

    AnotherFile.java

    package abc.def;

    ---------------

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

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

    the should be compiled like this:

    C:\blahblah> javac abc\def\ghi\MyFile.java
    (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, www.dictionary.com 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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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