Let's find a directory, like C:\myjob.
Then we set up two directories, one called SRC, the other called bin
Then we write the program in the SRC directory.
We write 4 java files
[Java]View PlainCopyprint?
- A.java
- Package com.lightning;
- Public class a{
- {System.out.println ("com.lightning.a");}
- }
[Java]View PlainCopy print?
- B.java
- Package com.lightning;
- Public class b{
- {System.out.println ("com.lightning.b");}
- }
[Java]View PlainCopyprint?
- C.java
- Package com;
- Public class c{
- {System.out.println ("com. C ");}
- }
[Java]View PlainCopyprint?
- ////test.java
- span class= "keyword" >package net.test;
- import com.lightning.*;
- IMPORT COM.*;  
- public class test{
- public static void main (string[] args)
- {  
- new a (); new c ();
- system.out.println (
- }
- }  
That's what happens when you write.
Then we build the COM directory, com\lightning\ directory, net\test\ directory
We'll test.java into the net\test\.
Put the A.java,b.java into the com\lightning\.
Put the C.java into the com\.
Then we c:\myjobs in the middle of the list:
C:\myjob>javac-sourcepath Src-d bin Src\net\test\test.java
And then we'll see what's in the bin directory.
And then we execute, and the same under C:\myjobs\.
C:\MYJOB>JAVA-CP bin Net.test.Test
2, from practice to theory
Just now I used a very simple but very complete example to show you the Java package mechanism.
Why is it that the simple javac in my mind can be so complicated?
Actually, it was so complicated,
Not A. Java, a row javac a class in the current directory is so simple.
First I want to break something and then build something.
Javac not just for A. Java compilation of a class. Javac comes with a make mechanism, that is, if the
Javac parameters of any class used in the Java file, Javac first to find out whether the class file exists or not
, if it does not exist, it will look for the source code file to compile in SourcePath.
What is SourcePath? SourcePath is a parameter of Javac, if you do not specify, then SourcePath
Is classpath.
For example, after we installed the JDK, I said do not set the CLASSPATH environment variable, because most people once set the
Classpath, not superfluous put what Dt.jar put in. (I can blow you up and tell you a
The sad truth--jre will never seek dt.jar from your classpath. You are in vain! ) is
Put the "." Lost, make a basin of cold water to their own body splashed, a little head up.
Don't set it! Classpath is not as pervasive and powerful as you think it is, it's just a simplified alternative to the command line.
It's "." If you don't set it.
Back to Sourcepath,sourcepath refers to where your source tree is stored.
Why is the source code tree? Rather than a directory of flat-panel source code?
Please all the original Mind of C's compilation process completely smashed!
Java is completely different, Java has no header files, and each. Java is placed in the source tree.
So how is this tree organized?
Yes, it's the package statement.
For example, wrote the package com.lightning;
So this. Java must be placed under the root of the source code \ com\lighting\.
Many impetuous beginners are spoiled by the default packaging method. Self-centered, thinking that Java is a set of libraries, write their own
When the most import comes in on the line, the code is never packaged, direct Javac, direct Java, how convenient.
I don't know what I wrote this. Java is just one of the small elements of the Java platform. If not packaged,
I write a point, you write a point, and even more dare to give their own class named string and so on.
It's all in a flat-panel catalogue, which one does the JRE choose?
Once you use the package statement, you'll need to use the code tree structure, and of course you have to javac directly.
But the javac out of this class to be placed in a directory that conforms to the package structure in order to play a normal role, otherwise it is
A lump of waste.
And, if you're this. Java also uses the other written by the package statement of the. Java, then this method is useless
The operation.
According to Sun's imagination, it should be written well. Java is placed in a directory that conforms to the package structure, and the package statement guarantees
Correctly placed, an error occurs if the directory location and the package statement indicate the difference.
So according to our package, we must put those. java files in the appropriate directory
In order to make javac find them smoothly.
Some people say Javac can be Java Aaa.bbb.c...java like Java?
No
The javac. java file parameter must be a file system path file name form.
And then if you use the other. Java,javac will look for those in the directory structure based on the current sourcepath.
Java files.
Of course, since the bag, in the use of the time.
Either write the full name-the package name. Class Name
or use Import
Have to mention, import is like C + + using, it is not responsible for the file operation, it is just convenient for you to write code.
In addition, the * in the import statement represents the class name and does not represent a fragment of the package name.
You import com.*;
The compiler still cannot find any classes in com.lightning.
Vice versa.
It's like you're telling the compiler that I'm going to use a person who's surnamed GE.
Then the people of the last name, of course, the compiler doesn't think it's included.
So, if the program once written to a certain size.
You have to use ant to manage these.
Or use the IDE, or the JDK will really become Java developer killer.
But for starters, before you know why you have ant or something like that, you'll want to understand how to use
JDK's hardships.
This and previously developed on UNIX use the IDE with gcc command line to later use make
The same kind of experience is similar.
Finally, the-D parameter of JAVAC indicates where the compiled class files are placed, and if you don't specify them, they
mixed with. java. Of course it also conforms to the directory structure.
Longforus:
This 2 days is editplus as long as the package will appear Could not find or Load main class problem troubled for a long time to read this article, although still not fully understand, but also made clear some problems,
When adding the package class Javac and Java, add the full package name plus the class name form:
The problem of Java classes that do not run directly with the package name in EditPlus has been found online.
Using the online method to integrate the modified settings of the EditPlus download can be used:
Link: Http://pan.baidu.com/s/1bpE1BZL Password: J7QD
Java Package test