java 如何设置 jdbc 驱动程序类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14804184/
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
How to set jdbc driver classpath
提问by Cocest
When I copy mysql jdbc driver to JDK's\jre\lib\ext, it execute perfectly well. Now, I want to use the jdbc by specifying its classpath to environment variable. But, after doing so, my program throws exception :
当我将 mysql jdbc 驱动程序复制到 JDK 的\jre\lib\ext 时,它执行得很好。现在,我想通过将 jdbc 的类路径指定为环境变量来使用 jdbc。但是,这样做之后,我的程序抛出异常:
"java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/books"
“java.sql.SQLException:找不到适合 jdbc:mysql://localhost/books 的驱动程序”
How do I set the classpath?
如何设置类路径?
回答by duffymo
You should not be putting ANY JARs in the jre/lib/ext folder.
您不应该在 jre/lib/ext 文件夹中放置任何 JAR。
You set the CLASSPATH using the -classpath option on javac.exe when you compile and java.exe when you run. Make sure that your code and all 3rd party JARs are in the CLASSPATH when you compile and run. Windows uses the semi-colon as the separator; Linux uses colon.
编译时使用 javac.exe 上的 -classpath 选项设置 CLASSPATH,运行时使用 java.exe 上的选项。编译和运行时,请确保您的代码和所有 3rd 方 JAR 都在 CLASSPATH 中。Windows 使用分号作为分隔符;Linux 使用冒号。
Maybe you need to start here:
也许你需要从这里开始:
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
回答by jbx
You can include any jar files you need by specifying them in the java command with the -cp
switch (which is identical to -classpath
. For example if the JDBC driver's name is 'myjdbc.jar' then you would execute your program as follows:
您可以通过在带有-cp
开关的 java 命令中指定它们来包含您需要的任何 jar 文件(与 相同-classpath
。例如,如果 JDBC 驱动程序的名称是“myjdbc.jar”,那么您将按如下方式执行程序:
java -cp myjdbc.jar your.package.YourClass
java -cp myjdbc.jar your.package.YourClass
If you have more jar files, you can separate them with a semi-colon on Windows or colon on Linux/Unix. Commonly the current directly is also included, and we put all needed jar files in a /lib
folder, so it would look something like this (on Windows):
如果你有更多的 jar 文件,你可以在 Windows 上用分号或 Linux/Unix 上的冒号分隔它们。通常当前的直接也包括在内,我们将所有需要的jar文件放在一个/lib
文件夹中,所以它看起来像这样(在Windows上):
java -cp .;lib/myjdbc.jar your.package.YourClass
java -cp .;lib/myjdbc.jar your.package.YourClass
Also, if you have lots of jar files, it would be more convenient to put them all in the /lib
folder and have something like this:
此外,如果您有很多 jar 文件,将它们全部放在/lib
文件夹中并具有以下内容会更方便:
java -cp .;lib/* your.package.YourClass
java -cp .;lib/* your.package.YourClass