Java and javac commands

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.