package related questions

Discussion in 'Java' started by Xiaoshen Li, Dec 16, 2005.

  1. Xiaoshen Li

    Xiaoshen Li Guest

    Dear All,

    I am learning package name and have been puzzled a lot. I have asked
    similar questions before, based on the replies, I have made some progress.

    *****************WHAT I CAN MAKE IT WORK:************************
    (No CLASSPATH environment was set)
    In the directory ~/programming/java/Tools/
    I have a java file SavitchIn.java with the line on the top:
    package Tools.SavitchTools;

    Compiling this file generate a class as:

    ~/programming/java/Tools/classes/Tools/SavitchTools/SavitchIn.class

    (Now still working at ~/programming/java/Tools/). A file Test.java with
    a import line on the top:
    import Tools.SavitchTools.SavitchIn;

    javac -classpath ./classes/ Test.java

    works great. (Question 1: why the generated Test.class is not put in the
    current directory, instead is in ./classes ?)

    Anyway,
    java ./classes/Test
    Works great.

    *******************WHAT I CANNOT MAKE IT WORK********************
    If I am working in a far away directory
    ~/programming/java/other_projects/test_package/

    Same Test.java file including the same import statement:
    import Tools.SavitchTools.SavitchIn;

    javac -classpath ./../../Tools/classes Test.java

    No error message at all. Test.class was generated and put in the current
    directory. (Question 2: why this time not put in ./classes, instead put
    in the current directory? ./classes exists.)

    java Test
    get error messages:
    Exception in thread "main" java.lang.NoClassDefFoundError:
    Tools/SavitchTools/SavitchIn
    at Test_Package.main(Test_Package.java:8)

    My guess is linking problem.
    Question 3: why executing failed? How to make it work?

    Thank you very much for your help.
     
    Xiaoshen Li, Dec 16, 2005
    #1
    1. Advertising

  2. Xiaoshen Li

    Xiaoshen Li Guest

    zero wrote:

    >
    > java classes are not linked at compile time - the compiler only needs
    > them to make sure you're using the objects & methods correctly. Instead,
    > they are linked at runtime - so, you need to specify the classpath when
    > running the application as well.
    >
    > java -classpath ./../../Tools/classes Test
    >


    No. It doesn't work. (I have tested several times.)
    java -classpath ./../../Tools/classes Test

    will direct the control to look for Test.class in
    ../../../Tools/classes/. , while it is actually locate in the current
    directory. So it gave the error:

    Exception in thread "main" java.lang.NoClassDefFoundError: Test


    Another question, in my OP, I showed that I never used -d with javac to
    indicate where to put the generated class file, so I still don't know
    the answer to my question 1 and 2 in OP.

    Thank you.
     
    Xiaoshen Li, Dec 16, 2005
    #2
    1. Advertising

  3. Xiaoshen Li

    zero Guest

    Xiaoshen Li <> wrote in news:dnunrs$8n4t$:

    > Dear All,
    >
    > I am learning package name and have been puzzled a lot. I have asked
    > similar questions before, based on the replies, I have made some
    > progress.
    >
    > *****************WHAT I CAN MAKE IT WORK:************************
    > (No CLASSPATH environment was set)
    > In the directory ~/programming/java/Tools/
    > I have a java file SavitchIn.java with the line on the top:
    > package Tools.SavitchTools;
    >
    > Compiling this file generate a class as:
    >
    > ~/programming/java/Tools/classes/Tools/SavitchTools/SavitchIn.class
    >
    > (Now still working at ~/programming/java/Tools/). A file Test.java
    > with a import line on the top:
    > import Tools.SavitchTools.SavitchIn;
    >
    > javac -classpath ./classes/ Test.java
    >
    > works great. (Question 1: why the generated Test.class is not put in
    > the current directory, instead is in ./classes ?)
    >


    you control the place where the class files are put with the -d option.
    To put Test.class in the current directory, use

    javac -d . -classpath ./classes/ Test.java

    > Anyway,
    > java ./classes/Test
    > Works great.
    >
    > *******************WHAT I CANNOT MAKE IT WORK********************
    > If I am working in a far away directory
    > ~/programming/java/other_projects/test_package/
    >
    > Same Test.java file including the same import statement:
    > import Tools.SavitchTools.SavitchIn;
    >
    > javac -classpath ./../../Tools/classes Test.java
    >
    > No error message at all. Test.class was generated and put in the
    > current directory. (Question 2: why this time not put in ./classes,
    > instead put in the current directory? ./classes exists.)
    >
    > java Test
    > get error messages:
    > Exception in thread "main" java.lang.NoClassDefFoundError:
    > Tools/SavitchTools/SavitchIn
    > at Test_Package.main(Test_Package.java:8)
    >
    > My guess is linking problem.
    > Question 3: why executing failed? How to make it work?


    java classes are not linked at compile time - the compiler only needs
    them to make sure you're using the objects & methods correctly. Instead,
    they are linked at runtime - so, you need to specify the classpath when
    running the application as well.

    java -classpath ./../../Tools/classes Test



    --
    Beware the False Authority Syndrome
     
    zero, Dec 16, 2005
    #3
  4. Xiaoshen Li

    zero Guest

    Xiaoshen Li <> wrote in news:dnuvqk$dd1q$:

    >
    > zero wrote:
    >
    >>
    >> java classes are not linked at compile time - the compiler only needs
    >> them to make sure you're using the objects & methods correctly.
    >> Instead, they are linked at runtime - so, you need to specify the
    >> classpath when running the application as well.
    >>
    >> java -classpath ./../../Tools/classes Test
    >>

    >
    > No. It doesn't work. (I have tested several times.)
    > java -classpath ./../../Tools/classes Test
    >
    > will direct the control to look for Test.class in
    > ./../../Tools/classes/. , while it is actually locate in the current
    > directory. So it gave the error:
    >
    > Exception in thread "main" java.lang.NoClassDefFoundError: Test


    Right, my bad. How a small mistake can have big consequences

    You need to tell the JRE how to locate *all* files, so the path to the
    tools package is not enough, you also need to tell where the Test class
    is. You do this by having both in the classpath, separated by the (OS
    specific) path separation character. On Linux this is a colon I believe.
    So correct would be:

    java -classpath .:./../../Tools/classes Test

    >
    > Another question, in my OP, I showed that I never used -d with javac
    > to indicate where to put the generated class file, so I still don't
    > know the answer to my question 1 and 2 in OP.
    >


    Hmm, I'm not sure why the tools class would end up in a classes
    subdirectory - unless you were using some IDE or build tool? Those
    sometimes put classes in a subdir. Normally if you don't specify the -d
    option it should end up in the current directory - unless you use
    packages of course, then the package directory tree should start in the
    current directory.

    As an aside, the convention is to have class names start with a capital
    letter, but not package names. So you should change package
    Tools.SavitchTools to tools.savitchtools.

    --
    Beware the False Authority Syndrome
     
    zero, Dec 16, 2005
    #4
  5. Xiaoshen Li

    Xiaoshen Li Guest

    Thanks a lot. Everything is working.

    Now, I just hope to ask a couple more questions.

    (1) For my case, after compiling, my own package SavitchIn.class is
    located in
    ~/programming/java/Tools/classes/tools/savitchTools/SavitchIn.class

    The java file SavitchIn.java is located in
    ~/programming/java/Tools/src/SavitchIn.java
    (
    with the line on the top:
    package tools.savitchTools;
    )

    To know what is inside this package, I need to open and read
    SavitchIn.java, not SavtichIn.class.

    My question is, how about the original packages came with Java, say
    java.io.*. Where are their class files and where are their java files? I
    hope to open and read their java files as I can with my own package
    java file SavitchIn.java.

    I know I can find all java packages and what the insides are in the
    sun's java web page. But I still hope to find them out on my own machine.

    (2) Is package strategy rarely used by java programmers? (It seems you
    are the only person knowing about it.)

    Thank you very much.
     
    Xiaoshen Li, Dec 19, 2005
    #5
  6. Xiaoshen Li

    zero Guest

    Xiaoshen Li <> wrote in news:do6n1r$8f22$:

    >
    > Thanks a lot. Everything is working.
    >
    > Now, I just hope to ask a couple more questions.
    >
    > (1) For my case, after compiling, my own package SavitchIn.class is
    > located in
    > ~/programming/java/Tools/classes/tools/savitchTools/SavitchIn.class
    >
    > The java file SavitchIn.java is located in
    > ~/programming/java/Tools/src/SavitchIn.java
    > (
    > with the line on the top:
    > package tools.savitchTools;
    > )
    >
    > To know what is inside this package, I need to open and read
    > SavitchIn.java, not SavtichIn.class.


    You (a human) need the .java file, but the compiler & runtime environment
    need the .class file.

    >
    > My question is, how about the original packages came with Java, say
    > java.io.*. Where are their class files and where are their java files?
    > I
    > hope to open and read their java files as I can with my own package
    > java file SavitchIn.java.
    >
    > I know I can find all java packages and what the insides are in the
    > sun's java web page. But I still hope to find them out on my own
    > machine.


    As stated above, the computer doesn't need the .java files to compile or
    run your programs, only the .class files. These are located in several
    jar files. You can find the standard jar files in the lib subdirectory
    of your java installation. If you are interested you can extract the
    class files from these jar files - but they won't help you much, since
    they're not source files.

    However, the JDK comes with the java source files as well. In your JDK
    installation directory, you should have the src.zip file. This contains
    all the java files. They are an interesting read, although you can't
    expect to understand all of it right away.

    >
    > (2) Is package strategy rarely used by java programmers? (It seems you
    > are the only person knowing about it.)
    >


    lol no, packages are used in any non-trivial project. I suppose the
    other posters here just saw that I already answered, so they didn't bud
    in :)


    --
    Beware the False Authority Syndrome
     
    zero, Dec 19, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Demkovitch

    QyeryString related questions..

    Ivan Demkovitch, Aug 21, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    591
    Kevin Spencer
    Aug 21, 2003
  2. Sammy
    Replies:
    2
    Views:
    454
    Sammy
    Feb 15, 2005
  3. Nalaka
    Replies:
    1
    Views:
    344
    Bruce Barker
    Feb 1, 2006
  4. Charles Krug
    Replies:
    3
    Views:
    2,346
    Miki Tebeka
    Apr 25, 2005
  5. Maxwell Hammer
    Replies:
    7
    Views:
    648
    Peter Hansen
    Jun 18, 2005
Loading...

Share This Page