I don't like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:

  • If you modify it in one script, suddenly some java programs will stop working.
  • If you put there the libraries for all the things which you run, and it gets cluttered.
  • You get conflicts if two different applications use different versions of the same library.
  • There is no performance gain as libraries in the CLASSPATH are not shared - just their name is shared.
  • If you put the dot (.) or any other relative path in the CLASSPATH that means a different thing in each place - that will cause confusion, for sure.

Therefore the preferred way is to set the classpath per each run of the jvm, for example:

Copyjava -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar"    "folder.subfolder../dit1/some.xml

If it gets long the standard procedure is to wrap it in a bash or batch script to save typing.

Answer from flybywire on Stack Overflow
๐ŸŒ
Michigan State University
web.pa.msu.edu โ€บ reference โ€บ jdk-1.2.2-docs โ€บ tooldocs โ€บ solaris โ€บ classpath-linux.html
Setting the class path
The class path can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ technotes โ€บ tools โ€บ unix โ€บ classpath.html
2 Setting the Class Path
April 21, 2026 - The class search path (class path) can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other ...
๐ŸŒ
HowTech
howtech.tv โ€บ home โ€บ how to set and check classpath in linux
How to Set and Check Classpath in Linux - HowTech
September 6, 2013 - Step#2: Update Classpath To set the classpath, type the command export CLASSPATH=/root/java and enter. By using the export command over here, we can set the class path for Java in a Linux or UNIX environment directly from the command prompt.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2011 โ€บ 01 โ€บ how-classpath-work-in-java.html
How to Set Classpath for Java on Windows and Linux? Steps and Example
In fact, CLASSPATH is an environment variable that is used by Java Virtual Machine to locate user-defined classes. As I said In this tutorial we will see How to set up the classpath for java in windows and Linux, java -classpath example in different scenarios, and the use of java -classpath or java -cp.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 493384 โ€บ os โ€บ Setting-classpath-linux
Setting classpath in linux. (GNU/Linux forum at Coderanch)
In addition, I tried creating this file in my home directory and running it as a shell script. I tried: This does nothing. How can I do this? Thank You for Responding Andrew Stallard ... I don't set CLASSPATH in my $HOME/.bash_profile. Which is a file you'll only have if bash is your command shell.
Find elsewhere
๐ŸŒ
Chilkat
chilkatsoft.com โ€บ java-classpath-Linux.asp
Java JAR Archives and classpath on Linux
setenv CLASSPATH path1:path2:... In sh, the CLASSPATH environment variable can be modified with these commands:
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 419723 โ€บ os โ€บ set-classpath
how to set classpath (GNU/Linux forum at Coderanch)
Depending on which shell you're using and what implementation of Linux or Unix you're working with, there are multiple places you can setup a "master" CLASSPATH. I normally use bash and set it up per-user, so I define my CLASSPATH in ~/.bash_profile. Since CLASSPATH is an environment variable, you can set it inside a given shell session to a new value at any time or unset it entirely.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java examples โ€บ java โ€“ set classpath from command line
Java - Set Classpath from Command Line
January 25, 2022 - Learn to use the -classpath or -cp option to set the Java classpath from the command prompt in Windows and Linux OS.
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 ":"

๐ŸŒ
LinuxQuestions.org
linuxquestions.org โ€บ questions โ€บ linux-newbie-8 โ€บ set-java-classpath-on-linux-914077
set java classpath on linux
November 17, 2011 - Hello! I'm a new user in Linux. I'm using opensuse 11.2, but i don't know how to set path to java. The problem in console: Code: Exception in thread &q
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ SSLTBW_2.2.0 โ€บ com.ibm.zos.v2r2.ioaz100 โ€บ classlin.htm
To define the CLASSPATH environment variable for Linux
January 20, 2022 - Get assistance for the IBM products, services and software you own ยท Provides fixes and updates for your system's software, hardware, and operating system
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ java classpath syntax in linux vs. windows
Java Classpath Syntax in Linux vs. Windows | Baeldung
January 8, 2024 - We can define the elements in the classpath either through the -cp option of the java/javac commands or through the CLASSPATH environment variable. No matter which approach we take to set the classpath, we need to follow the classpath syntax.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 466452 โ€บ java โ€บ set-classpath-java-Linux
How to set classpath for java in Linux (Beginning Java forum at Coderanch)
October 14, 2009 - Different Linux distro's have different preferred ways for installing Java. Google for "<your distro name> Java". On Ubuntu, the best way to install Sun JDK 6, which will automatically set up your path correctly, is entering the command: sudo apt-get install sun-java6-jdk ... try like this.... export CLASSPATH=/local/home/soft/bea81sp6/jdk142_11 hope would help Thanks, Samir