开发者

CLASSPATH on Mac, and how does Mac look for mysql-connector-java-bin.jar

开发者 https://www.devze.com 2022-12-24 17:48 出处:网络
Is there a default classpath on Mac OS X 10.6? When I echo $CLASSPATH开发者_高级运维, nothing would show up. In my .profile, I only see PATH variable being set. My problem is that My servlet cant seem

Is there a default classpath on Mac OS X 10.6? When I echo $CLASSPATH开发者_高级运维, nothing would show up. In my .profile, I only see PATH variable being set. My problem is that My servlet cant seem to find a suitable driver to connect to the mysql server. I use Eclipse, with Glassfish v3 and MAMP for MYSQL server.


There are several methods of getting JARs to be seen by Java on Mac OS X:

  • Place it in /Library/Java/Extensions
  • Create/edit the CLASSPATH environment variable
  • Specify the classpath explicitly with the -cp option.

The CLASSPATH environment variable is not set by default, however, you can set it if you so choose. Be aware, however, that any environment variables that you set in ~/.profile will only take effect within your Terminal session and will not affect any GUI applications. If you want to set environment variables so that they affect your GUI applications, you can create a file named ~/.MacOSX/environment.plist that includes your environment variables. Any changes made to that file will take effect when you next login.

As has been observed, placing JARs in the extensions folder or modifying the CLASSPATH environment variable are generally bad ideas since they can lead to dependency hell. A better way is to bundle your JARs with your artifact and to set the metadata appropriately so that they are on your artifact's classpath. If you use Apache Maven2 to build your artifact, you can have it automatically download as well as bundle any thirdparty dependencies and set the classpath appropriately for your artifact.


Do not use the CLASSPATH environment variable. This is portability trouble. The whole environment variable is a mistake of the Sun guys. It's only useful for starters, but certainly not in real world. This would only confuse the starters more afterwards. Besides, appservers (and IDE's) completely ignores this environment variable. Do not put the libraries in the library of JRE or JDK. This is portability trouble as well. If you upgrade the JRE/JDK or run the application somewhere else, it won't work anymore.

In webapplications, you normally just drop webapp-specific 3rd party libraries in Webapp/WEB-INF/lib. This folder is covered by the webapp's default classpath. If those libraries are rather appserver-specific (e.g. JDBC driver is required to create a JNDI datasource which is managed by the appserver), then you need to drop them in Appserver/lib. This folder is covered by the appserver's default classpath. In case of Glassfish, you need to put it more specifically in the domain-specific /lib folder, e.g. glassfish/domains/<domainname>/lib.


I struggled a lot with this one. Try adding the appserv-rt.jar (located on Glassfish lib directory) to your project's build path. (I't will drag all it's dependancies if you want to avoid this first create a library with the jar and then add the library to your build path.

0

精彩评论

暂无评论...
验证码 换一张
取 消