package related questions

X

Xiaoshen Li

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.
 
X

Xiaoshen Li

zero said:
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.
 
Z

zero

Xiaoshen Li said:
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
 
Z

zero

Xiaoshen Li said:
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.
 
X

Xiaoshen Li

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.
 
Z

zero

Xiaoshen Li said:
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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top