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
๐ŸŒ
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 ...
๐ŸŒ
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 ...
๐ŸŒ
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.
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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.
Top answer
1 of 12
900

When programming in Java, you make other classes available to the class you are writing by putting something like this at the top of your source file:

import org.javaguy.coolframework.MyClass;

Or sometimes you 'bulk import' stuff by saying:

import org.javaguy.coolframework.*;

So later in your program when you say:

MyClass mine = new MyClass();

The Java Virtual Machine will know where to find your compiled class.

It would be impractical to have the VM look through every folder on your machine, so you have to provide the VM a list of places to look. This is done by putting folder and jar files on your classpath.

Before we talk about how the classpath is set, let's talk about .class files, packages, and .jar files.

First, let's suppose that MyClass is something you built as part of your project, and it is in a directory in your project called output. The .class file would be at output/org/javaguy/coolframework/MyClass.class (along with every other file in that package). In order to get to that file, your path would simply need to contain the folder 'output', not the whole package structure, since your import statement provides all that information to the VM.

Now let's suppose that you bundle CoolFramework up into a .jar file, and put that CoolFramework.jar into a lib directory in your project. You would now need to put lib/CoolFramework.jar into your classpath. The VM will look inside the jar file for the org/javaguy/coolframework part, and find your class.

So, classpaths contain:

  • JAR files, and
  • Paths to the top of package hierarchies.

How do you set your classpath?

The first way everyone seems to learn is with environment variables. On a unix machine, you can say something like:

export CLASSPATH=/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/

On a Windows machine you have to go to your environment settings and either add or modify the value that is already there.

The second way is to use the -cp parameter when starting Java, like this:

java -cp "/home/myaccount/myproject/lib/CoolFramework.jar:/home/myaccount/myproject/output/"  MyMainClass

A variant of this is the third way which is often done with a .sh or .bat file that calculates the classpath and passes it to Java via the -cp parameter.

There is a "gotcha" with all of the above. On most systems (Linux, Mac OS, UNIX, etc) the colon character (:) is the classpath separator. On Windows the separator is the semicolon (;)

So what's the best way to do it?

Setting stuff globally via environment variables is bad, generally for the same kinds of reasons that global variables are bad. You change the CLASSPATH environment variable so one program works, and you end up breaking another program.

The -cp is the way to go. I generally make sure my CLASSPATH environment variable is an empty string where I develop, whenever possible, so that I avoid global classpath issues (some tools aren't happy when the global classpath is empty though - I know of two common, mega-thousand dollar licensed J2EE and Java servers that have this kind of issue with their command-line tools).

2 of 12
124

Think of it as Java's answer to the PATH environment variable - OSes search for EXEs on the PATH, Java searches for classes and packages on the classpath.