java.lang.ClassNotFoundException: org.postgresql.Driver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5337859/
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
java.lang.ClassNotFoundException: org.postgresql.Driver
提问by Tony
Whenever I build my project as jar(via NetBeans) it seems that it does not include the postgresql driver library. I remember doing it before without any problems on previous versions of NetBeans and Drivers. I cmd run something like:
每当我将我的项目构建为 jar(通过 NetBeans)时,它似乎不包含 postgresql 驱动程序库。我记得以前在以前版本的 NetBeans 和驱动程序上没有任何问题。我 cmd 运行类似:
C:\Users\Username>java -jar "C:\Users\Username\Documents\NetBeansProjects\OrdersImport\dist\OrdersImport.jar" C:\orders\sometextfile.txt
Should there be something extra to include in this line to add postgresql-9.0-801.jdbc4.jar? I did include library to lib inside of the project and it does work without any troubles if I run it from NetBeans directly. I've looked at my previous project where it did work, seems to be everything same, pathetic I just can't remember, help please.
是否应该在这一行中添加一些额外的内容来添加 postgresql-9.0-801.jdbc4.jar?我确实在项目内部将库包含到 lib 中,如果我直接从 NetBeans 运行它,它确实可以正常工作。我看过我以前的项目,它确实有效,似乎一切都一样,可悲的是我不记得了,请帮忙。
回答by a_horse_with_no_name
There should be an entry in your MANIFEST.MF file that references the Postgres driver. And the driver needs to copied so that it's reachable from the real jar files location.
您的 MANIFEST.MF 文件中应该有一个引用 Postgres 驱动程序的条目。并且需要复制驱动程序,以便可以从真正的 jar 文件位置访问它。
So your MANIFEST.MF needs to include something like this:
所以你的 MANIFEST.MF 需要包含这样的内容:
Class-Path: lib/postgresql-9.0-801.jdbc4.jar
类路径:lib/postgresql-9.0-801.jdbc4.jar
If the JDBC driver is part of your NetBeans project, NetBeans should have copied it to dist/lib.
如果 JDBC 驱动程序是您的 NetBeans 项目的一部分,NetBeans 应该已将它复制到 dist/lib。
If you don't want to change the manifest file (or cannot), you need to manually reference all needed libraries on the command line. But then you cannot use the -jar
option any longer:
如果您不想(或不能)更改清单文件,则需要在命令行上手动引用所有需要的库。但是您不能再使用该-jar
选项:
java -cp postgresql-9.0-801.jdbc4.jar;OrdersImport.jar com.mypackage.MyMain C:\orders\sometextfile.txt
java -cp postgresql-9.0-801.jdbc4.jar;OrdersImport.jar com.mypackage.MyMain C:\orders\sometextfile.txt
Remember that you have to specify the main class when using -cp or -classpath
请记住,使用 -cp 或 -classpath 时必须指定主类
回答by Kai G
You would have to add the postgre jar to your classpath:
您必须将 postgre jar 添加到您的类路径中:
C:\Users\Username>java -classpath "location of postgresql-9.0-801.jdbc4.jar" -jar "C:\Users\Username\Documents\NetBeansProjects\OrdersImpo rt\dist\OrdersImport.jar" C:\orders\sometextfile.txt