简体   繁体   English

无法通过class.forName(“ com.mysql.jdbc.Driver”)加载com.mysql.jdbc.Driver类

[英]Couldn't load class com.mysql.jdbc.Driver by class.forName(“com.mysql.jdbc.Driver”)

I'm doing simple java application that interacts with mySQL database. 我正在做与mySQL数据库交互的简单Java应用程序。 It should run on the Ubuntu Server. 它应该在Ubuntu服务器上运行。 I'm developing it in the Intellij IDEA, and it works great there. 我正在Intellij IDEA中开发它,并且在这里很好用。

However, when I generate jar file and launch it in the Ubuntu command line (both server and my home PC running ubuntu), nothing works. 但是,当我生成jar文件并在Ubuntu命令行(服务器和运行ubuntu的家用PC)中启动它时,没有任何效果。

I simply type java -jar %my_jar_filename% and get the error 我只需键入java -jar %my_jar_filename%并得到错误

"Couldn't load main class ... Class.forName("com.mysql.jdbc.Driver")" “无法加载主类... Class.forName(“ com.mysql.jdbc.Driver”)“

I know that since java 5(i'm not sure here which version, I use 8) it's not necessary to write it, but if I don't write it I get 我知道,由于java 5(我不确定在这里使用哪个版本,我使用8),所以没有必要编写它,但是如果我不编写它,我会得到

"No suitable driver found" “找不到合适的驱动程序”

I googled this problem and the only solution I found was to make sure the driver actually exist on the PC and add that Class.forName(""); 我搜索了这个问题,发现的唯一解决方案是确保驱动程序确实存在于PC上并添加Class.forName("");

It doesn't help me. 这对我没有帮助。 Driver exists, because in the IDE program works. 驱动程序存在,因为在IDE程序中有效。 I think the trouble is with java options. 我认为麻烦在于java选项。 I tried to add mysql connector via java -cp , 我试图通过java -cp添加mysql连接器,

but it gave me the same error and even tried to compile the program in terminal from source via javac, but also the same issue. 但是它给了我同样的错误,甚至试图通过javac从源代码在终端中编译程序,但是还是同样的问题。 On my PC mysql connector is in the folder usr/share/java/ 在我的PC上,mysql连接器位于usr/share/java/文件夹中

The server where application should finally work has the same settings What am I doing wrong? 应用程序最终应在其中运行的服务器具有相同的设置我在做什么错?

**SOLVED:**Thanks everybody The solution was quite simple. **已解决:**谢谢大家,解决方案非常简单。 This answer helped https://stackoverflow.com/a/45303637/9184305 这个答案帮助https://stackoverflow.com/a/45303637/9184305

This looks like problem with class path , it may not have h jar file . 这看起来像类路径问题,它可能没有h jar文件。 I would suggest to use maven and create maven project and build jar file. 我建议使用maven并创建maven项目并生成jar文件。 Maven will take care of the dependencies and packaging. Maven将处理依赖关系和打包。

This could be because you're using the wrong driver - we use this class instead: 这可能是因为您使用了错误的驱动程序-我们改用此类:

com.mysql.cj.jdbc.Driver

Reading your description, you might also have the problem that your code is working in your IDE but not when you try to run it from the jar / command line. 阅读您的描述时,您可能还会遇到这样的问题:您的代码可以在IDE中运行,但是当您尝试从jar /命令行运行它时却不能。

In that case, 在这种情况下,

  1. how are you creating your jar? 您如何创建罐子?
  2. do you deploy all your dependencies along with your jar? 您是否将所有依赖项与jar一起部署?
  3. do you have a MANIFEST.MF file that indicates your Main class? 您是否有一个MANIFEST.MF文件指示您的主班?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 的Class.forName(“com.mysql.jdbc.Driver”)。的newInstance() - Class.forName(“com.mysql.jdbc.Driver”).newInstance() ClassNotFoundException Class.forName(“ com.mysql.jdbc.Driver”); - ClassNotFoundException Class.forName(“com.mysql.jdbc.Driver”); Class.forName(“ com.mysql.jdbc.Driver”)中的ClassNotFoundException; - ClassNotFoundException from Class.forName(“com.mysql.jdbc.Driver”); 如何保护Class.forName(“com.mysql.jdbc.Driver”)? - How to secure Class.forName("com.mysql.jdbc.Driver)? Class.forName("com.mysql.jdbc.Driver") 没有在该类的 jar 文件中查找 - Class.forName("com.mysql.jdbc.Driver") is not looking in jar file for the class Class.forName(“ com.mysql.jdbc.Driver”)在Raspberry Pi上不起作用 - Class.forName(“com.mysql.jdbc.Driver”) does not work on Raspberry Pi 这究竟做了什么Class.forName(“com.mysql.jdbc.Driver”)。newInstance(); - what exactly does this do Class.forName(“com.mysql.jdbc.Driver”).newInstance(); 与Java applet相关的Class.forName(“ com.mysql.jdbc.Driver”)。newInstance()中的ClassNotFoundException - ClassNotFoundException in Class.forName(“com.mysql.jdbc.Driver”).newInstance() related to java applet 我必须在Java中的每个方法中编写Class.forname(“ com.mysql.jdbc.Driver”)吗? - do i have to write Class.forname(“com.mysql.jdbc.Driver”) in every method in java? 更好的理解 - Class.forName(“com.mysql.jdbc.Driver”)。newInstance(); - Better understaning - Class.forName(“com.mysql.jdbc.Driver”).newInstance ();
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM