I'm packaging my java program as a jar..it includes mysql-connector.jar..it runs fine when i run it in eclipse..now i need to distriute the jar file..I packaged the jar file and when I run it through command line using java -jar myprogram.jar, it throws ClassNotFoundException: com.mysql.jdbc.Driver
could anyone please tell me what is going wrong??

my manifest looks like this

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
    <classpathentry kind="lib" path="lib/mysql-connector-java-5.1.14-bin.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

Dani AI

Generated

Good catch by — the ClassNotFoundException means the MySQL driver classes weren’t visible to the JVM when you ran the JAR. The XML you posted is an Eclipse project file (IDE metadata), not the runtime MANIFEST.MF that the JVM uses. That explains why it worked inside Eclipse but failed with java -jar myprogram.jar.

What to do (practical options and why they work)

  • Produce a “fat” (uber) JAR so the connector classes are actually inside the runtime classpath. In Eclipse’s Runnable JAR exporter pick the option that extracts library classes into the generated JAR — this merges the driver classes so com.mysql.jdbc.Driver is present. Build tools (Maven Shade / Gradle Shadow) do the same reliably for repeatable builds.
  • Alternatively ship the connector JAR next to your app and either set the JAR’s MANIFEST.MF Class-Path (points to the external jar) or run with an explicit classpath instead of -jar. Note: java -jar ignores the -cp / -classpath argument, so use java -cp myprogram.jar:lib/* com.my.Main (Unix) or java -cp myprogram.jar;lib/* com.my.Main (Windows) if you go this route.

Quick checks and a small workaround

  • Inspect the runnable JAR to confirm the driver is present:
    jar tf myprogram.jar

    look for a com/mysql/jdbc/Driver.class entry.

  • As a temporary test, explicitly load the driver in code:
    Class.forName("com.mysql.jdbc.Driver");

    Modern JDBC drivers auto-register via ServiceLoader, but explicit loading can help diagnose classpath issues.

Notes and cautions

  • Avoid simply nesting a connector JAR inside your JAR without merging — the JVM won’t see it.
  • Keep an eye on duplicate drivers when deploying to containers or when using signed jars.

If the Runnable JAR export fixed it for you (as reported), the above explains why and how to reproduce/verify the fix.

Recommended Answers

All 2 Replies

You just go on your program you want to build as .jar.
Go File>Export>Runnable Jar and then you have options how to pack it...
Find what you want and it will automaticly include jdbc.driver in your .jar package...

thanks it worked..that was the only option i had

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.