azrosen92
azrosen92

Reputation: 9057

Where can I put my java libraries so they will always be in the class path when using javac?

I'm using a mac, and I would like to know if there is a directory in which I can put all my java libraries that I use often so that I can compile/run the code that uses them without explicitly setting the classpath each time? I want to do something analogous to what a package manager would do in python, but it doesn't seem like there are any package managers for java.

Upvotes: 3

Views: 3075

Answers (3)

Dan
Dan

Reputation: 11069

Two options:

1 - set a CLASSPATH in your ~/.profile file. Then every time you open a Terminal, it will be there.

2 - You can also put the JAR files into the lib/ext directory for your JDK. See this article for the details on that although note that Apple recommends you not do this.

Upvotes: 0

Alex
Alex

Reputation: 13941

I'd strongly advise that you avoid doing this. While it might be convenient for you personally, it's terrible from a repeatability standpoint. If you have trouble with your code and want to send your project to somebody else to look at, they shouldn't have to reproduce your entire environment to be able to compile it.

For package management in Java, look at the Maven project. This will allow you to describe your project dependencies and have the tool automatically download the appropriate JARs and add them to your project's classpath.

Upvotes: 8

T.J. Crowder
T.J. Crowder

Reputation: 1074258

You can put them anywhere you like, and set the CLASSPATH environment variable so that it includes that path. javac uses the environment variable, so you don't have to specify it each time.

As you're using a Mac, I'll point you to this SO question and its answers about how to set environment variables. To include multiple directories in your CLASSPATH, separate them with the standard path separator (which I believe is : on Mac OS X).

Upvotes: 3

Related Questions