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

  • Home
  • SEARCH
  • 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 6216283

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:12:33+00:00 2026-05-24T07:12:33+00:00

Trying to build a java application I am able to run the application successfully

  • 0

Trying to build a java application I am able to run the application successfully through Eclipse but I’m unable to run the jar file built by ant. I suspect my build.xml is to blame, I’m trying to get it right. Need help!

The mysql driver .jar file is in the lib/ directory in the project.

error:

$ java -jar dist/lib/MyProject.jar 
Loading Mysql JDBC driver...
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at com.test.bh1.bh.main(Unknown Source)

bh.java

package com.test.bh1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class bh {

    /**
     * @param args
     */
    public static void main(String[] args) {        
        System.out.println("Loading Mysql JDBC driver...");
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception e) {
            msg ("Error instantiating mysql jdbc driver. E=" + e.getMessage());
            // bail. 
            return;
        }

        msg ("Successfully instantiated mysql jdbc driver. ");

        Connection con = null;
        String url = "jdbc:mysql://servername:3306/databasename";
        String user = "username";
        String password = "password";

        try {            
            con = DriverManager.getConnection(url, user, password);
            Statement st = con.createStatement();
            ResultSet result = st.executeQuery("SELECT VERSION()");

            if (result.next()) {
                msg(result.getString(1));
            }

            con.close();

        } catch (SQLException ex) {
            msg(ex.getMessage());
        }

    }

    private static void msg (String m) {
        System.out.println(m);
    }

}

build.xml

<project name="TestProj1" default="dist" basedir=".">
    <description>
        Build File For This Project
    </description>
  <!-- set global properties for this build -->
  <property name="src"      location="src"/>
  <property name="build"    location="build"/>
  <property name="dist"     location="dist"/>
  <property name="user.name"    location="Brad Hein (CQA)"/>
  <property name="lib.dir"  location="lib/"/>
  <property name="mysql.jar"    location="mysql-connector-java-5.0.8-bin.jar"/>
  <property name="appName.jar"  location="${dist}/lib/MyProject.jar"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
    </path>

  <path id="build.classpath">
    <pathelement location="${mysql.jar}"/>
    <pathelement path="${appName.jar}"/>
  </path>


  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}" includeantruntime="false">
           <classpath refid="build.classpath"/>
    </javac>

    <!-- Copy dependency libraries to the build path so they get packaged up in the distro jar file. -->
    <copy todir="${build}/lib">
         <fileset dir="${lib.dir}">
             <include name="*.jar"/>
     </fileset>
    </copy>
  </target>


  <target name="dist" depends="compile"
        description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>

    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${appName.jar}" basedir="${build}">
    <!-- Creates a manifest file in the jar-->
        <manifest>
                <attribute name="Built-By" value="${user.name}" />
                <attribute name="Class-Path" value="./lib/mysql-connector-java-5.0.8-bin.jar"/>
                <attribute name="Main-Class" value="com.test.bh1.bh"/>
        </manifest>
    </jar>
  </target>

  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
  • 0 0 Answers
  • 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

Sidebar

Related Questions

I'm trying to build a java application with gcj but getting the error below.
I am trying to migrate a Java application built by Ant to Maven2. Among
Title: Eclipse plug-in development Unable to instantiate class due to java.lang.NoClassDefFoundError: Trying to build
I'm trying to run a sample Java application from the command promopt but I'm
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
I'm trying to build a Java regular expression to match .jar! The catch is
I'm trying to build an Java EE 6-application on GlassFish V3, using JSF 2.0,
I'm trying to build something (ultimately a gem but for now an application) that
I'm trying to debug my Java application in Eclipse however when I hit a
I'm trying to build a small Java app for connecting to an application called

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.