Mac上的CLASSPATH,以及Mac如何查找mysql-connector-java-bin.jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2509138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:37:28  来源:igfitidea点击:

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

javajdbcclasspath

提问by Thang Pham

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 PATHvariable 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 v3and MAMPfor MYSQL server.

Mac OS X 10.6 上有默认的类路径吗?当 I 时echo $CLASSPATH,什么都不会出现。在我的 中.profile,我只看到PATH设置了变量。我的问题是我的 servlet 似乎找不到合适的驱动程序来连接到 mysql 服务器。我使用Eclipse,Glassfish v3MAMPMYSQL 服务器。

采纳答案by Michael Aaron Safyan

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

有几种方法可以让 Java 在 Mac OS X 上看到 JAR:

  • Place it in /Library/Java/Extensions
  • Create/edit the CLASSPATH environment variable
  • Specify the classpath explicitly with the -cp option.
  • 把它放在 /Library/Java/Extensions
  • 创建/编辑 CLASSPATH 环境变量
  • 使用 -cp 选项显式指定类路径。

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 ~/.profilewill 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.plistthat includes your environment variables. Any changes made to that file will take effect when you next login.

默认情况下不设置 CLASSPATH 环境变量,但是,如果您愿意,可以设置它。但是请注意,您设置的任何环境变量~/.profile只会在您的终端会话中生效,不会影响任何 GUI 应用程序。如果您想设置环境变量以便它们影响您的 GUI 应用程序,您可以创建一个名为的文件~/.MacOSX/environment.plist,其中包含您的环境变量。对该文件所做的任何更改将在您下次登录时生效。

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 Maven2to build your artifact, you can have it automatically download as well as bundle any thirdparty dependencies and set the classpath appropriately for your artifact.

正如所观察到的,将 JAR 放在扩展文件夹中或修改 CLASSPATH 环境变量通常是坏主意,因为它们会导致依赖地狱。更好的方法是将 JAR 与工件捆绑在一起,并适当地设置元数据,以便它们位于工件的类路径中。如果您使用Apache Maven2构建您的工件,您可以让它自动下载并捆绑任何第三方依赖项,并为您的工件适当设置类路径。

回答by BalusC

Do notuse the CLASSPATHenvironment 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 notput 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.

千万不能使用CLASSPATH环境变量。这是便携性的问题。整个环境变量是 Sun 人的错误。它只对初学者有用,但在现实世界中肯定没有用。这只会让初学者在事后更加困惑。此外,应用服务器(和 IDE)完全忽略了这个环境变量。千万不能把图书馆JRE或JDK的库。这也是便携性的问题。如果您升级 JRE/JDK 或在其他地方运行应用程序,它将不再起作用。

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 /libfolder, e.g. glassfish/domains/<domainname>/lib.

在 web 应用程序中,您通常只需将特定于 webapp 的 3rd 方库放入Webapp/WEB-INF/lib. 该文件夹包含在 webapp 的默认类路径中。如果这些库是特定于应用程序服务器的(例如,需要 JDBC 驱动程序来创建由应用程序服务器管理的 JNDI 数据源),那么您需要将它们放入Appserver/lib. 此文件夹由应用程序服务器的默认类路径覆盖。对于 Glassfish,您需要更具体地将其放在特定于域的/lib文件夹中,例如glassfish/domains/<domainname>/lib.

回答by BrainO2

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.

我在这个问题上挣扎了很多。尝试将 appserv-rt.jar(位于 Glassfish lib 目录中)添加到项目的构建路径中。(如果你想避免这种情况,我不会拖拽它的所有依赖项,首先用 jar 创建一个库,然后将该库添加到你的构建路径中。