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.

Answer from michael on askubuntu.com
Top answer
1 of 3
13

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.

2 of 3
4

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 ":"

🌐
Chilkat
chilkatsoft.com › java-classpath-Linux.asp
Java JAR Archives and classpath on Linux
javac -classpath ".:/chilkatJava/chilkat.jar" Test.java java -classpath ".:/chilkatJava/chilkat.jar" Test
Find elsewhere
🌐
Geeky Hacker
geekyhacker.com › home › linux › how to add classpath for jar files in java in ubuntu
How to add CLASSPATH for jar files in Java in Ubuntu
June 5, 2024 - Some files do not have the second ... line exists you need to change it slightly. Look at the example. CLASSPATH="/usr/local/Java/jdk1.6/lib:/home/user/MYLIB:."...
🌐
Baeldung
baeldung.com › home › java › jvm › ways to add jars to classpath in java
Ways to Add JARs to Classpath in Java | Baeldung
July 19, 2025 - First, if we’re launching our program from the command line, it can make sense to specify our JAR dependencies as part of the command: java -cp /path/to/jar/file.jar com.example.MyClass
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-jar-file-to-classpath-in-java
How to set classpath when class files are in .jar file using Java
July 21, 2023 - Following example demonstrates how to set class path when classes are stored in a .jar or .zip file. c:> java -classpath C:\java\myclasses.jar utility.testapp.main
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath-linux.html
Setting the class path
But when classes are stored in an archive file (a .zip or .jar file) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jar file, the command would look something like this: % java -classpath /java/MyClasses/myclasses.jar ...
🌐
Ubuntu Forums
ubuntuforums.org › showthread.php
Ubuntu Community Hub
April 28, 2011 - This is a space for discussions around Ubuntu, the community and the projects that bring them all together. We strive to keep this a welcoming and safe place for our community and therefore follow a simple Code of Conduct and honor an inclusive Diversity Policy · If you’re a newcomer to ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Include Jars In Java Classpath Example - Java Code Geeks
February 13, 2025 - Click on the Dependencies tab and then click the + (Add) button. Select JARs or directories..., browse to the JAR file location, and click OK. IntelliJ will automatically include the JAR in the project’s classpath.
🌐
Wikitechy
wikitechy.com › tutorials › java › how-to-add-jar-file-to-classpath-in-java
How to add JAR file to Classpath in Java - By Microsoft Awarded MVP - Learn in 30sec | wikitechy
Another benefit of using -classpath command line option is that, it enables each and every application to have its own set of JAR in classpath not at all like previous option that is available to all Java program running on same host. If you are running an executable JAR file, you might have noticed Class-Path attribute in manifest file inside META-INF folder.