Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

package related questions

Thread view: 
Xiaoshen Li - 16 Dec 2005 12:13 GMT
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.
zero - 16 Dec 2005 17:23 GMT
> Dear All,
>
[quoted text clipped - 20 lines]
> 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
[quoted text clipped - 21 lines]
> 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

Signature

Beware the False Authority Syndrome

Xiaoshen Li - 16 Dec 2005 14:29 GMT
> 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.
zero - 16 Dec 2005 23:41 GMT
>> java classes are not linked at compile time - the compiler only needs
>> them to make sure you're using the objects & methods correctly.
[quoted text clipped - 11 lines]
>
> 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.

Signature

Beware the False Authority Syndrome

Xiaoshen Li - 19 Dec 2005 12:48 GMT
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.
zero - 19 Dec 2005 18:28 GMT
> Thanks a lot. Everything is working.
>
[quoted text clipped - 13 lines]
> 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?
[quoted text clipped - 5 lines]
> 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 :-)

Signature

Beware the False Authority Syndrome



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.