Referencing a JAR archive
Hello forum, this question has probably been asked a thousand times but I cant seem to find a answer.
I have a java package installed a C:\Program File\CSoftware\Import\Classes.jar
In my code (C:\Java_Code), how do I make a reference to this package so that I can use its contents?
Thanks very much,
Harold Clements
You need to import the relevant classes/packages like so
import mypackage.*;
//or
import mypackage.gui.frame;
And then you need to add the JAR to your classpath. You using an IDE or command line? also which Operating System?
Google is your friend
http://www.cs.duke.edu/courses/fall01/cps108/resources/jar.html
Message was edited by:
tjacobs01
Thank you very much for your reply. I am compiling for the command line.
a)I am using Windows XP, how do I add a path to a classpath? (I know how to add to a normal system path)
b)From my example above what would be my equal to your mypackage?
Thanks again and sorry for the stupid questions.
java -cp .;C:\Program File\CSoftware\Import\Classes.jar ProgramName
I am really confused now.....
I will start again. I have a 3rd party package installed at the following location: :\Program File\CSoftware\Import\Classes.jar
I am using the command line to try and compile. I dont know how the reference the classes.jar packages in my own java code.
The example given to me above (1st reply) uses [import mypackage.*]. I would like to know (from the path I have given) what I would used in place of mypackage.
I take it (from the 4th reply) that when I have a compiled class I need to used the cp [path] [filename] command.
Thanks everyone for your help.
So you don't have any API to tell you the package names and class names in the jar, and how to use them?!
How did you expect to use it?!
you will need to add the package jar to the classpath for java and
javac. and of course for compilation you will need to use the proper
imports.
java(c) -cp PATH;PATH... Filename
. = THIS directory
javac -cp .;c:\whatever MyProgram.java
java -cp .;c:\whatever MyProgram
I know what is inside the Classes.jar file as a friend wrote them.
An example of a couple of classes that I have used.
GASimpleAlgorithm\Chromosome.class
GALittleAlgorithm\ Chromosome.class
GAAntAlgorithm \Ant.class
Ok, but what I also need to know is how to reference the classes.jar packages in my own java code.
import Classes.* does not work
> An example of a couple of classes that I have used.
> GASimpleAlgorithm\Chromosome.class
> GALittleAlgorithm\ Chromosome.class
> GAAntAlgorithm \Ant.class
> import Classes.* does not work
why would it?
shouldnt it be:
import GASimpleAlgorithm.*;
you might want to suggest to your friend that packages
are typically all lowercase.
OK, thanks for that I think that I have the right imports now. However, when I try compiling:
javac -cp .;C:\Program File\CSoftware\Import\Classes.jar GATest.java
I get - javac: invalid flag: -cp error.
with javac change it to -classpath
I don't think it likes the space in the 'Program Files' folder.
Indeed......
THANK YOU VERY MUCH....It is all working now
Cheers,
Harold
no problem. enjoy the jarry goodness.