Compilation and debugging of classes with packages (create and reference) in Java

Source: Internet
Author: User

compilation and debugging of classes with packages (create and reference) in Java

Java source program compiled everyone knows, that is, cmd in the directory where the source files Javac **.java can, when the program has a package declaration can be simple direct javac **.java? The answer is no, and here's a simple example to prove what a direct javac **.java would be like.

As follows: F:\JAVAWEB2 class \20160531 Directory has A.java file, note that there is a package declaration in the source file

Package Mypack; Public classA {String name; intAge ;  Public voidsetName (String _name) { This. Name =_name; }     Public voidSetage (int_age) {         This. Age =_age; }     PublicString GetName () {return  This. Name; }     Public intGetage () {return  This. Age; }     Public Static voidMain (string[] args) {A a=NewA (); //a.setname ("Zs");A.name="ZS"; A.setage ( -); System. out. println (A.getname () +a.getage ()); }}

The Javac A.java results are now compiled directly for A.java:

We can clearly see the current directory in the presence of A.class bytecode files, so that can not directly run it? Give it a try!

This error has occurred because We have a package declaration in A.java, when the A.java source file is executed, the Java virtual machine will first look for the bytecode file in the A.java current directory, although this is found, but because there is a package declaration in A.java, the Java Virtual machine immediately go to the package directory to find there is no A.class bytecode file, this found Can be successfully executed , do not believe we do the experiment! (Here I'll teach you how to compile a a.java that has a package declaration, that is, to pack and compile javac-d.) A.java)

This is the package compiled successfully, then we can see the Mypack directory appears in the A.class file, below try to execute it

At this time we found that the implementation or not pass?? What is it then? This is a problem for many novices, and it is important to remember that theparameters of the Java command are the full class name of the class, not the file name.

The full class name of the source program should be mypack. A, so it should be written like this: Java mypack. A

This will be executed successfully!

Don't worry!! There are more exciting! As the use of the package how may not explain the package import to create the compilation it!

This time we introduce another test class Test.java, the code is as follows:

Import Mypack. A;public class Test{public static void Main (string[] args) {A A = new A (); A.setname ("Zs");//a.name= "Zs"; a.setage (18); System.out.println (A.getname () +a.getage ());}}

  

The compilation is performed as follows:

The result will certainly be able to compile execution, the execution process is this:Test.java compiled after the generated bytecode file in the current directory (at compile time he would look for Mypack in the A.class file, if none, compile does not pass), when executed, Since there is no package declaration in Test.java, the Java Virtual machine is first found in the current directory Test.class will be executed, to the program to refer to the Class A place, and then the Java Virtual machine in the current directory to see if there is a A.class bytecode file, At this point even if the discovery will also be based on the source program in the package import into the package to find A.class, found to perform successfully (in fact, in the compilation phase has been looking for!) )

The following further: if we give the test class Test.java plus package declaration packages Mypack1;

At this time we test.java to the package compiled, here I will explain to you two knowledge points:1. When the package compiles, it automatically creates the packages directory, does not need to create a new package name folder; 2. javac-d. * When there are multiple Java files in the current directory that need to be compiled or packaged. Java directives can be compiled or packaged for all Java files in the current directory according to whether a package declaration is in the program

Then how do we execute the Test.java file? Java Test.java?? Obviously this is not possible, remember what I said earlier: TheJava Command's parameters are "class's full class name", not "file name".

So we need to do this:

This will

These are the general conditions, that is, the classpath is in the current directory, when the classpath is not in the current directory is still able to execute it? And what if it should be implemented?

If I put Test.java in a directory outside, then we need to set the Classpath parameter ourselves. For example: F:\JAVAWEB2 class >java-cp f:/javaweb2 class/20160531 Mypack1.java; or in any directory: JAVA-CP f:/javaweb2 class/20160531 Mypack1.java

This will be a success! The concrete implementation process everybody own analysis experience!

Sum up: 1. The parameters of the Java command are the full class name of the class, not the file name.

2. When you package the compilation, the package directory is created automatically, and you do not need to create a new package name folder yourself.

3. javac-d when there are multiple Java files in the current directory that need to be compiled or packaged. The *.java directive can be compiled or packaged for all Java files in the current directory according to whether the program has a package declaration.

4. When the classpath is not in the current directory, you need to use the JAVA-CP ... such as: JAVA-CP F:/JAVAWEB2 class/20160531 Mypack1.java.

5. Be aware of the process by which the Java Virtual machine imports execution bytecode files based on the package declaration package.

Compilation and debugging of classes with packages (create and reference) in Java

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.