Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understanding java libraries, jar files, classes...

 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harriet Tuttle
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't answer your question but I'm glad you posted it. So I'll be keeping an eye on this thread
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,

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

/Svend Rost

Edit: Forgot the 5th question
[ October 11, 2004: Message edited by: Svend Rost ]
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>


For more information about packages and JAR files, take a look at:
http://www.javaworld.com/javaworld/jw-09-2002/jw-0906-java101.html


/Svend Rost
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Svend, thank you for your replies. I now understand why I was getting "package" and "library" confused, class libraries are referred to as packages in java.

The article that you linked to was perfect, I'd recommend anyone who needs clarification on this subject to read it.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic