0

I'm doing simple java application that interacts with mySQL database. It should run on the Ubuntu Server. I'm developing it in the Intellij IDEA, and it works great there.

However, when I generate jar file and launch it in the Ubuntu command line (both server and my home PC running ubuntu), nothing works.

I simply type java -jar %my_jar_filename% and get the error

"Couldn't load main class ... 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

"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("");

It doesn't help me. Driver exists, because in the IDE program works. I think the trouble is with java options. I tried to add mysql connector via java -cp,

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. On my PC mysql connector is in the folder 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

Pavlo Kovalov
  • 91
  • 1
  • 10
  • Perhaps the driver jar is missing from the runnable package you have created. Check that by looking inside the jar. – S.K. Sep 19 '18 at 04:42
  • did this work for you? https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database – user1506104 Sep 19 '18 at 04:43
  • @user1506104 no, as in IDE standard way of connection to database works perfectly, and I’d like to fix the problem with dependency rather then connection – Pavlo Kovalov Sep 19 '18 at 19:29

2 Answers2

0

This looks like problem with class path , it may not have h jar file . I would suggest to use maven and create maven project and build jar file. Maven will take care of the dependencies and packaging.

Tejas Garde
  • 337
  • 2
  • 13
0

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.

In that case,

  1. how are you creating your jar?
  2. do you deploy all your dependencies along with your jar?
  3. do you have a MANIFEST.MF file that indicates your Main class?
Capn Sparrow
  • 2,030
  • 2
  • 15
  • 32