Use Javac and Java commands in a CMD window to compile and execute a class with an inheritance relationship with the package name

Source: Internet
Author: User

I. BACKGROUND

When I recently wrote Java code with a package name and inherited relationships using Notepad and ran it, I found a lot of errors, and after trying to solve them, let's look at what problems we will meet and give solutions.

Second, the test process

1. Parent Class Code

1  PackageCom.hafiz.zhang;2 3  Public classFu4 {5     PrivateInteger i;6 7      Public voidSayHello (String name) {8System.out.println ("Hello" +name);9     }Ten}

2. Sub-class Code

1  PackageCom.hafiz.zhang;2 3  Public classZiextendsFu4 {5     Private StaticString name = "Hafiz.zhang";6      Public Static voidMain (string[] args) {7         NewZi (). SayHello (name); 8     }9}

1) The first time you run the Javac Zi.java command directly in the same directory as the base class and subclass, the following error occurs:

The workaround is that we need to run with the Javac *.java command, because there is an inheritance relationship, and the child class is compiled with the parent class first

2) Run the Java Zi command with the following error

What is the reason for this? Because we use the package name when we write the program, we need the full name when we run the class file, the command is modified to: Java Com.hafiz.zhang.Zi

We will find the following error

This is because we do not have a subfolder Com/hafiz/zhang and the Zi.class file does not exist under the subfolder, so the main class cannot be found.

The solution: Use javac-d. *.java ("-D." Represents the Create package path in the current directory) command to compile, so the Javac command automatically helps us create the folder specified by the package name and creates a Zi.class file under that folder.

At this point we also find that the subfolder Com/hafiz/zhang has been automatically generated and that there is a Zi.class file under that subfolder.

Then we run: Java com.hafiz.zhang.Zi command, program execution success!

This leads us to the way in which the CMD window uses Javac and Java commands to compile and execute a class with an inheritance relationship with the package name:

1. Use javac-d. *.java to compile

2. Use the Java Com.hafiz.Zi (class full name with package names) command to run!

Use Javac and Java commands in a CMD window to compile and execute a class with an inheritance relationship with the package name

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.