0

So.. this is driving me nuts. Day 2 of trying to get mysql working on Java and it's still not working.

I'm developing a Java application on Windows using IntelliJ IDEA (latest version), exporting it as JAR (Build artifact) and running it on a Ubuntu server.

Choosing Run in IDEA (Windows) causes the library (mysql-connector-java-5.0.8) to be included, as the program cannot find the database. However, when I upload the JAR (artifact), I'm getting:

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

I've tried just about anything, and after spending almost 16 hours on this, I'm out of options and getting really frustrated. I've read most of the posts here and tried all of them, none of them worked.

Looking at the size of the JAR, the library is being included; just not recognized by Ubuntu. Perhaps I need to use some kind of command-parameter to enable the library? Here's how I run the JAR:

java -jar output.jar

Thanks in advance!

Someone
  • 21
  • 4

3 Answers3

2

I think I've fixed this, but I don't exactly know how. Under Project Structure -> Artifacts I've right clicked the library (JAR) and selected 'Extract Into Output Root'. I don't know if this has any concequences but at least for now this is working and I can continue development..

For the record, here's my .IML file:

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/production" />
    <output-test url="file://$MODULE_DIR$/test" />
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
      <excludeFolder url="file://$MODULE_DIR$/.idea" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library" exported="">
      <library>
        <CLASSES>
          <root url="file://$MODULE_DIR$/libs" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
        <jarDirectory url="file://$MODULE_DIR$/libs" recursive="false" />
      </library>
    </orderEntry>
    <orderEntry type="library" name="mysql:mysql-connector-java:5.1.36" level="project" />
  </component>
</module>
Someone
  • 21
  • 4
0

nesting jar files is not supported by Java. You need to ensure that all required libraries are on your classpath as separate jars or directories.

If you're making an enterprise application, your application server should typically provide the JDBC drivers for you which means you need to configure that properly and set up your application to retrieve a connection using jndi.

jwenting
  • 5,505
  • 2
  • 25
  • 30
0

Please check that your libraries are included in the classpath of your MANIFEST.MF file.

For example:

Manifest-Version: 1.0
Class-Path: mysql-connector
Main-Class: some.package.mainClass
Scorpio
  • 2,309
  • 1
  • 27
  • 45
Martyn
  • 1
  • 1