Use System.getenv instead of System.getProperty. Note that you can also get the effective classpath for the current Java process with:

System.getProperty("java.class.path");

And that this value can, and in most cases will, be different from whatever your CLASSPATH environment variable is setup to be.

Answer from Perception on Stack Overflow
Discussions

java - Listing available classes in current classpath using a command line tool - Stack Overflow
I need to check if a specific class is available from current directory using a default JRE/JDK command line tool. I could build my own class to list it or check if a specific class is reacheble ... More on stackoverflow.com
🌐 stackoverflow.com
java - How to change the classpath in command prompt? - Stack Overflow
I have created a classpath in command prompt. Later I want to change that classpath to another location. Please help me. Thanks I have entered the command "set classpath= d:/java" Now i want to ch... More on stackoverflow.com
🌐 stackoverflow.com
Java : How to use a -classpath in the command prompt.
http://www.ibm.com/developerworks/library/j-classpath-windows/ Best article about the topic. Please include the error next time. More on reddit.com
🌐 r/learnprogramming
3
1
August 6, 2015
debugging - How to debug classpath command line option (-cp) for java executable? - Stack Overflow
I want to execute the java executable (Open JDK 11.0.2) on Windows from command line and I am not sure if I specify the class path option correctly, e.g. java -cp "./foo_\*/*" -version How can I ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
Now to check the value of Java classpath in windows type "echo %CLASSPATH" in your DOS command prompt and it will show you the value of the directory which is included in CLASSPATH.
🌐
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.
🌐
Medium
medium.datadriveninvestor.com › java-classpath-know-it-and-use-it-correctly-2cf6e4dc87ee
Java Classpath, know it, and use it correctly! | by Tom Henricksen | DataDrivenInvestor
June 29, 2022 - Although we discussed the PATH before here we haven’t talked about CLASSPATH. The CLASSPATH helps the Java Virtual Machine or JVM know where to look for resources. The PATH is geared toward the operating system.
🌐
Controlyourmindset
controlyourmindset.com › posts › f9db4adcbb1853f4c7aab6cda723492f
How can I check Java path in CMD? – Control Your Mindset
A pragmatic way: Class. forName(“com. ... ClassNotFoundException , then the jar is not on you current classpath. ... Use -classpath or -cp option to provide the classpath locations while starting the Java application or tool....
🌐
Muthu Saravanan
mshappylearning.wordpress.com › 2014 › 08 › 31 › java-path-and-classpath-settings-on-windows-command-prompt-system-variables
JAVA Path and Classpath Settings on Windows (command prompt / System Variables) | Muthu Saravanan
August 31, 2014 - Platform: WINDOWS Using Command Prompt 1) Install latest JDK 2) Open Command Prompt and enter the command : javac -version, it shows error like below. 3) setting Path set path=%path%; eg: set path=%path%;C:\Program Files (x86)\Java\jdk1.7.0\bin ...
Find elsewhere
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › classpath.html
Setting the Classpath in Java
export CLASSPATH=.:/Users/username/introcs/ If the file doesn't exist, create a new one. Close the Terminal and open a new one. If it does not work, in addition, try repeating the same instructions with the file /Users/username/.profile or /Users/username/.bash_profile. Here are a few suggestions that might help correct any installation woes you are experiencing. If you need assistance, don't hesitate to contact a staff member. How can I check the value of my CLASSPATH variable?
🌐
How to do in Java
howtodoinjava.com › home › java basics › java classpath
How to set CLASSPATH in Java - HowToDoInJava
February 23, 2023 - Learn to set java classpath in environment variable in windows 10 and pass as command-line argument. Learn to verify classpath from coammand prompt.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › paths.html
PATH and CLASSPATH (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care. The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value. To check whether CLASSPATH is set on Microsoft Windows NT/2000/XP, execute the following:
🌐
Oracle
docs.oracle.com › javase › › 7 › docs › technotes › tools › windows › classpath.html
Setting the class path
The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting ...
🌐
Edureka
edureka.co › blog › set-java-classpath
How To Set Classpath In Java | Java Path And Classpath | Edureka
June 19, 2023 - Run Command Prompt 1. Type in the following echo %JAVA_HOME% This will echo the directory path of JAVA_HOME or it will echo nothing if Variable is not set. echo %JAVA_HOME% ... To check version type java -version: 1.
🌐
Reddit
reddit.com › r/learnprogramming › java : how to use a -classpath in the command prompt.
r/learnprogramming on Reddit: Java : How to use a -classpath in the command prompt.
August 6, 2015 -

So I have two different .java files in two different directories. One contains the main method which calls the other one through its method name. . This is in the directory

E:\JavaWorld\TardyCoder\temp1

Here is my program which contains the main method with the fileName Lemons.java

public class Lemons{
public static void main(String args[]){

Sorra sorraObject = new Sorra();
sorraObject.testName();	
 }
 }

My another program is in the directory

E:\JavaWorld\TardyCoder\temp2

Here is my program Sorra.java which doesn't contain the main method.

 class Sorra{
public void testName(){
System.out.println("My name is Khan.");
 }
 }

I used -sourcepath to create two .class files like this:

   E:\JavaWorld\TardyCoder\temp1>javac -sourcepath E:\JavaWorld\TardyCoder\temp2 Lemons.java

Everything worked perfectly and i got two .class files in their respective directories.

Now I tried to use the -classpath to get the output like this:

E:\JavaWorld\TardyCoder\temp1>java -classpath .; E:\JavaWorld\TardyCoder\temp2 Lemons

But I'm getting an error :(

Error: Could not find or load main class E:\JavaWorld\TardyCoder\temp2

Please explain the right way to use -classpath in command prompt.

Edit: Got the answer. There should be no space after .;

Top answer
1 of 2
2
http://www.ibm.com/developerworks/library/j-classpath-windows/ Best article about the topic. Please include the error next time.
2 of 2
1
You need to put 2 line breaks to make paragraphs in your post. To format code blocks, indent with 4 spaces. Then your post becomes more readable and you'll get more help: So I have two different .java files in two different directories. One contains the main method which calls the other one through its method name. I used -sourcepath to create two .class files like this: E:\... javac -sourcepath E:\... FileName.java Now I tried to use the -classpath to get the output like this: E:\... java -classpath E:\.. FileName But I'm getting an error :( Please explain the way to use -classpath in command prompt. First things first, you should always mention errors when ever you ask for help. Anyway, I assume this isn't what you're actually executing, since it's not valid syntax. I assume that you must have removed paths and that the leading path is actually part of the prompt string (most people use $ to show the prompt string). If that's not the case, then this would be the underlying problem. It's noteworthy that you probably don't want to be using absolute paths here. You almost always want relative paths (eg, if you're in /foo/bar/baz and want to refer to a file with the path /foo/bar/tor/gat.png, you'd use the relative path ../tor/gat.png -- .. means go up a folder). Anyway, when you've compiled your Java files into class files, their location is the class path that you'd want. In 99% of projects, you don't need to set the class path because it defaults to including the current directory, which is what you'd want. Eg, if your project contains two files, A.java and B.java in the default package, you'd compile with javac A.java B.java # Or use `javac *.java` for simplicity which will result in the files A.class and B.class (and possibly others for inner classes, etc) appearing in the directory. You'd then merely have to point the JVM at the class with the entry point with java A Which requires no classpath since the class files are in the default package and the current working directory, and thus the JVM is able to find them. Now on the other hand, if your project had the files in foo/A.java and foo/B.java, where foo is the package they're in, you'd have to run the files from the base directory (not in the packages) with java foo.A # Note the dot instead of a slash And if foo.A needs foo.B, then the JVM will be searching for foo/B.class. So how does the JVM know where to look? That's what the classpath is for. In the cases above, we didn't have to set the class path because we made sure to work from the base directory from which all our packages are in. Usually you'd set the classpath for libraries, not for your own code. Having to set it for your own code may be indicative of you doing something wrong. If you get the general gist of what I'm saying here, perhaps you'll be able to figure out how to get things working (experiment a bit, if needed). If you're still stuck, elaborate on the exact paths you're using, the locations of files, the error message you're getting, and the packages that files are in.
🌐
IBM
ibm.com › docs › en › ssw_ibm_i_74 › rzaha › classpth.htm
IBM Documentation
The Java virtual machine uses the Java classpath to find classes during runtime. Java commands and tools also use the classpath to locate classes. The default system classpath, the CLASSPATH environment variable, and the classpath command parameter all determine what directories are searched ...
🌐
Chilkat
chilkatsoft.com › java-classpath-Windows.asp
Java JAR Archives and classpath on Windows
Copy the JAR to one of the directories listed in the CLASSPATH environment variable. To see the current value of the CLASSPATH environment variable, open a MS-DOS prompt and type: echo %CLASSPATH%Another way of viewing the classpath is to run this Java code: