How to add CLASSPATH for jar files in Java in Ubuntu

Default featured post

When you do Java programming sometimes it happens that you need to add extra libraries to your application. In order to add those libraries you need to manipulate your CLASSPATH of Java programming language. Simply, CLASSPATH refers to the jar files libraries and this post shows “How to add classpath for jar files in Java in Ubuntu”.

In short, you need to add a new path to your CLASSPATH variable. For doing this so you should open /etc/environment with root permission and edit (add) new CLASSPATH to it.

The content of the file is look like this:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
CLASSPATH="/usr/local/Java/jdk1.6/lib:."

Some files do not have the second line and in that condition you need to add it manually which is not a big deal. For adding your CLASSPATH if the second line exists you need to change it slightly. Look at the example.

CLASSPATH="/usr/local/Java/jdk1.6/lib:/home/user/MYLIB:."

If your file does not contain the second line you need to define your CLASSPATH like below which is almost the same as above.

CLASSPATH="/home/user/MYLIB:."

Now, after applying the changes you need to save changes and reboot your system to get new libraries to work.