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 OverflowAs 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.
If u want to do while running your java program ,you can use
java -classpath C:\java\MyClasses utility.myapp.Cool
for more details about class path see oracle documentation about classpath.
java - Setting Classpath using command prompt in Windows 7 - Stack Overflow
Java : How to use a -classpath in the command prompt.
mysql - Set default classpath to use in `java` command in command prompt - Stack Overflow
How to write a batch file to set classpath and execute java programs - Stack Overflow
Videos
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 .;
WIndows : Copy mysql-connector.jar to C:\Program Files\Java\jdk1.6.0\jre\lib\ext
and copy the same file to C:\Program Files\Java\jre1.6.0\lib\ext
go to My Computer -> Properties -> Advanced -> Environment Variables
Set these paths
JAVA_HOME
C:\Program Files\Java\jdk1.6.0
PATH
C:\Program Files\Java\jdk1.6.0\bin;
CLASSPATH
.;C:\Program Files\Java\jre1.6.0\lib\ext\mysql-connector.jar;.;
open a fresh command propmpt
type
java -version press Enter
WINDOWS
Go to My Computer -> Properties -> Advanced -> Environment Variables
then find CLASSPATH variable in System variables and click on edit to add your jar file there.
LINUX or MAC
In your shell use a variable CLASSPATH in your .bashrc or .profile to set a default class path.
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.
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>