Low Orbit Flux Logo 2 D

Java How To Install Apache Commons IO

You’ve probably seen code examples where people import libraries from Apache Commons IO. There exists a number of useful utilities that come with this library. These can make your life easier.

NOTE - We are doing this without Maven and without an IDE. If you use either of those things your experience might be different.

There are three main steps to installing Apache Commons IO:

If you don’t have it installed and setup on the CLASSPATH properly you might see an error like the following:

error: package org.apache.commons.io does not exist

You can download Apache Commons IO library as binary or source HERE.

We’re going to show you how to download and install this on Linux. The steps should be almost the same for Mac OSX or Windows. The paths might be a bit different.

First create a directory to keep your libraries in. Then download the file using wget and unpack it.

mkdir my_jars
cd  my_jars/
wget https://apache.claz.org//commons/io/binaries/commons-io-2.8.0-bin.tar.gz
tar xvfz commons-io-2.8.0-bin.tar.gz 

Next Setup the CLASSPATH. You can set this up and verify it with the following commands. This will be effective immediately.

export CLASSPATH=.:/home/user1/my_jars/commons-io-2.8.0/commons-io-2.8.0.jar

If you want the CLASSPATH to remain setup after you leave the current terminal you will want to add the command from above to your .bashrc file like this.

vi ~/.bashrc
export CLASSPATH=.:/home/user1/my_jars/commons-io-2.8.0/commons-io-2.8.0.jar

Next you are going to want to test this out. Create a Java source file that imports everythings from org.apache.commons.io and test one of the methods. Here is a code example showing how you might do this.

vi Test1.java
import org.apache.commons.io.*; import java.io.*; class Test1 { public static void main( String args[] ) { long x = FileUtils.sizeOf(new File("Test1.java")); System.out.println(x); } }

Once you’ve setup your source file try building it with javac. If everything was done correctly it should compile with no errors.

javac Test1.java 

Last, try running the test program.

java Test1

Thats it! You’re done!


Here is our video showing you what we’ve covered here: