Can anyone tell me how to add to the classpath on OSX?
-
3Do you want to fix a single application, a lot of applications for a single user or a lot of application for all users on the machine? – Thorbjørn Ravn Andersen Nov 04 '09 at 19:10
-
javac -cp .:/home/ec2-user/lib/*: QuickStart.java – Kanagavelu Sugumar Nov 22 '17 at 18:04
5 Answers
If you want to make a certain set of JAR files (or .class files) available to every Java application on the machine, then your best bet is to add those files to /Library/Java/Extensions
.
Or, if you want to do it for every Java application, but only when your Mac OS X account runs them, then use ~/Library/Java/Extensions
instead.
EDIT: If you want to do this only for a particular application, as Thorbjørn asked, then you will need to tell us more about how the application is packaged.

- 32,152
- 8
- 53
- 57
-
1Saved my day for making JUnit work on Vim with Syntastic syntax checker plugin. I wonder why there wasn't any other simple installation example for this on the www... – Ory Band Dec 17 '12 at 22:09
-
1Why MAC is not respecting -cp/-classpath options, looks like it always loads the jars from /Library/Java/JavaVirtualMachines – Kanagavelu Sugumar Feb 02 '17 at 18:01
-
1-jar command is able to scan /Library/Java/Extensions, ~/Library/Java/Extensions – Kanagavelu Sugumar Feb 02 '17 at 18:18
-
This works great as a workaround, but I agree with @kanagavelu It would be good to know why classpath is being ignored. – JoshOrndorff Sep 15 '17 at 16:55
-
1I tried placing the jar files in the `Extensions` it does not work – Mrak Vladar Mar 27 '21 at 17:02
In OSX, you can set the classpath from scratch like this:
export CLASSPATH=/path/to/some.jar:/path/to/some/other.jar
Or you can add to the existing classpath like this:
export CLASSPATH=$CLASSPATH:/path/to/some.jar:/path/to/some/other.jar
This is answering your exact question, I'm not saying it's the right or wrong thing to do; I'll leave that for others to comment upon.

- 13,427
- 22
- 69
- 98
-
1I just want to add my jar to classpath. Why are there paths to two jars? – cegprakash Feb 19 '15 at 21:11
-
2He is showing how to add more than 1 jar. He also implies that adding just 1 jar is simply appending `:path/to/jar` to the existing classpath @cegprakash – ylun.ca Jul 09 '15 at 18:32
To specify a classpath for a single Java process, you can add a classpath option when you run the Java command.
In you command line. Use
java -cp "path/to/your/jar:." main
rather than just
java main
The option tells Java where to search for libraries.

- 572
- 3
- 13
If your shell is tcsh or csh, you can set it in /etc/profile. Open terminal, "vim /etc/profile" and add the following line:
setenv CLASSPATH (insert your classpath here)

- 3,066
- 1
- 24
- 40

- 1,245
- 9
- 6
Normally there's no need for that. First of all
echo $CLASSPATH
If there's something in there, you probably want to check Applications -> Utilites -> Java.

- 13,427
- 22
- 69
- 98

- 7,188
- 17
- 64
- 103
-
2
-
-
2if the CLASSPATH env var is not set in your shell you will get an empty row ... => if you want it to contain something you should set it in .bash_profile , .bashrc or whatever env vars source file your shell might use ... – Yordan Georgiev Oct 19 '17 at 10:19