HỎI Lỗi thực thi class java trong CMD

thufpt

New Member
2/8/21
11
2
3
Hà Nội
chào các bác ạ
em mới học Java mà giờ em chạy class bằng lệnh java trong CMD để in ra dòng Hello Java Training mà nó báo lỗi như hình ảnh, các bác xem giúp em với
thực thi javac thì không vấn đề gì, động đến java cái là nó lỗi

1627903241858.png
Java:
package training;

public class p01 {
    public static void main(String[] args) {
        System.out.println("Hello Java Training");
    }
}
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
3,007
1,335
113
Hi,
First look at this example and see how to post the JAVA codes
1627905379905.png

Back to your question:
It's about the settings of environment variables: PATH and CLASSPATH. An Operating System (OS) requires a PATH for the files it needs. JAVA Virtual Machine (JVM) is similar to OS JVM requires a CLASSPATH so that it could find the java classes.
Normally the javac creates a class with the same prefix name with the suffix .class in the same directory of the source. The PATH of OS always contains the CURRENT DIRECTORY and that is the reason why you could execute the compiler javac, but when you run the JVM via the command java the JVM seaches for the CLASSPATH that contains the file p01 and it didn't find it. Therefore you got the error message saying "could not find .... p01". Because the class is in the package training the class p01 is in the directory .\training\p01.class
How to solve the problem?
The fastest way:
Code:
java training.p01
The convenient way:
1) learn to master the setting of environment variables of the working operating system which is either LINUX or MacOS or WINDOWS
2) ON WINDOWS with a CMD:
Code:
set CLASSPATH=.\training;%CLASSPATH%
java p01
the ".\training" (dot) after
= and before ; tells JVM that this CURRENT DIRECTORY contains all requested .class files.
3) Study the settings of environment variables on WINDOWS if you want to put ".\training" as the current directory in the global variable CLASSPATH
 
Sửa lần cuối:

thufpt

New Member
2/8/21
11
2
3
Hà Nội
Hi,
First look at this example and see how to post the JAVA codes
View attachment 2806

Back to your question:
It's about the settings of environment variables: PATH and CLASSPATH. An Operating System (OS) requires a PATH for the files it needs. JAVA Virtual Machine (JVM) is similar to OS JVM requires a CLASSPATH so that it could find the java classes.
Normally the javac creates a class with the same prefix name with the suffix .class in the same directory of the source. The PATH of OS always contains the CURRENT DIRECTORY and that is the reason why you could execute the compiler javac, but when you run the JVM via the command java the JVM seaches for the CLASSPATH that contains the file p01 and it didn't find it. Therefore you got the error message saying "could not find .... p01". Because the class is in the package training the class p01 is in the directory .\training\p01.class
How to solve the problem?
The fastest way:
Code:
java training.p01
The convenient way:
1) learn to master the setting of environment variables of the working operating system which is either LINUX or MacOS or WINDOWS
2) ON WINDOWS with a CMD:
Code:
set CLASSPATH=.\training;%CLASSPATH%
java p01
the ".\training" (dot) after
= and before ; tells JVM that this CURRENT DIRECTORY contains all requested .class files.
3) Study the settings of environment variables on WINDOWS if you want to put ".\training" as the current directory in the global variable CLASSPATH
I'm sorry, I begin to learn Java so please can you show me detail more
how to set CLASSPATH=.\training;%CLASSPATH% and where
I use eclipse for code java, I already view some videos and do-follow but can't run, you can check the link below to clear more
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
3,007
1,335
113
Sorry, the setting of environment variable has NOTHING to do with JAVA or PYTHON or C++ etc. It's the OS and each OS has its own rules. I cann not help you with WINDOWS specific features.
If JAVA source is coded as a member of a package the package name (here training) is the directory which will be created either explicitly or implicitly. Example:
Code:
javac p01.java  <--- implicit without package (here training)
javac -d . p01.java <--- explicit with package name
// the invocation
implicit (without package name):
java p01
explicit (with package name)
java training/p01
OR:
java training.p01
Bild_2021-08-02_173432.png
 
Sửa lần cuối:
  • Like
Reactions: thufpt

thufpt

New Member
2/8/21
11
2
3
Hà Nội
Sorry, the setting of environment variable has NOTHING to do with JAVA or PYTHON or C++ etc. It's the OS and each OS has its own rules. I cann not help you with WINDOWS specific features.
If JAVA source is coded as a member of a package the package name (here training) is the directory which will be created either explicitly or implicitly. Example:
Code:
javac p01.java  <--- implicit without package (here training)
javac -d . p01.java <--- explicit with package name
// the invocation
implicit (without package name):
java p01
explicit (with package name)
java training/p01
OR:
java training.p01
View attachment 2807
Thank you for showing it to me but I still can't run it in cmd, when run in eclipse it work but in cmd only can run javac
1627920417553.png
1627920522340.png
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
3,007
1,335
113
you forget the parameter -d and the dot: javac -d . p01.java
 
  • Love
Reactions: thufpt

Joe

Thành viên VIP
21/1/13
3,007
1,335
113
the tag -d . means: create the classes in THIS directory (the dot = current directory) and because p01.java is a member of package training javac creates a folder training (or subdirectory) for the classes in the source folder (current directory). If you run "dir" in CMD you will see the training folder and in this training folder you will find the p01.class
 
  • Like
Reactions: thufpt

thufpt

New Member
2/8/21
11
2
3
Hà Nội
the tag -d . means: create the classes in THIS directory (the dot = current directory) and because p01.java is a member of package training javac creates a folder training (or subdirectory) for the classes in the source folder (current directory). If you run "dir" in CMD you will see the training folder and in this training folder you will find the p01.class
Yes, I understand, many thank, good night
 
  • Like
Reactions: Joe