I know that export CLASSPATH=/usr/local/java/tools.jar:$CLASSPATH will add tools.jar to the CLASSPATH, but i want to set folder to the CLASSPATH

like this

export CLASSPATH=/usr/local/java/lib/:$CLASSPATH

but its not working.


First, in general, setting the env var CLASSPATH usually causes more problems than it solves -- since not all app's want/need the same classpath, & often break when undesired or even unneeded jars are included in the classpath. A java app should only include the minimum number of jars it requires, no more, no less.

When you have specific, individual apps that do require that the classpath be set, then usually the command-line option is preferred: java -cp path1:path2:.... Desktop icons can have their command altered to include these options, or shell scripts can be modified to include these options.

That being said (and since there are always exceptions to the rule), then depending on the version of java (this requres java 6 or later), you can specify that a whole directory of jars be added to the classpath by adding a "*" at the end of a directory; e.g, the following:



  • /dir1/foo.jar - (the single jar) will be added to the classpath;
  • /dir2/dir3 - all un-jar'd classes in this directory will be added to the classpath (must be in proper package structure; e.g, com.my.Foo.class must be in /dir2/dir3/com/my/Foo.class)
  • /dir5/dir6/* - all jars in this directory (i.e., /dir5/dir6/*.jar) will be added to the classpath. Note that this "*" isn't a wildcard (you can't use f*.jar or even *.jar); it's a special character indicating "add all jars"

In general, if you have to add a whole directory of jars to the application's classpath, the app was not bundled correctly. Rather, the app should have a manifest containing the list of jars it depends on. Or at the very least, only one jar should be added to your classpath, and that jar can have in its manifest the whole list of jars in some subdirectory.

  • It takes me more than 5 hours to figure it out this in ubuntu 12.04. After you compile a file and run the class, two things are important. 1. Whether you use package in your class. And you need to use "java -cp . className" in the package parent folder. 2. You must use ".:" like "java -cp .:/usr/lib/jvm/java-1.6.0-openjdk-i386/jre/lib/*:/dir2/jarlib/*". – zhihong Jun 11 '14 at 15:50
  • @zhihong you should consider using an IDE like netbeans, letting it defer to the build tool in use, which actually configures your classpath. In Netbeans, set up a new project using maven or gradle (or ant -- but this gets too complicated when dealing with dependencies (see also: maven ant tasks, which I prefer over ivy)). Gradle is currently the 'hot' thing, fwiw; you should give it a try. Also, (1) you should always use package names; and (2) there's never a need to include classes in the jvm directory in the classpath; they are already in the "boot" classpath. – michael Jun 11 '14 at 21:37
  • thanks for your suggestion. Actually,I use eclipse, and the code runs correctly there, but when I run it in the terminal, then got the "ClassNotFoundException". I tried many ways to add the external lib/jar, but all failed. Finally, it works by include the jvm directory and external lib/jar in the command line WITH . But, I tried today, you are correct, the jvm directory is not needed. – zhihong Jun 12 '14 at 7:33

if you want to set classpath permanently then 1) find out where java is installed.. you may use "whereis java" openjdk-7/6 is in /usr/lib/jvm/.....

2) we need to set up CLASSPATH in /etc/environment

  sudo gedit /etc/environment

3) add the following likes .. ( DONT LEAVE ANY SPACES WHILE TYPING)(customize according to your java version and installation) (this home path is for open jdk 7)


   export JAVA_HOME


   export CLASSPATH

separate directory by ":"

  • Why is everyone adding "{JAVA_HOME}/lib" to their classpath? It's not necessary, or even a good idea. For that matter, setting the env CLASSPATH is also a bad idea, unless it's in a shell script, just for that script, in which case, the command-line option "-cp" to java would still be preferable. – michael Jun 11 '14 at 21:39

open terminal and type

sudo nano ~/.bashrc

add all the exports you need like ...

export JAVA_HOME=/usr/local/java/jdk1.6.0_06(your path)

export ANT_HOME=/opt/ant/apache-ant-1.9.4

finally restart terminal for changes to take effect

Not the answer you're looking for? Browse other questions tagged or ask your own question.