Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Java with Ant - keep getting error JNI error has occurred

Tags:

java

eclipse

ant

I am working on improving my coding/ development skills and am new to using Ant. My goal is to be able to compile programs more complicated than "Hello World" from the command line. The program I am trying to compile and run uses libraries at that stored in an API, and I think I have included the correct path in my build.xml file, the code will compile and jar (when I use ant compile and ant jar commands), but when I run it I get an run time error.

Here is my Ant build file:

<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"/>

this generates the following errors:

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

I really appreciate any help!

like image 418
Minderbinder Avatar asked Jun 11 '15 18:06

Minderbinder


2 Answers

I had the same error. Turns out that Eclipse was using a symlink in the Eclipse setup for Ant Home (external ant installation).

I solved it by changing Eclipse's Ant Home to a definitive location.

In Eclipse Window->Preferences then Ant->Runtime then click on the Ant Home... button on the right and choose the correct Ant install directory.

like image 86
shonky linux user Avatar answered Sep 21 '22 22:09

shonky linux user


You compiled with the classpath set to the dropbox-core-sdk-1.7.7.jar jar, but you didn't run the code with that classpath. You need to do the same for the java task, otherwise the JVM won't find the third-party classes.

<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
    <classpath>
             <path location="${jar.dir}/dropbox-core-sdk-1.7.7.jar"/>
             <path location="${jar.dir}/jackson-core-2.2.4.jar"/>
    </classpath>
</java>
like image 24
M A Avatar answered Sep 22 '22 22:09

M A