Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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:/dir2/dir3:/dir5/dir6/*:etc...

Means:

  • /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.

share|improve this answer
    
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 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_n Jun 11 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 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)

   JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/bin"

   export JAVA_HOME

   CLASSPATH=".:/usr/lib/jvm/java-7-openjdk-i386/lib:/home/laptop/Desktop/a2"

   export CLASSPATH

separate directory by ":"

share|improve this answer
    
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_n Jun 11 at 21:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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