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

Source: Internet
Author: User

Have you ever been confused by java manual compilation and running? Now, let's summarize:
Step 1: Find out the role of-classpath/-cp
In javac/java: add this parameter to tell the compiler to specify the parameter to be referenced. class file or. java location, accurate to the target file, or the Directory Name of the upper layer of the target folder (if there is a package mechanism.


Step 2: Find out the role of-d

In javac: If the java file contains the package mechanism and the-d + path parameter is added, the corresponding package is generated under the path.



Third point:
If you compile or run the program in the current directory, You can omit the-cp parameter.


Fourth: in java, the default modifier is default, which can be accessed and referenced only under the same package.


Fifth: Well, let's first try it out:

A. java

package p1;public class A{     public int i;     public void m()     {}}

B. java
package p2;import p1.*;public class B extends A{    public static void main(String[] args){    B b=new B();    System.out.println(b.i);     b.m();    }}           



A. java/B. java Directory:/home/luo/jdk/lab






Explanation:

Under the lab directory, I find A. java/B. java and compile and generate p1 translate under the jdk directory? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> keys + keys/2z8LE3NXStb2howo8YnI + Cgo8YnI + Cgo8YnI + keys" http://www.2cto.com/uploadfile/Collfiles/20140425/20140425090854398.png "alt =" \ ">






Next, we will explain:
When compiling A. java/B. java, pwd =/home/luo/jdk.
Generate p1.A under jdk/lab and p2. B under jdk,

When the class is finally loaded, the directory I am in IS/home/luo.

So when you load a class, you need to specify the p1.A p2. B path, and use" : "Connected, and then specify the name of the class to be run, that is, the class containing the main () function.









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.