Mac 上的 CLASSPATH,以及 Mac 如何查找 mysql-connector-java-bin.jar
Mac OS X 10.6 上有默认类路径吗?当我echo $CLASSPATH
时,什么也不会显示。在我的 .profile
中,我只看到设置了 PATH
变量。我的问题是我的 servlet 似乎无法找到合适的驱动程序来连接到 mysql 服务器。我使用 Eclipse、Glassfish v3 和用于 MYSQL 服务器的 MAMP。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几种方法可以让 JAR 在 Mac OS X 上被 Java 看到:
默认情况下未设置 CLASSPATH 环境变量,但您可以选择设置它。但请注意,您在
~/.profile
中设置的任何环境变量只会在终端会话中生效,不会影响任何 GUI 应用程序。如果您想要设置环境变量以影响您的 GUI 应用程序,您可以创建一个名为~/.MacOSX/environment.plist
的文件,其中包含您的环境变量。对该文件所做的任何更改都将在您下次登录时生效。正如我们所观察到的,将 JAR 放在扩展文件夹中或修改 CLASSPATH 环境变量通常是坏主意,因为它们可能会导致依赖地狱。更好的方法是将 JAR 与工件捆绑在一起,并适当地设置元数据,以便它们位于工件的类路径上。如果您使用 Apache Maven2 来构建您的工件,您可以让它自动下载并捆绑任何第三方依赖项,为您的工件适当设置类路径。
There are several methods of getting JARs to be seen by Java on Mac OS X:
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.
不要不要使用
CLASSPATH
环境变量。这是可移植性的问题。整个环境变量是Sun 人的错误。它仅对初学者有用,但在现实世界中肯定没有用。这只会让之后的首发者更加困惑。此外,应用程序服务器(和 IDE)完全忽略此环境变量。不要将库放在 JRE 或 JDK 的库中。这也是可移植性的问题。如果您升级 JRE/JDK 或在其他地方运行应用程序,它将不再工作。在 Web 应用程序中,您通常只需将特定于 Web 应用程序的第 3 方库放入
Webapp/WEB-INF/lib
中。该文件夹由 web 应用程序的默认类路径覆盖。如果这些库是特定于应用程序服务器的(例如,需要 JDBC 驱动程序来创建由应用程序服务器管理的 JNDI 数据源),那么您需要将它们放入Appserver/lib
中。该文件夹由应用程序服务器的默认类路径覆盖。对于 Glassfish,您需要将其更具体地放在特定于域的/lib
文件夹中,例如glassfish/domains//lib
。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 inAppserver/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
.我在这个问题上挣扎了很多。尝试将 appserv-rt.jar(位于 Glassfish lib 目录)添加到项目的构建路径中。 (如果您想避免这种情况,我不会拖动它的所有依赖项,首先使用 jar 创建一个库,然后将该库添加到您的构建路径中。
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.