I am having a bit of a hard time conceptualizing the scope of java libraries, jar files, source code, and classes. I am, hopefully, going to post this question in a way that is easy for beginning programmers to understand and learn from, as well as, in a way that is easy for well established programmers to understand my questions so they can answer them.
I have always taken certain things for granted in java, such as if I need a class that is in a specific library I import it. Also, when I see �package� I understand that it is saying, package this compiled code in the stated library (or .jar file?).
Where I�m having the hard time is in understanding the scope of the whole thing (�thing� is my technical term for java file/class/library/jar structure). I�ll try to explain the java file structure as I understand it, if someone could correct me where I�m wrong it would be greatly appreciated.
-.java files are source code. -class files are compiled .java files and I think they can be stored in .jar files. -.jar files are Java ARchive files, this is a way of compressing and throwing multiple files into one file (a java zip file if you will). -a library is a collection of classes (is this stored in a .jar file?). -when you want to use a certain class you need to import the library that it is in.
So I guess my questions are as follows: 1. Should all classes reside in .jar files? 2. When you use a �package� statement does it automatically create a .jar file? 3. When you use a �package� statement is it saying �create this class in this library� or is it saying �put this class in this .jar file�? 4. Is a library always stored in a .jar file? 5. Can classes be in the same library, but not in the same .jar file?
Even if you can only answer one or two of my questions it would be helpful to hear your answers, it�s always nice to have something explained in more than one way if possible.
1. Should all classes reside in .jar files? No, you can have a make a normal Java class, say HelloWorld.java, compile it and run it using .../>java HellowWorld
2. When you use a �package� statement does it automatically create a .jar file? No. A package is an encapsulation mechanism that you can use to group related classes and subpackages. Bundling the package's classfiles in a JAR file simplifies the distribution of the package however.
3. When you use a �package� statement is it saying �create this class in this library� or is it saying �put this class in this .jar file�? When you use the package statement, as in
The above show's a class called Person which is a member of the package personPackage. So yes, you might say that using the package statement states that your class is a member of a "library".
4. Is a library always stored in a .jar file? By library do you mean package? If so, then no. When you make a package you define where your classes are located. For instance, if you have a package called foo, all the classes in the package will be located in the directory .../foo. And if you have a package called foo.bar, then the classes in the subpackage (that is, bar) will be located in .../foo/bar.
5. I guess you could save the files in a library in different JAR files..
I hope you understand my answers... and that they are correct
Edit: Forgot the 5th question [ October 11, 2004: Message edited by: Svend Rost ]
Joined: Sep 24, 2004
Svend, I do understand your answers and they are much appreciated.
When you asked me what I meant by library (�By library do you mean package?�), I guess I meant that I hear about class libraries in java quite a bit, so that is what I am eluding to (apparently I�m not sure what I mean). My understanding of a library is a grouping of classes, I assumed that this grouping was physical (i.e. in the same folder or file - .jar was my thinking). Now I�m guessing that it�s a good idea to group your classes from the same library in the same folder, but you may not have to.
Joined: Oct 23, 2002
First of all: Concerning the library definition, then it matches my package defition.
In order to make a package then all the classes belonging to that package have to be in the same folder - that was what I tried to point out in my answer to your Q4. However, if you'd like to make a JAR file containing only a few of the classes from your library/package then you can do that.
A small example:
You have a package called animal containing a number of classes (animals) and you have a subpackage called fourLeggedAnimals containing, say the classes Horse and Dog. Your files containing the package.animal statement shold be in the following folder: ../animal> and the files containing the package.animal.fourLeggedAnimals statement should be in: ../animal/fourLeggedAnimals>
A package is just a namespace convention in Java, and the directory structure of the files must match the package name. Each package name element (the text between the dots) corresponds to a folder in your computer's directory structure. Packages in Java are very similar to namespaces in C++, but with the directory structure enforced.
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
subject: understanding java libraries, jar files, classes...