① preparatory work
First write Helloworld.java to G:\Javaspace path with Notepad
Public class helloworld{publicstaticvoid main (String []args) {System.out.println ( "Hello World");}}
because I want to compile the Java file on the G-Disk, in the cmd window input
G:
Go to the G-Tray directory (online Although CMD is not case-sensitive, but this letter is to be capitalized!) )
And then enter
CD Javaspace
The CD is the cmd directory command (change directory), at which time the CMD directory is transferred to the directory where we Helloworld.java.
② compiling
The next step is to tell the cmd helloworld.java and Java compiler (i.e., Javac) where
There are two ways to set the Java compiler path
One is the set path command with CMD (this is a once and for all method, only set once)
SET Path=c:\program files\java\jdk1.8.0_20\bin;%path%
(The path after the drive letter is case-free)
The second way to set the compiler path is to specify it at compile time, because we have already moved to the G:\Javespace directory, so enter directly
"C:\programs Files\java\jdk1.8.0_20\bin\javac" Helloworld.java
(Helloworld.java file names are case-sensitive)
If there is no error prompt after pressing ENTER, the Helloworld.class file should have been generated under G:javaspace, indicating that the compilation has succeeded
③ Run
If CMD is not exited at this time, the cmd directory is still G:\Javaspace and the input
Java HelloWorld
Java excites the JVM and runs Helloworld.class, and the HelloWorld here does not have to be suffixed. class
If you are prompted to "cannot find or load the main class", then the CLASSPATH path set when installing the JDK is not G:\Javaspace and can be reset in "My Computer-Properties ...--Environment variables"
You can also temporarily modify the classpath path in cmd and enter
JAVA-CP G:\javaspace HelloWorld
This line command means to start the JVM and set Classpath, and then run Helloworld.class
Beginner cmd Compile run Java file, partial path to case-sensitive