To install Apache Commons use:
sudo apt-get install libcommons-math-java
Set the classpath to whichever directory contains the library you are using. Something like:
javac -classpath .:/library/directory/path/ MyProgram.java
Run man javac for more information.