Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I've created little java application that connects to MySQL. I tested it on my local machine and it worked fine. I exported the Java project to Runnable jar file using Eclipse.

I copied the jar file on my server running Ubuntu 12.04 and ran it with the command

java -jar server.jar

and it threw the error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)

I understand that this class could not be found on the server. Then - how can I install this driver?

I have everything in folder /tvplan/ there's a server.jar file that is exported as Runnable jar also there is a folder lib which holds mysql-connector-java.jar and libintl.jar (both I got using sudo apt-get install libmysql-java , but when I run java -cp "lib/mysql-connector-java.jar;server.jar" Server bt it throws me an error Error: Could not find or load main class Server

share|improve this question
Copying the Mysql JDBC jar to the same directory should work for you. – Marcelo Aug 12 at 17:15
It is a runnable jar file, don't see how that can be a web application @LuiggiMendoza. – Marcelo Aug 12 at 17:17
@Marcelo didn't see the java -jar ... command – Luiggi Mendoza Aug 12 at 17:19

marked as duplicate by Brian Roach, Richard Sitze, Sindre Sorhus, Roman C, Rostyslav Dzinko Aug 13 at 10:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

If you use a jar file then the jar file must include a class-path line in the manifest. The line specifies the libraries/class to include. Your jar either does not do this at all, or has a wrong link.

Open the jar with an unzip program and open the manifest file to see which issue it is.

I suspect you used Eclipse's option to include the libraries (but not extract or repackage them) and therefor they're placed in a folder outside of the jar file, and that folder is not present on the linux machine. There are 3 options for the Eclipse exporter regarding library handling, the first 2 will produce a self contained Jar. The first option will break the license agreement on the mysql connector, but the 2nd will not. The 3rd will put the libraries in a folder that you need to copy along with the jar you made.

share|improve this answer
I cannot open the jar file with Archive utility (I'm on MacOS). And when I exported with Eclipse, there were only the jar file, no filders no nothing. Can you please provide step by step solution? – LokoTerrorita Aug 12 at 17:49
Either 1) Get "the unarchiver" from the app store to unarchive the jar file. Then see which issue you're having, or 2) Export the jar file using the 1st option for library handling. – Xabster Aug 12 at 17:56

Use the cp option of java to put the MySQL jdbc driver on the classpath, for instance:

java -cp ".;lib/mysql-connector-java-version-bin.jar;server.jar" package.Server

if your jdbc driver resides under lib directory and replace package.Server with the fully qualified name of your main class.

share|improve this answer
-cp does not work with -jar. – Xabster Aug 12 at 17:20
@Xabster thanks, I edited it – Katona Aug 12 at 17:29
So this should work? It sais Error: Could not find or load main class package.Server – LokoTerrorita Aug 12 at 17:52
@LokoTerrorita as I mentioned in the answer, you have to replace package.Server with your main class (which contains the main method) – Katona Aug 12 at 17:54
1  
@LokoTerrorita if you put everything in (default package) then there is no package name, just use the name of the class. Btw, using default packages in java is a bad practice, you should avoid it – Katona Aug 12 at 18:07
show 4 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.