Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8370481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:55:19+00:00 2026-06-09T13:55:19+00:00

I have a simple example which tries to connect to a database and I

  • 0

I have a simple example which tries to connect to a database and I could only do it when I unjarred the mysql*.jar file in the working directory.

  1. I tried to export the /path/to/mysql.jar.file to $PATH and $CLASSPATH and that didn’t work.
  2. I tried to use include the path like this, but it didn’t work:
    javac -cp “.:/usr/share/java/” HelloWorld.java

What could be the issue. Why is it not finding the jar file?

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:55:20+00:00Added an answer on June 9, 2026 at 1:55 pm

    You need to set up the classpath when running the JVM as well as running the compiler. The classpath must include the JAR itself, not the folder containing the JAR.

    Your line

    javac -cp ".:/usr/share/java" HelloWorld.java
    

    shows that you are passing a classpath to javac, the Java compiler, but not the JVM.

    Let’s use the following simple class, which just connects to MySQL and then disconnects again:

    import java.sql.*;
    
    public class HelloWorld {
        public static void main(String[] args) throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "password");
            System.out.println("Got connection");
            con.close();
        }
    }
    

    Here’s what happens when I come to run it:

    C:\Users\Luke\Java>echo %CLASSPATH%
    .;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip;C:\Users\Luke\stuff\ojdbc6.jar
    
    C:\Users\Luke\Java>java HelloWorld
    Exception in thread "main" 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 HelloWorld.main(HelloWorld.java:5)
    

    In this case, the MySQL driver JAR isn’t on the classpath, so we get an error when we attempt to load the driver class (com.mysql.jdbc.Driver).

    If I add the driver JAR to the classpath using the -cp command-line switch (I have a copy of this JAR in the current directory), it works:

    C:\Users\Luke\Java>java -cp .;mysql-connector-java-5.1.18-bin.jar HelloWorld
    Got connection
    

    (I’m on Windows, so I use ; as a path separator. You appear to be using some dialect of Unix, so you should use : instead.)

    Similarly, if we add the JAR to the CLASSPATH environment variable, it also works:

    C:\Users\Luke\Java>set CLASSPATH=%CLASSPATH%;mysql-connector-java-5.1.18-bin.jar
    
    C:\Users\Luke\Java>java HelloWorld
    Got connection
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Backbone view which uses jQuery UI's buttons. For example I
I have a simple java project (adapted from the example here ), which is
I have this simple example I can't seems to get working : MERGE INTO
I have a simple application which was working fine until I added some code
Here is example which I have try <?php include 'spider/classes/simple_html_dom.php'; $html = new simple_html_dom();
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
I have a simple example ( SVG source ) looks like you can see
so I have a simple example--a fully crossed three treatment three context experiment, where
Does anyone have a simple example of implementing an async validation rule in csla?
I have a very simple example. A button on the bottom of the screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.