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
🌐
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 - # On Windows system: CLASSPATH="PATH1;PATH2;PATH3" # On Linux system: CLASSPATH="PATH1:PATH2:PATH3" We’ve learned that the classpath separator can be different depending on the operating system. However, if we take a closer look at the Java man page on Linux, it says the classpath separator is the semicolon (;).
🌐
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 · Use the -Djava.class.path system property: javac -Djava.class.path=.:/chilkatJava/chilkat.jar Test.java java -Djava.class.path=.:/chilkatJava/chilkat.jar Test · IMPORTANT:On Windows systems, the semicolon character should be used for the path separator. However, on Linux systems, the colon character should be used.
🌐
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 ...
🌐
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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › unix › classpath.html
Setting the Class Path
April 21, 2026 - The class path is the path that the Java Runtime Environment (JRE) searches for classes and other resource files. ... 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.
Find elsewhere
🌐
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
To set Classpath for Java In Linux, you can simply export CLASSPATH="your classpath" from either your .bash_profile or .bashrc script which will run whenever your login into your Linux or Unix Machine.
🌐
Coderanch
coderanch.com › t › 612449 › os › Java-CLASSPATH-linux
Java CLASSPATH on linux (GNU/Linux forum at Coderanch)
For User level . Edit the /etc/bashrc ( or the /etc/profile ) and export the java path at the top of the file as follows Save and close .
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › paths.html
PATH and CLASSPATH (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
To set the path permanently, set ... environment variable, ENV. To set the path: ... The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes....
🌐
Reddit
reddit.com › r/learnjava › what exactly is the classpath? i don't fully get it and only know that it's akin to the path variable in linux.
r/learnjava on Reddit: What exactly is the classpath? I don't fully get it and only know that it's akin to the PATH variable in Linux.
September 6, 2023 -

I read this:

In Java, the term "classpath" refers to a parameter that specifies a set of directories or JAR (Java Archive) files where the Java Virtual Machine (JVM) and Java compiler should look for classes and resources when running or compiling Java applications

But where does this classpath comes into existence? If you for example have a completely vanilla project with some source code, and want to compile it manually with javac, does this notion of class path exist, for example?

How does it work in the context of build automation tools like Maven?And in an intelliJ project, for example, where can I find the class path and how does it work relatively to the project?

I've read a lot about what it does, but not what it exactly is and I would really appreciate some clarification!

Edit: I tried to look at the docs (oracle for example), but even there it's all kind of vague.

Top answer
1 of 3
3
Check out the answers in here : https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Vertica
docs.vertica.com › 11.1.x › en › connecting-to › client-libraries › client-drivers › modifying-java-classpath
Modifying the Java CLASSPATH | Vertica 11.1.x
July 14, 2023 - # export CLASSPATH=$CLASSPATH:/opt/vertica/java/lib/vertica-jdbc-x.x.x.jar · If you are using a shell other than Bash, consult its documentation to learn how to set environment variables. You need to either set the CLASSPATH environment variable for every login session, or insert the command to set the variable into one of your startup scripts (such as ~/.profile or /etc/profile). Provide the class paths to the .jar, .zip, or .class files. ... As with the Linux/UNIX settings, this setting only lasts for the current session.
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
setting java classpath [SOLVED] / Newbie Corner / Arch Linux Forums
May 20, 2009 - export PATH=/opt/java/bin:$PATH export JAVA_HOME="/opt/java/" export CLASSPATH=.:/opt/java/
🌐
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#1: Access Classpath First ... has been currently set in Linux. Step#2: Update Classpath To set the classpath, type the command export CLASSPATH=/root/java and enter....
🌐
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 - PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games" CLASSPATH="/usr/local/Java/jdk1.6/lib:."
🌐
SitePoint
sitepoint.com › get started
Setting classpath in Linux - Get Started - SitePoint Forums | Web Development & Design Community
December 2, 2005 - I’ve previously used Java only in Windows but am now trying to use it in Linux. I’m having trouble setting up the blessed classpath. Can anyone help please? I have installed Java SDK as root in /opt/j2sdk1.4.2_10/ as was the default. THis is how I have set the classpath so far - CLASSPATH=/opt/j2sdk1.4.2_10/bin:/opt/j2sdk1.4.2_10/lib/tools.jar I am trying to compile a test program javac HelloWorld.java as a non root user from home/java/ but always get - bash: javac: command not found I’ve ...
🌐
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.
🌐
Super User
superuser.com › questions › 560998 › classpath-reading-in-linux
java - Classpath reading in Linux - Super User
$ $CLASSPATH $ $ source .profile $ $CLASSPATH > bash: :/usr/java/connectors/mysql-connector-java-5.1.23-bin.jar: No such file or directory $ /usr/java/connectors/mysql-connector-java-5.1.23-bin....