【问题标题】:ant built version of application fails to find dependent jarant构建版本的应用程序找不到依赖jar
【发布时间】:2011-10-18 12:55:41
【问题描述】:

尝试构建一个 java 应用程序我能够通过 Eclipse 成功运行该应用程序,但我无法运行由 ant 构建的 jar 文件。我怀疑我的 build.xml 是罪魁祸首,我正在努力纠正它。需要帮助!

mysql驱动.jar文件在项目的lib/目录下。

错误:

$ 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>

【问题讨论】:

  • 您是否将 mysql.jar 添加到您的类路径中?
  • 如果我像这样运行它$java -classpath ./lib/mysql-connector-java-5.0.8-bin.jar -jar dist/lib/MyProject.jar 它仍然会以同样的错误退出
  • 在您的异常处理代码中,放置 e.printstacktrace() 而不是 getMessage()。它不会解决任何问题,但您可能会获得有关原因的其他信息。
  • 完成。使用堆栈跟踪更新问题。
  • 我怀疑你的 jar 文件名有错别字。如果你在 java-6 上试试这个$java -classpath ./lib/*.jar -jar dist/lib/MyProject.jar

标签: java ant build.xml


【解决方案1】:

这样做:

<attribute name="Class-Path" value="./lib/mysql-connector-java-5.0.8-bin.jar"/>

表示mysql-connector-java-5.0.8-bin.jar 需要位于您的appName.jar 旁边的lib 文件夹中。不在里面。尝试将您的 lib 文件夹与 mysql jar 放在您的 appName.jar 旁边并运行应用程序应该确认这是问题所在。

【讨论】:

  • lib/mysql-connector-java-5.0.8-bin.jar 实际上确实存在于应用程序 jar 旁边,这要归功于在 build.xml 的 javac 部分之后定义的 ant“复制”操作
  • @Brad - 您的构建文件将 MySQL jar 复制到 build/lib,但您在 dist/lib 中运行 MyProject.jar。当你这样做时,给定你的清单类路径,java 将期望 MySQL.jar 在 dist/lib/lib 中 - 即相对于“运行”的 MyProject jar 在 lib 下。
  • 你是对的。我将 mysql.jar 复制到 dist/lib/lib 并且它有效!谢谢!
【解决方案2】:

试试

java -cp lib -jar dist/lib/MyProject.jar 

【讨论】:

  • 同样的结果...$ java -cp lib -jar dist/lib/MyProject.jar Loading Mysql JDBC driver... Error instantiating mysql jdbc driver. E=com.mysql.jdbc.Driver
猜你喜欢
  • 2010-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多