java.lang.ClassNotFoundException: org.postgresql.Driver
每当我将项目构建为 jar(通过 NetBeans)时,它似乎不包含 postgresql 驱动程序库。我记得以前在以前版本的 NetBeans 和驱动程序上这样做过,没有任何问题。我 cmd 运行类似的内容:
C:\Users\Username>java -jar "C:\Users\Username\Documents\NetBeansProjects\OrdersImport\dist\OrdersImport.jar" C:\orders\sometextfile.txt
是否应该在这一行中包含一些额外的内容来添加 postgresql-9.0-801.jdbc4.jar?我确实在项目内部包含了 lib 库,如果我直接从 NetBeans 运行它,它确实可以正常工作。我看过我以前的项目,它确实有效,似乎一切都一样,可悲的是我不记得了,请帮忙。
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MANIFEST.MF 文件中应该有一个引用 Postgres 驱动程序的条目。并且需要复制驱动程序,以便可以从真实的 jar 文件位置访问它。
因此,您的 MANIFEST.MF 需要包含如下内容:
类路径:lib/postgresql-9.0-801.jdbc4.jar
如果 JDBC 驱动程序是 NetBeans 项目的一部分,则 NetBeans 应该将其复制到 dist/lib。
如果您不想(或不能)更改清单文件,则需要在命令行上手动引用所有需要的库。但是您不能再使用 -jar 选项:
java -cp postgresql-9.0-801.jdbc4.jar;OrdersImport.jar com.mypackage.MyMain C:\orders\sometextfile。 txt
请记住,使用-cp或-classpath时必须指定主类
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.
So your MANIFEST.MF needs to include something like this:
Class-Path: 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.
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: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
您必须将 postgre jar 添加到您的类路径中:
You would have to add the postgre jar to your classpath: