1

I wrote an app and it works fine by me. But on other pc-s it throws an exception.

java.sql.SQLException: No suitable driver found for jdbc:sqlserver

I give it (only the app.jar file) to my college, and he gets this error. I give him a jdbcdriver.jar file and, nothing changed. However I use jre7, I added a line to the code:

    Class.forName("com.mysql.jdbc.Driver");

And than he gets another exception:

Error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

What can I do to run this app properly on other pc-s?

Tehsil
  • 131
  • 1
  • 9
user3411294
  • 29
  • 1
  • 3
  • You need to google sometimes before posting your query. Atleast we will all know that you have put some efforts. Possible duplicate of http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse and http://stackoverflow.com/questions/18158864/runtime-error-java-lang-classnotfoundexception-com-mysql-jdbc-driver – Balwinder Singh Apr 17 '14 at 09:17
  • you need to use the driver.jar in the classpath. If you are running through command line, use >java Main -classpath "driver.jar" – Hirak Apr 17 '14 at 09:18
  • Why are you loading the MySQL driver when you want to connect to SQL Server? – Mark Rotteveel Apr 17 '14 at 10:56
  • Can I connect to sql server without using mysql driver? – user3411294 Apr 17 '14 at 12:10

1 Answers1

0

Put the jar file, containing the MySql Driver on the classpath when starting your application.

The switch is called -cp or -classpath like this:

java -cp ".;mysql.jar" my.package.Main

assuming you start your application from the root directory which is named my.package.Main. The classpath separator is the one for Windows. For unix based OSs you need to use : i guess.

Harmlezz
  • 7,972
  • 27
  • 35
  • What sould I put instead of my.package.Main eg.: my app path is: D:\app.jar this is not enough: java -cp D:\mysql-connector-java-5.1.23-bin.jar d:TCfinder.jar – user3411294 Apr 17 '14 at 09:29
  • What is the fully qualified name of your class containing the `public static void main(String[] args)` method? Use that fully qualified name instead. – Harmlezz Apr 17 '14 at 09:31
  • TCfinder is my main class – user3411294 Apr 17 '14 at 09:32
  • I set "java -cp D:\mysql-connector-java-5.1.23-bin.jar TCfinder -jar D\:TCfinder.jar " and I still get Error: Could not find or load main class TCfinder – user3411294 Apr 17 '14 at 09:51
  • You may try: `java -cp "D:\mysql-connector-java-5.1.23-bin.jar" -jar D\:TCfinder.jar` instead or `java -cp "D:\mysql-connector-java-5.1.23-bin.jar;D\:TCfinder.jar" TCfinder`. – Harmlezz Apr 17 '14 at 10:58
  • java -cp "D:\mysql-connector-java-5.1.23-bin.jar" -jar D:\TCfinder.jar D:\sf.c 149 There are 2 aguments in the end. – user3411294 Apr 17 '14 at 11:26
  • and this: java -cp "D:\mysql-connector-java-5.1.23-bin.jar; D:\TCfinder.jar D:\sf.c 149" TCfinder do not work either, could not find or load main class TCfidner – user3411294 Apr 17 '14 at 11:32
  • How did you started your main class in the first place? Before I posted my answer? – Harmlezz Apr 17 '14 at 11:34
  • public class TCfinder { public static void main(String[] args) throws Exception { ... – user3411294 Apr 17 '14 at 11:46
  • I meant which command have you used to start it (in the console / command line interface)? – Harmlezz Apr 17 '14 at 12:55
  • oh, I start it form ultraedit,it's a tool and this is the command: java -jar D:\TCfinder.jar %f %line% – user3411294 Apr 17 '14 at 13:07
  • The answer was really easy: the jdbc.jar file should be in a lib folder next to the app.jar file. eg.: d:\application\app.jar d:\application\lib\jdbc_driver.jar And start it is simple: java -jar d:\application\app.jar Thanks for all the answers! – user3411294 Apr 17 '14 at 14:24