2011-08-02 66 views
1

試圖構建一個java應用程序我能夠通過Eclipse成功運行應用程序,但我無法運行由ant構建的jar文件。我懷疑我的build.xml是我的責任,我試圖讓它正確。需要幫忙!ant應用程序的內置版本無法找到依賴的jar

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> 
+3

您是否將mysql.jar添加到類路徑中? –

+0

如果我像這樣運行它'$ java -classpath ./lib/mysql-connector-java-5.0.8-bin.jar -jar dist/lib/MyProject.jar',它仍然會以異常 –

+0

退出處理代碼,把e.printstacktrace()而不是getMessage()。它不會解決任何問題,但您可能會有其他原因的信息。 – Cygnusx1

回答

2

這樣做:

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

說,mysql-connector-java-5.0.8-bin.jar需求是在一個lib文件夾旁邊的appName.jar。不在裏面。嘗試將你的lib文件夾放在你的appName.jar旁邊的mysql jar中,然後運行應用程序應該確認這是問題所在。

+0

''lib/mysql-connector-java-5.0.8-bin.jar'實際上確實存在於app jar旁邊,這要歸功於在javac之後定義的ant「copy」操作build.xml的一部分 –

+1

@Brad - 您的構建文件將MySQL jar複製到build/lib,但是您在dist/lib中運行MyProject.jar。當你這樣做時,給定你的清單類路徑,java會期望MySQL.jar位於dist/lib/lib中 - 即在相對於'running'MyProject jar的lib下。 –

+0

你說得對。我將mysql.jar複製到dist/lib/lib並且它工作正常!謝謝! –

0

嘗試

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

同樣的結果...'$ java -cp lib -jar dist/lib/MyProject.jar 加載Mysql JDBC驅動程序... 實例化mysql jdbc驅動程序時出錯。 E = com.mysql.jdbc.Driver' –

相關問題