Search code examples
javanetbeansnetbeans-8apache-commonsfileutils

Netbeans not able to import FileUtils pacakge?


I am compiling my java software as Netbeans IDE 8.2. It uses the package FileUtils.

 import org.apache.commons.io.FileUtils;

I am using below function from FileUtils:

 FileUtils.copyURLToFile(inputUrl, dest);

It builds fine and runs fine using Netbeans IDE.

However, when I run the jar file separately using the command:

    java -jar myProgram.jar 

I get below error in the same line:

     java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils

I don't understand why FileUtils is not getting packaged in jar file. Please help.

This is my Apache Ant Build file:

 <target name="package-for-store" depends="jar">
<property name="store.jar.name" value="iNetsControlProgramAllOs"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<delete dir="${store.dir}"/>
<mkdir dir="${store.dir}"/>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
    <zipgroupfileset dir="dist" includes="*.jar"/>
    <zipgroupfileset dir="dist/lib" includes="*.jar"/>
    <manifest>
        <attribute name="Main-Class" value="${main.class}"/>
    </manifest>
</jar>
<zip destfile="${store.jar}">
    <zipfileset src="${store.dir}/temp_final.jar"
    excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>


Solution

  • Your jar does not include the thirs party libraries, you must include them.

    How do you build your myProgram.jar?

    If you perform this with Netbeans itself, you may follow "Adding Necessary Libraries", on this documentation.

    Otherwise, if not already the case, you may consider using Maven.

    Edit: OK, you are using Apache Ant So, you need to include third party libraries in the "build path", which is the aim of your existing lines:

    <zipgroupfileset dir="dist" includes="*.jar"/>
    <zipgroupfileset dir="dist/lib" includes="*.jar"/>
    

    Can you check where is your third party Jar file providing FileUtils?

    I guess it is NOT under dist, either dist/lib.

    In addition, you should change the syntax to something like:

    <zipgroupfileset dir="dist/lib" includes="**/*.jar"/>
    

    To ensure regarding ALL libraries in every sub-directories of dist/lib one.

    Let me know if you need further explanations.