ClassPath and Package in Java

xiaoxiao2021-03-06  22

ClassPath and Package in Java

Popularity 24, a total of 0 comments, commenting Supermmm, April 9, 2001

Foreword: Because these two questions are more asked, they have a relatively zero, it is difficult to organize, so I will write a piece directly, please forgive me. First, class path (Class Path) When you are full of hope Good Java, then wrote a Hello World, then compiled, run, waiting for the two beautiful words appeared in front of the eyes, but unfortunately, only see the can't Find Class HelloWorld or Exception in Thread "main" java.lang.nosuchmethoderror: maain. Why? Compile Class clearly. Let's take a look at the running process of the Java program. We already know that Java is explained through the Java virtual machine, that is Through the java command, Javac compiles generated .Class file is the code to be executed by virtual machine, called bytecode, the virtual machine loaded these bytecodes through ClassLoader, which is a common class.

There is a problem here, where is the ClassLoader who knows the Java itself class library and the user's own class? Or has a default (current path). Or there is a user specified variable to indicate that this variable is class The pathpath, or the parameter to the virtual machine when running. This is to indicate three ways of ClassPath. The process of compiling the process and the process of running is similar, just finding it, the other is to find it.

In fact, the Java virtual machine is initialized by Java Luncher, which is also the program of Java (or Java.exe). The virtual machine searches and loads all the needs of the needs in order: 1, Boots: Composition of Java Platform Include classes in RT.jar and I18n.jar. 2, extended classes: Classes using Java extended mechanisms are located in the extended directory ($ java_home / jre / lib / ext). JAR file package. 3, users Category: Developer defined classes or third-party products that do not use Java extended mechanisms. You must use the -classpath option in the command line or use the ClassPath environment variable to determine the location of these classes. We are above the user's own class That is to specifically refer to these classes.

In this way, in general, users only need to specify the location of the user class, the boot class, and the extension class are "automatic".

So what should I do? The user path is some list of directories that contain classes, .jar, .zip files, as for the class specific to find, because the problem involved in Package, the following will be said, temporarily think as long as It contains this class to find this class. If there is different separators depending on the platform, UNIX systems are basically ":": ", Windows is mostly"; ";" Its possible source is: * "., That is, the current directory, this is the default value. * ClassPath environment variable, once the settings are overwritten. * Command line parameters - cp or -classpath, once specified, the upper two is overwritten. * Specified by the -jar parameter .jar file package, cover all other values, all classes come from this specified file. Due to generating executable .jar files, there are other knowledge, such as Package, and specific configuration files. The final mention of this article will be mentioned. You can first look at some examples of JDK. We will give an example of HelloWorld to explain. Let's do the following assumptions: * The current directory is / helloworld (or C: / HelloWorld, after using the previous one * JDK version is 1.2.2 (under Linux) * Path environment variable settings are correct. (This can be used in any directory) * The file is HelloWorld.java, the content is: public class helloworld {public static void main (String [] args) {system.out.println ("Hello World! / N"); system.exit (0);}} First of all this file must be written, if you are familiar with C, it is likely to be written : public static void main (int Argc, string [] argv) {....} is not right, do not believe you can try it. Because there is no Java specification in the hand, make the following conjecture: Java Application Program, must Public static void main (String []) begins, other different are not the same. So far, we set up only pat on. 1, the current path is to point your .class file in the current directory, [Hello World] $ javac helloworld.java // This step will not have a big problem, [HelloWorld] $ java helloworld // This step may have a problem. If you have a problem like starting, you first determine that it is not because it's wrong. Error. If there is no error command, then do it: [HelloWorld] $ Echo $ ClassPath or C: / HelloWorld> Echo% classpath% Take a look at whether the classpath environment variable is set, if set, then use the following command: [HelloWorld] $ ClassPath = or C: / HelloWorld> Set classpath = to make it empty, then re-run. This time the user class path is ".", So there should be no problem. There is a method to Add "." Add to ClassPath. [/] $ Classpath = $ ClassPath :. or C: / HelloWorld> set classpath =% classpath%; also can succeed. Good luck. 2, when your program requires a third party Class library support, and more common, you can use this method. For example, a common database driver, write servlet package required by servlet. The setting method is to add ClassPath in the environment variable. Then you can compile it directly. Still take HelloWorld as an example, such as you want to run it in the root directory, then you can perform $ JAVA HelloWorld or C: /> directly in the root directory.

Java HelloWorld will definitely be wrong. If your classpath has no change. I think everyone should know why it is wrong. How do you change it? I have said that the user path is some directory that contains the class you need. Archive package, .zip package. There is no generating package now, so I have to add the directory where helloWorld.class is located, according to the previous practice, then run once, see, huh, success, change the path, and succeed! Not only to run the class directly, when you want to implement some classes in IMPORT, you can do it. Don't know that you don't think, with your system's continuous expansion, if you add it to this environment variable, then This variable will get more bloated, although the environment variable space can be opened, always feel some uncomfortable. Take a look at the following method. 3. Indicate the classpath in the command line parameters. Or the same target as above, in any directory Execute HelloWorld, how to implement this method? [/] $ Java -cp / helloworld HelloWorld or C: /> java -cp c: / helloworld HelloWorld is OK. This is the easiest application of this method. When you use another package, you can also use another method. For example: $ javac -classpath apath / apackage.jar :. myjava.java $ java -cp apath / apackage.jar :. myjava or C: /> Javac -classpath apath / apackage.jar ;.. Myjava.java C: /> java -cp apath / apackage.jar ;. Myjava This method also has an inconvenient place is that the path where the third party is located longer or needs When two or more packages, every time compilation is long, it is very inconvenient, it can write the script to solve this time. For example: Compile (file, permissions change to executive, current directory) $ Cat Compile - -------------------------- #! / bash javac -classpath apath / apackage.jar: anotherpath / annotherpage.jar :. myjavva. Java ------------------------- - Run (file, permissions to executable, current directory) $ CAT RUN --------------------------- #! / bin / Bash java-cp apath / apackage.jar: anotherpath / annotherpage.jar :. myjava ------------------------- or: compile.bat C: / helloworld> Type compile.bat ----------------------- javac -classpath apath / apackage.jar: anotherpath / anotherpackage.jar :. Myjavva.java ------------------------- Run.bat C: / HelloWorld>

Type run.bat ---------------------- Java -cp apath / apackage.jar: anotherpath / annotherpackage.jar :. myjava ----- ------------------- you can. Try it. Previous mentioned the expansion class, what is the extension class? Java expansion class is the application developer to expand The core platform features the Java class package (or native code). The virtual machine can use these extensions like using the system class. Some people suggest that the package can be placed in the extended directory, so that ClassPath is not set, nor It is not very convenient to run correctly, but individuals think that this is not good, can't put it in it, some standard expansion packs can, such as JavaServlet, Java3d, etc. You can make a suggestion, add an environment. Variables, such as Jarpath, specify a directory, store users' JAR ZIP and other packages, this should wait for Sun to do it. Under Windows 98, when I installed, I can't install it, I always crash, it is difficult to install it. The default is not to run correctly, then put Tool.jar into ClassPath and works normally. Now test, remove it is still correct. After multiple tests, I found that I have installed JDK very well, I will crash when I haven't installed it, and it can be installed for a few times. If you find it properly, you can't work properly, add Tools.jar to classpath, try it. Second, package (package "in java It is a more important concept, and package is defined: definition: a package is a collection of research classes and interfaces That Provides Access Protection and namespace management. Also: a package is some of the related classes that provide access protection and namespace management Collection of interfaces. The purpose of using the package is to make the class easy to find usage, prevent naming conflicts, and control access. Here we don't discuss excessive things about the package, only discuss and compile, run, class path-related things. As for Other contents of the package, please check your own documentation. Little, packet is a directory, the subcaps below are subdirectory, this package Class is the file in this directory. We use an example to explain. First, the directory structure is as follows: packagetest / source /, the root directory refers to the packagetest directory, our source program is placed in the Source directory. The source program is as follows: PackageTest .java package pktest; import pktest.subpk *;. public class PackageTest {private String value; public PackageTest (String s) {value = s;} public void printValue () {System.out.println ( "value of PackageTest is" Value);} public static void main (string [] args) {

PackageTest test = new PackageTest ( "This is a Test Package"); test.printValue (); PackageSecond second = new PackageSecond ( "I am in PackageTest"); second.printValue (); PackageSub sub = new PackageSub ( "I am In PackageTest "); Sub.printValue (); System.exit (0);

}

} PackageSecond.java package pktest; public class PackageSecond {private String value; public PackageSecond (String s) {value = s;} public void printValue () {System.out.println ( "Value of PackageSecond is" value);}

PackageSub.java package pktest.subpk; import pktest. *; Public class packagesub {

private String value; public PackageSub (String s) {value = s;} public void printValue () {PackageSecond second = new PackageSecond ( "I am in subpackage."); second.printValue (); System.out.println ( " Value of packagesub is " value);

} Main.java import pktest. *; Import pktest.subpk. *; Public class main () {

Public static void main () {

PackageSecond Second = New PackageSecond ("I am in main"); second.printValue (); packagesub sub = new packagesub ("i am in main"); Sub.printValue (); system.exit (0);

}

} Where main.java is a program other than the package, used to test the classes within the package, PackageTest.java belongs to the pktest. PackageSecond.java is also pktest, and packages are pktest. The SUBPK package is pktest.subpk.

For details, please visit the source program. Ok, first put the source program in the Source directory, so that the source becomes the current directory, then compile, huh, it is mistake, main.java: 1: package pktest Not Found in import Import pktest. *; Here, how to find the package in the class path, we have done a little assumption: "Just contain this class to find this class", there is a problem now. In fact, JDK Tool Javac, Java, Javadoc needs to find classes, see the directory, thinking is the name of the package, for the import statement, a package corresponds to a directory. In this example, IMPORT PKTEST. *, we know that the class path can contain a directory, then With that directory, if you have a directory / myclass, then you will find the / myclass / pktest directory and the class. All are available, if you don't report wrong. Because the current classpath is only the current directory, There is no pktest directory in the current directory, so it will be wrong. The classpath can also contain .jar .zip files, these are the compressed packs that can be taken by the directory, you can see the .jar .zip file as a virtual directory, then then Targeted as the catalog.

Ok, you should know how to do it, the modified directory structure is as follows: packagetest | | __source main.java | | __subtest packagetest.java packagesecond.java | | __subpk packagesub.java then recompile, run, haha, pass. Let's run PackageTest. [Source] $ java pktest / packageTest how to make an error? Exception in thread "main" java.lang.noclassdeffounderror: pktest / packagetest is like this, Java wants to run a class name, it can Regardless of your class, look for this class like the same as before we discussed, so it looks the pktest / packagetest as a class name, of course, it will be wrong, you should do this, [Source] $ JAVA Pktest.PackageTest, you should understand the reason, I will not say much. Note that Javac is different, you can specify the source file path, Javac is only compiled, does not run, the lookup class is only in the source file to touch IMPORT in the source file Do it, there is no relationship with the package where the source file is located. It seems to have some bad places, how to generate .class files are so dispersed, watching true twists, don't worry, Javac has a -D command line parameter, you can specify one Directory, put the generated .CLASS files to you in this directory. [Source] $ mkdir classes [Source] $ javac -d classes pktest / packagetest.java [Source] $ javac -d classes main.java So how do you run? [Source] $ cd classes [classes] $ java pktest.pagetest [classes] $ java main. In fact, JDK's tool is simple and simple, powerful, not used or wrong If the tool is not a tool, the key is some of the principles and necessary knowledge behind the tool. The integrated environment is very good, but it shields a lot of underlying knowledge, not wrong, once an error, if there is no such necessary knowledge It's hard to, I have to ask in BBS, others only tell you the specific way you solved. The next time I encounter a little change, I don't understand. So don't be stamped in tools, Java's tool combination, small and medium-sized projects (five or sixty classes), or payable. Third, JAR files should be. Jar .zip all see as .jar files. 1, from front We can see that JAR files are very important in Java, which greatly facilitates users' use. We can also do your own .jar package.

Or use the previous example, main.java is something outside the package, using the classes in the pktest package, we now have to make a pktest into one. Jar package, very simple, just we have put pktest .class Concentrated, [classes] $ jar -cvf mypackage.jar pktest will generate mypackage.jar files, test it, the main.class we have produced is in the class directory, so you can know from the front: [Classes] $ Java -cp mypackage.jar :. Main can run. 2, if you have seen the example of JDK, you will know, .jar can also run directly, [/ deco] $ java -jar ajar.jar Ok, let's try a try, [classes] $ java -jar mypage.jar failed to load main-class manifest attribwork mypackage.jar It seems that our JAR and its JAR are still different, what is different? ? Take it an example, recompile, generate .jar files, then discover, it is. JAR compression package Meta-inf / manifest.mf file is different, more line-class: xxxxx, then look wrong Information, originally did not specify main-class, look at the jar command, found a parameter -M, -m include manifest information from specified manifest file and error message a bit related, it seems to read a formulation file. I have to write a cat the [classes] $ cat myManifest Manifest-Version:. 1.0 Main-Class: pktest.PackageTest Created-By: 1.2.2 (Sun Microsystems Inc.) [classes] $ jar cvfm mypackage.jar myManifest pktest added manifest adding: pktest / (IN = 0) (OUT = 0) (Stored 0%) Adding: pktest / packagesecond. Class (in = 659) (deflated 40%) Adding: pktest / subpk / (in = 0) (store = 0) Adding: pktest / subpk / packagesub.class (in = 744 (out = 454) (deflated 38%) Adding: pktest / packagetest.class (in = 1041) (OUT =

602) (deflated 42%) [classes] $ java -jar mypackage.jar Value of PackageTest is This is a Test Package Value of PackageSecond is I am in PackageTest Value of PackageSecond is I am in subpackage. Value of PackageSub is I am in PackageTest is ok, success, this will do a .jar file that can be executed directly. You can try it yourself to do a MAIN as the executable JAR. Small knot: This article, we discussed The basic but more important things in Java, mainly Class Path. It is not a simple ClassPath's completeness of ClassPath, but trying to let readers understand that their principles, think about themselves, do their own. In fact Most things are in the sun's Java DOC. I just talk about it slightly, I hope to help. Due to the limited conditions, JDK1.2.2 is tested in 98 and Linux, other versions of JDK And the platform, please test it yourself, it is inevitable, please refer to it. Below is some questions you need to pay attention to: 1. If you need to use .jar files in the class path, you must put the file name of the JAR file into the class path, and Not the directory thereof. 2, at any time, the class name must have a complete package name, 3, "."

The current directory is best in your class path. Below is some common compilation and running mode. 4. To Compile HelloWorld.Java App in The Default package in C: / MyDir, USE CD / MyDir C: /JDK1.3 /bin/javac.exe -classpath. HelloWorld.java 5. To Run a helloworld.class app, in the default package in c: / mydir, use cd / mydir c: /jdk1.3/bin/java.exe -classpath Hello HelloWorld.class app, in the default package in a jar in c: / mydir, uuse cd / mydir c: /jdk1.3/bin/java.exe -classpath helloWorld.jar HelloWorld 7. To Compile a helloworld.java app in c: / mypackage, in package mypackage, use cd / c: /jdk1.3/bin/javac.exe -classpath. mypackage / helloworld.java 8. To Run a helloworld.class app in c : / Mypackage, use cd / c: /jdk1.3/bin/java.exe -classpath. Mypackage.helloworld 9. To Run a helloworld.class app in c: / mypackage, in a jar in package mypackage , USE CD / MYDIR C: /JDK1.3/bin/java.exe -classpath helloworld.jar mypackage.helloWorl (Note: Default package refers to any package in the program). The last small suggestion, Take a look at Sun's JDK Tools Documentation and put JDK's Some tools Java Javac Javadoc Jar JavaP JDB ... It will be good. The Simplest Is The Best. Reference: Java Tools Documentation. Java Glossary http://mindprod.com/powered by jive "Happy Java everyone talks" the organizing committee of the event, you have any opinions or suggestions, please contact Guty

转载请注明原文地址:https://www.9cbs.com/read-43540.html

New Post(0)