0

I wrote a simple multiplayer JAVA app that can connect to MySQL using JDBC driver. I ended up packing it up in .jar file. When I was testing on my local machine MacOS, it was fine. (I don't exactly remember all the steps to set up JDBC driver, because it was year ago when I wrote this app). The app at this time connects to an external database on another server (I didn't change that because it had no problems). It would be another error if it was a problem with the remote database.

Now, when I need to put it on production on Windows Server 2003, I keep getting the standard error :

 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
And I don't get it, because I think I've done the steps right:
0. Installed Java
1. downloaded this: http://dev.mysql.com/downloads/connector/j/
2. added it to the same folder where the app.jar is
3. wrote in cmd: set CLASSPATH =.;C:\Documents and Settings\Administrator\Desktop\tvplan_exchange\mysql-connector-java-5.1.28-bin.jar
4. tested the variable: echo %CLASSPATH% and it showed the same path
5. runned the app: java -jar app.jar
6. got the error:
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at svagers.Db.connect(Db.java:21)
    at svagers.Server.main(Server.java:58)
    MYSQL ERRORcom.mysql.jdbc.Driver

Well, for those who think that using app like this is a security error - no worries, that is not a problem, I just need it to be running not to consider risks :)

This is the environment: OS: Windows Server 2003 (Standard Edition, Service Pack 2) Java: 1.7.0_45 (SE Runtime Environment

Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
LokoTerrorita
  • 117
  • 2
  • 13
  • 1
    Did you load your MySQL driver by calling: `Class.forName("com.mysql.jdbc.Driver");`? – Branislav Lazic Jan 08 '14 at 21:46
  • This is the line: Class.forName(driver).newInstance(); where driver is com.mysql.jdbc.Driver – LokoTerrorita Jan 08 '14 at 21:54
  • And as I said - everything should be fine in the code, because I get it up and running on my computer (I used my computer as a server to connect from other computers for a developement period) – LokoTerrorita Jan 08 '14 at 21:55
  • Then you didn't add properly your mysql-connector dependency to classpath. – Branislav Lazic Jan 08 '14 at 21:59
  • Can you tell me how to do that properly? I added it like I wrote: set CLASSPATH =.;C:\Documents and Settings\Administrator\Desktop\tvplan_exchange\mysql-connector-java-5.1.28-bin.jar and the directory exists, and the file with that name exists too. What could be wrong? – LokoTerrorita Jan 08 '14 at 22:06

1 Answers1

2

I was going to say "run the app with java -cp %CLASSPATH% -jar app.jar" but then remembered you can't have both: you use either -jar or -cp. See this answer.

Community
  • 1
  • 1
vanOekel
  • 6,358
  • 1
  • 21
  • 56