Assume that two Java source files are stored in D: \ java: MyJava. java and Fighting. java.
MyJava. java is as follows:
Public class MyJava {
Public static void main (String [] arr)
{
System. out. println ("MyJava ");
}
}
Fighting. java is as follows:
Package mxw. ly;
Public class Fighting {
Public static void main (String [] arr)
{
System. out. println ("learn to use java and javac commands ");
}
}
Javac command
Compile as follows:
1. D :\
Cd java
Javac *. java
2. javac-sourcepath D: \ java D: \ Java \ *. java
3. javac D: \ Java \ MyJava. java (the suffix name is case-sensitive. If javac D: \ Java \ MyJava. Java is input, compilation fails)
You can also specify the output directory of the compiled file: javac-sourcepath D: \ java-d D: \ output D: \ Java \ *. java
(After the-d parameter is used to specify the compiled output directory, the package in the. java source file is automatically compiled into the corresponding folder)
Java commands
1. C: \ Documents ents and Settings \ Administrator> java-classpath D: \ java; MyJava
2. D: \ Java> java MyJava
The class Name of the Java instruction is case-sensitive and does not require the suffix of. class.
There are two Java source files in D: \ java:
A. java:
Public class {
Public static void Eat ()
{
System. out. println ("A. Eat ");
}
}
B. java:
Public class B extends {
Public static void main (String [] arr)
{
B B = new B ();
B. Fun ();
}
Public void Fun ()
{
Super. Eat ();
}
}
First compile:
C: \ Documents ents and Settings \ Administrator> javac D: \ Java \ B. java
The A. class and B. class files are generated in D: \ Java.
Then run: java-classpath D: \ java; B (if D: \ java is specified in the system environment variable classpath, you can directly enter java B)
Console Output A. Eat
Put A. java in the D: \ Java \ import folder
First compile: C: \ Documents ents and Settings \ Administrator> javac D: \ Java \ B. java
An error is reported because A. java is no longer in the same directory as B. java.
Compile with another command: C: \ Documents ents and Settings \ Administrator> javac-classpath D: \ java \ Import; D: \ Java \ B. java
After compilation is successful, B. class is generated at D: \ Java and A. class is generated at D: \ Java \ import.
Then run: C: \ Documents ents and Settings \ Administrator> java-classpath D: \ java; B
An error is reported because A. Class is not in the same directory as B. class.
Run the following command: C: \ Documents ents and Settings \ Administrator> java-classpath D: \ java; D: \ java \ import; B
Execution successful, output A. Eat
If the system variable classpath value is D: \ Java; D: \ java \ import; you can directly enter the following command on the console: java B
Execution successful, output A. Eat