我正致力於提高編碼/開發技能,並且是使用Ant的新手。我的目標是能夠從命令行編譯比「Hello World」更復雜的程序。我試圖編譯和運行的程序使用存儲在API中的庫,並且我認爲我已經在我的build.xml文件中包含了正確的路徑,代碼將進行編譯和jar(當我使用ant compile和ant jar命令時),但是當我運行它時,我得到一個運行時錯誤。使用Ant編譯Java - 不斷髮生錯誤發生JNI錯誤
這裏是我的Ant構建文件:
<project name="Main" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="lib"/>
<property name="main-class" value="myProject.Main"/>
<target name="clean">
<delete dir="${classes.dir}"/>
</target>
<target name="compile" depends="clean">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
<classpath>
<path location="${jar.dir}/dropbox-core-sdk-1.7.7.jar"/>
<path location="${jar.dir}/jackson-core-2.2.4.jar"/>
</classpath>
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
這會產生以下錯誤:
Buildfile: /Users/Phil/Documents/Java workspace/DropBoxProgram/build.xml
clean:
[delete] Deleting directory /Users/Phil/Documents/Java workspace/DropBoxProgram/build/classes
compile:
[mkdir] Created dir: /Users/Phil/Documents/Java workspace/DropBoxProgram/build/classes
[javac] Compiling 7 source files to /Users/Phil/Documents/Java workspace/DropBoxProgram/build/classes
jar:
[jar] Building jar: /Users/Phil/Documents/Java workspace/DropBoxProgram/lib/Main.jar
run:
[java] Error: A JNI error has occurred, please check your installation and try again
[java] Exception in thread "main" java.lang.NoClassDefFoundError: com/dropbox/core/DbxException
[java] at java.lang.Class.getDeclaredMethods0(Native Method)
[java] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
[java] at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
[java] at java.lang.Class.getMethod0(Class.java:3018)
[java] at java.lang.Class.getMethod(Class.java:1784)
[java] at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
[java] at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
[java] Caused by: java.lang.ClassNotFoundException: com.dropbox.core.DbxException
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 7 more
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 1 second
我真的很感激任何幫助!
哈哈我是一個短視的白癡。這確實解決了我的問題。我的問題實際上是我將我的Ant Home指向C:\ apache-tomcat而不是C:\ apache-ant。 – thonnor
謝謝!這讓我瘋狂!你拯救了我的一天(或者說:我的夜晚)! – mmo