As per my understanding you want to change the classpath which you have set by command
set classpath=d:java

can be done in two ways either you can set classpth directly as environment varible by
--> Right click on my computer select advanced options
--> there you will see option as environment variables open that option
--> now you will see multiple variables being set...search for classpath variable if it exist their edit this variable value by just putting semicolon and write ur full classpath ended by semicolon and save it.
FOR EG:-

variable name:- CLASSAPTH
variable value- .;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\ojdbc14.jar;

Second option is just set your class in command promt as u have set earlier

by opening command prompt

set classpath=C:\Oracle\product\10.1.0\Client_2\jdbc\lib\ojdbc14.jar;

both the method are easy but i would prefer you should go with first one as by doing that you didn't need to set your classpath again and again after you reboot your system or application.

Answer from Mukund Jindal on Stack Overflow
🌐
Princeton CS
introcs.cs.princeton.edu › java › 15inout › classpath.html
Setting the Classpath in Java
From DrJava, choose the menu option Edit -> Preferences -> Resource Locations -> Extra Classpath -> Add and select C:\introcs. Click the Apply button, then the OK button. To set the classpath for the Windows XP Command Prompt:
Discussions

java - Setting Classpath using command prompt in Windows 7 - Stack Overflow
set classpath= %classpath%;C:\java-programes\Servlet-Programing-new1\TotalUsersOnline\lib\servlet-api\*.jar;C:\java-programes\Servlet-Programing-new1\TotalUsersOnline\lib\servlet\*.jar; More on stackoverflow.com
🌐 stackoverflow.com
September 14, 2013
How to write a batch file to set classpath and execute java programs - Stack Overflow
Some of my java programs need so many jar files to execute. For executing this, I may have to add all of those jar files in classpath variable of environment variables or else have to set classpath manually at command prompt each and every time when I open a new cmd prompt. More on stackoverflow.com
🌐 stackoverflow.com
mysql - Set default classpath to use in `java` command in command prompt - Stack Overflow
What I'm trying to do is running a .java source by compiling and running it from command prompt (not using any IDE) using commands javac and java and the program connects with MySQL, so everytime I run the program from cmd, I need to specify path of the MySQL connector using -classpath switch ... 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
🌐
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 in command prompt to set classpath from command prompt in Windows and Linux OS.
🌐
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
Classpath can be specified using CLASSPATH environment variable which is case insensitive, -cp or -classpath command-line option or Class-Path attribute in manifest.mf file inside the JAR file in Java.
🌐
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 - setting Classpath set classpath=%classpath%;<java installed directory\lib\*.jar> eg: set classpath=%classpath%;C:\Program Files (x86)\Java\jdk1.7.0\lib\*.jar
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › paths.html
PATH and CLASSPATH (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications.
🌐
HowToDoInJava
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.
Find elsewhere
🌐
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 the CLASSPATH environment variable.
Top answer
1 of 3
1

There is no need for a batch file to specify the classpath for Java on command line as Jack wrote in his comment.

Take a look on the version 7 Java documentation pages:

  • Java
  • Setting the class path

There is -cp or better readable for humans -classpath which can be used on command line to define the classpath.

The paths to multiple classes can be specified by using a semi-colon as separator.

And double quotes around all paths must be used if one path contains a space character.

Example:

"%JAVA_HOME%\bin\java.exe" -classpath "C:\Java\My First Class;C:\Java\MySecondClass" MyJavaApp

This approach is mainly useful on using a shortcut (*.lnk) to a Java application which needs different classses than what is usually used and defined in system wide environment variable CLASSPATH.

But for developing and testing Java applications in a console window with a different list of classes than defined system wide, it is better to have a batch file for example with name JavaDevEnv.bat with following code

@echo off
title Java Development Environment
cd /D "Path to\Preferred Working Directory\For\Java\Development"
set "CLASSPATH=C:\Java\My First Class;C:\Java\MySecondClass"

and create a shortcut on Windows desktop or in Windows start menu with the command line

%SystemRoot%\System32\cmd.exe /K "Path to\Batch File\JavaDevEnv.bat"

defined in the properties of the shortcut file (*.lnk).

The working directory can be also defined with Start in in properties of the shortcut file instead of being set in batch file using change directory command.

And an appropriate Comment should be also written in properties of the shortcut file, for example same as used on command title which sets the title of the console window as hint for which purpose to use this shortcut.

A double click on this shortcut results in opening a new console window, executing the batch file which sets window title, working directory and environment variable CLASSPATH used by Java executed from within this console window and then remains open for user input.

2 of 3
0

Batch file: In order to run class file which uses classes of Some jar file and classes of same application.

As of Eclipse IDE Set all build path entries to the Class Path. As we are running it through batch file we need to set the target application class file too. As shown below.

REM MyApplication\mainClass.bat
REM MyApplication\target\classes - %~dp0target\classes;

mainClass.bat which contains set class path

@echo off
REM Maven local repository path
SET REPO=%USERPROFILE%\.m2\repository

REM Setting the class path
SET CLASSPATH=%REPO%\junit\junit\4.11\junit-4.11.jar;%REPO%\com\oracle\ojdbc5\11.1.0.7.0\ojdbc5-11.1.0.7.0.jar;%~dp0target\classes;%~dp0target\test-classes


java -Xmx256m com.github.yash777.MainClass

Java command line option to debug remotely

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000 <YourAppName>
🌐
Edureka
edureka.co › blog › set-java-classpath
How To Set Classpath In Java | Java Path And Classpath | Edureka
June 19, 2023 - 1. In vi editor open /etc/profile # vi /etc/profile . 2. Add export JAVA_HOME=/usr/jdk1.8.0. 3. Add export CLASSPATH=$CLASSPATH:/home/LOG4J_HOME/log4j-2.2.16.jar:.. 4. Add export PATH=$PATH:/usr/jdk1.8.0/bin. 5. Run # . /etc/profile to activate these settings.
🌐
Javainterviewpoint
javainterviewpoint.com › how-to-set-classpath-for-java-in-windows
javainterviewpoint.com
June 5, 2015 - Own it today for $1,911, or select Lease to Own or make an offer · Free transaction support
🌐
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.
🌐
Michigan State University
web.pa.msu.edu › reference › jdk-1.2.2-docs › tooldocs › solaris › classpath-linux.html
Setting the class path
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › tools › unix › classpath.html
2 Setting the Class Path
April 21, 2026 - Such settings can remain. You can change the class path by using the -classpath or -cp option of some Java commands when you call the JVM or other JDK tools or by using the CLASSPATH environment variable. See JDK Commands Class Path Options.
🌐
Drew University
users.drew.edu › bburd › JavaForDummies6 › classpath.pdf pdf
1 Chapter 2 Setting your Classpath
you open a command prompt window, and type this set command, then none of your · other open command prompt windows will use that modified classpath value.
🌐
Coderanch
coderanch.com › t › 370926 › java › Setting-CLasspath-command-line
Setting CLasspath from command line (Java in General forum at Coderanch)
Hi, I am trying to set a classpath through command line as follows-- java -classpath -Dcl.prop=C:\\server.properties but the command line argument needs a "classname".....however I am doing this to be able to read the value of cl.props in the jsp using a System.getProperty("cl.prop"); How do I do this?When I do try java -classpath -Dcl.prop=C:\\server.properties LL000SInput.jsp --OR------ java -classpath -Dcl.prop=C:\\server.properties LL000S I get the following error Exception in thread "main" java.lang.NoClassDefFoundError: LL000SInput/jsp ---OR------- Exception in thread "main" java.lang.NoClassDefFoundError: LL000S Any help will be appreciated Thanks,
Top answer
1 of 1
1

Executive Summary

java -cp doesn't automatically run a program in the specified classpath, it just specifies the classpath to use when running a Java program which you must then specify in that same command.

Full Explanation

java runs Java programs. It does not modify them or store persistent configuration about them.

The -cp flag does not set the classpath, it specifies a classpath to use when running a java program.

Running java with only a -cp flag (and its argument) is invalid syntax. You must tell java what java program you want to run with that classpath. This will run that program, once, with that classpath. It will not cause it to be run with that classpath in the future unless the classpath is specified again.

It's unclear from your question exactly what you're trying to accomplish, but I've detailed a few possibilities below.

(If you still need more information, please feel free to edit your question for clarification and comment on this answer, or if you find you have a separate question, you can ask it separately. The manual page for the java command may also help; please note that the name of a class or jar file is required. To get the manual page for the exact installed version of java on your machine, run man java.)

You might not have to manually set a classpath at all.

If myapp is a directory located in /home/ubuntu, which contains classes for your program, then you don't (generally) have to specify a classpath at all. In that case, just run the program (i.e., pass the name of the class that contains the program's main function to java.

For example, if the .class file for the class that contains your main method is /home/ubuntu/myapp/launch-myapp.class, just run:

java /home/ubuntu/myapp/launch-myapp

(Note that you don't specify the .class suffix, as it is implied.)

Most Java programs consist of multiple classes and often all the .class files are packed into a single .jar file. If that's the case, run java with the -jar flag and specify its full filename.

For example, if your program's classes (including the class containing main) is provided by the file /home/ubuntu/myapp/myapp.jar, just run:

java -jar /home/ubuntu/myapp/myapp.jar

Note that, to run a .jar file with java -jar, you do specify the .jar extension at the end of the filename; it is not taken to be implied.

Much less frequently, you might really want to manually specify a classpath.

To do that, you must specify the the classpath and also what Java program you want to run (i.e., the class that contains its main method).

For example, suppose you want to run /home/bob/myapp.class with the classpath /home/ubuntu/myapp. Then you would run:

java -cp /home/ubuntu/myapp /home/bob/myapp

Or if you're running /home/bob/myapp.jar with that classpath:

java -cp /home/ubuntu/myapp -jar /home/bob/myapp.jar

You might be trying to set the default classpath, to be used when running any Java program in the future (except when an alternate classpath is specified).

If that's what you're trying to do, you should set the CLASSPATH environment variable. See the official documentation on that as pointed out by karel.

In short, you simply make CLASSPATH a semicolon-delimited list of the directories you want searched automatically.

For more information on setting environment variables, see this question and this Ubuntu documentation wiki page.