3

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.

azrosen92
  • 8,357
  • 4
  • 26
  • 45

3 Answers3

8

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.

Alex
  • 13,811
  • 1
  • 37
  • 50
3

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

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

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.

Dan
  • 10,990
  • 7
  • 51
  • 80
  • 1
    Comment on 1) - it is there on that specific machine for that specific profile. In other words the project will only build properly on one computer for one specific user. Basically the same for 2), but that is even worse as you're putting jars where they are not supposed to be, also influencing all other applications that use the same runtime. – Gimby Nov 19 '12 at 16:40