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

别说谁变了你拦得住时间么 提交于 2019-12-06 09:01:13

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2509138/classpath-on-mac-and-how-does-mac-look-for-mysql-connector-java-bin-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!