sorin
sorin

Reputation: 170390

Where to install JAR files on OS X so other Java applications will find them?

I want to install different JAR files, like database JDBC drives on OS X so other Java applications will find them.

Where am I supposed to put them? I tried ~/Library/Java/Extensions but it doesn't seam to work.

Details:

OS X 10.8.2

java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

Note: I do not have control over how the application is initialized, but I do assume that the application does not incude it's own Java distribution and that it will start the OS installed versions (the one that is available from the command line).

Also, I do not have any CLASSPATH setup, and please remember that environment variables that are available to command line programs are not the same as the ones available to GUI apps on OS X.

I need to specify that I am trying this for my development machine and that this approach is not a way of deploying Java applications.

Upvotes: 10

Views: 32479

Answers (4)

user3338350
user3338350

Reputation: 89

Ok just my contribution... but I wanted to run my small database program at the command line but I needed mysql-connector-java-5.1.29-bin.jar in the class path to do so. I spent some time trying to figure it out and found the solution. I dropped the jar file in the Library/Java/Extensions folder and restarted my machine. Now the above says you should include the classpath when you are "executing" the program (java -cp ~/Library/Java/Extensions/foo.jar Bar), but it did not work for me. Instead I issued the command during the compile time and then executed my program:

$ javac -cp /Library/Java/Extensions/mysql-connector-java-5.1.29-bin.jar FirstExample.java  
$ java FirstExample

And this is what I got... query data from my database:

Connecting to database...
Creating statement...
ID: 1, Title: Sales Representative, First: Nancy, Last: Davolio
ID: 2, Title: Vice President, Sales, First: Andrew, Last: Fuller ID: 3, Title: Sales Representative, First: Janet, Last: Leverling
ID: 4, Title: Sales Representative, First: Margaret, Last: Peacock
ID: 5, Title: Sales Manager, First: Steven, Last: Buchanan
ID: 6, Title: Sales Representative, First: Michael, Last: Suyama
ID: 7, Title: Sales Representative, First: Robert, Last: King
ID: 8, Title: Inside Sales Coordinator, First: Laura, Last: Callahan
ID: 9, Title: Sales Representative, First: Anne, Last: Dodsworth

Goodbye!

Hope this helps

Upvotes: -3

Brian Agnew
Brian Agnew

Reputation: 272247

I will second Steve's comments and further suggest that you use Maven or Ivy for your dependency management.

The library management is per-project. It's much easier to manage updates and interdependencies. Maven (I don't know about Ivy) can tell you which libs are out of date, which conflict with each other etc. For each project you can specify precisely the versions you require without worrying whether changes will break other projects.

Upvotes: 1

Steve McLeod
Steve McLeod

Reputation: 52448

Please DON'T do this. Here's why: Other apps will break, because those JAR files will be loaded for every Java app.

In detail: I sell a Java app for Mac. It relies on having a specific recent version of a JAR file (for JFreeChart). Sometimes there is an older version of the same JAR file in the appropriate Extensions folder. This breaks my app.

But if you really want to do this: According to Apple's docs:

Extension Libraries

Java software on other platforms often makes use of the $JAVA_HOME/lib/ext directory within a JDK installation to store support class or jar files. While Java for Mac OS X also contains a lib/ext directory, developers should not directly modify it for the same reasons mentioned above. The /Library/Java/Extensions directory can be used for additional jar files or JNI libraries that need to be placed on the system classpath. For more controlled access, the ~/Library/Java/Extensions directory can be used for user-level installation of support libraries. Items placed in either of these directories do not need to be named in an application's classpath and will be available to all applications run under the respective scope (system-level or user-level, depending which directory is used).

Upvotes: 15

blomqvie
blomqvie

Reputation: 61

They have to be included in to the classpath of the application. If you execute it from the command line, then use e.g. "java -cp ~/Library/Java/Extensions/foo.jar Bar"

On the other hand, if you use an application server like tomcat, you can either package the jar inside the war of your application of place it into a shared library folder of the server.

Upvotes: 2

Related Questions