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

[346 byte] By [haroldclementsa] at [2007-11-27 11:08:05]
# 1

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?

_helloWorld_a at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 2

Google is your friend

http://www.cs.duke.edu/courses/fall01/cps108/resources/jar.html

Message was edited by:

tjacobs01

tjacobs01a at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 3

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.

haroldclementsa at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 4

java -cp .;C:\Program File\CSoftware\Import\Classes.jar ProgramName

TuringPesta at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 5

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.

haroldclementsa at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 6

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?!

Hippolytea at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 7

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

TuringPesta at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 8

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

haroldclementsa at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 9

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

TuringPesta at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 10

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.

haroldclementsa at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 11

with javac change it to -classpath

TuringPesta at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 12

I don't think it likes the space in the 'Program Files' folder.

haroldclementsa at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 13

oh, duh. use quotes.

TuringPesta at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 14

Indeed......

THANK YOU VERY MUCH....It is all working now

Cheers,

Harold

haroldclementsa at 2007-7-29 13:26:21 > top of Java-index,Java Essentials,New To Java...
# 15

no problem. enjoy the jarry goodness.

TuringPesta at 2007-7-29 13:26:26 > top of Java-index,Java Essentials,New To Java...