Re: Question on jars
On 03/23/2011 03:43 PM, Anonymous wrote:
hi everyone. I've been going thru a nice book trying to learn java and the
book mentioned how to make a jar of classes and talked abit about classpath
and how you have to make directories matching the class names etc. So I
tried to create a jar of my classes but I can't compile against it. I am
getting errors about the class not being found even though verbose shows my
classpath is being used.
Are jars meant to be used this way in compiling code against packaged
classes or are jars just for executing? I've missed something here.
Jars can be accessed during compilation to find needed classes. A jar
file can be referenced on the command line when compiling or running a
Java application. The usual way of finding jar files with your library
of classes is to put a reference in the manifest file you use to package
your application into a jar.
You can find a short discussion of classpath here;
You can also put library jars into the ext directories of the compiler
and runtime packages.
My guess is that you are having a classpath problem during the compile.
If for example you have a library jar called classes.jar located in
the mypath directory and your application file is called
mypath/MyApplication.java, you need to be in the directory above mypath
when executing javac.
javac -cp .:mypath/classes.jar mypath/MyApplication.java
You need to be in the same place to jar the application correctly too
jar -cvfm mypath/MyApp.jar mypath/MyApp.man mypath/*.class
will package all your application .class files into MyApp.jar. You will
also need a manifest file with the following lines;
I put this all into a Windows batch file or shell file so I don't have
to type it over and over.
Let us know if that gets you going.