I\'ve downloaded two jars. I want to import some of their contained classes. How do I do this?
It\'s for my algorithms class. I\'ve tried following the instructions
If you're using Terminal to compile and launch your program, then in the Terminal window, begin by setting the CLASSPATH
:
$ export CLASSPATH=$CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar
Then you can type echo $CLASSPATH
and see that the jars are referenced.
Now, in the same Terminal window, use javac to compile your class. Setting the CLASSPATH
as above only applies to the current Terminal window and any processes launched from it.
Alternately you can pass the CLASSPATH
to javac:
$ javac -cp $CLASSPATH:/Users/Michael/path/to/jar1.jar:/Users/Michael/path/to/jar2.jar MyClass.java
To persist this CLASSPATH
for future Terminal sessions, add the export
line above to the file .profile in your home directory.