classpath、package 和 jar

Error: Can not find or load the main class

First, an example is given:

  1. In the E:\aaacreation A.java directory

    A.java

    public class A {
    	public static void main(String[] args) {
    		System.out.println("AAA");
    	}
    }
    
  2. Compile A.java by cmd

    javac E:\aaa\A.java
    

    At this time in E: generation A.class \ directory under the aaa

  3. By cmd run A.class (in E: \ aaa directory)

    java A
    

    This time it can run successfully

  4. By cmd run A.class (in E: \ directory)

    This time will find that fail, because there is no class A

3 and 4 by the comparison, we can conclude that when the java command, is to look like in the current directory, not recursive search subdirectories in the current directory, if you can not find the class in the current directory, an error will occur .

If I just want to E: Run \ directory E: \ aaa \ class files in the directory, how to do it? Which you need to use this option the classpath.

classpath

In cmd execution java -helpyou can see the official instructions on classpath this option (also including a description of other options)

This command is used to search for class files , which is what we want.

The format is:

java -classpath 类路径 类名

Class can have a plurality of paths, with ;spaced, need not ;end

Then, follow the instructions in the E: \ directory, I would add classpath command:

java -classpath E:\aaa A

Running success!

This time we combine the second part of the classpath of view, you can see why we perform in the current directory java command to run the class file is no problem. Because when we execute the java command, in fact, will join the current directory as a parameter when executing.

That is, we execute java Acommand, actually executes java -classpath . A, which .represents the current directory.

package

The above is just a simple class, A class does not belong to any package, in practice we will put the class in the package, like a large cabinet divided into many small grid, it is easy to find us. Generally package names are written in reverse domain name companies, similar to com.icecoffeethis.

Multistage Java packages are in the form of multi-level folders in the file system, such as com.icecoffee.testcorresponding to the file system is com\icecoffee\testsuch a multi-level folder. That being the case, let me start wondering, if we want to run a com.icecoffee.testclass B under the package, that my command should be like? As follows.

  1. Creating B.java \ bbb \ directory: in E

    B.java

    package com.icecoffee.test;
    
    public class B {
    	public static void main(String[] args) {
    		System.out.println("BBB");
    	}
    }
    
  2. Compile B.java

    javac B.java
    

    In the E: \ bbb \ directory created under the B.class file, this time I see B.class file by jd-gui, find package information in the file is not in the source code

  3. Creating com \ icecoffee \ test multi-level directory \ bbb \ directory, move the file to test B.class directory: in E

    This time viewing by jd-gui again B.class file, find this line of code package information there, why put under the corresponding package will have this line of code, I do not know at present.

  4. The first attempt to run B.class

    java -classpath E:\bbb B
    

    An error

  5. The second attempt to run B.class

    java -classpath E:\bbb\com\icecoffee\test B
    

    An error

  6. B.class correct operation of the command

    java -classpath E:\bbb com.icecoffee.test.B
    

This contrast of 4,5,6 three commands, we can conclude that when the class to run located in a package, the correct command is the outermost layer of the package in the same directory as a parameter the classpath, class name in front of the need to add multi-level package, i.e., the class name to be written in a manner fully qualified class name. In this one, I try to step 5 multi-level parameters included in the package were run in the classpath, proved to be unworkable.

jar

jar package is multiple class files into a compressed package, .jar files can be opened to view by compression software.

In cmd by jar -helpcan see the jar command usage-related

Now I'm part of the third package of B.class files into a jar package

In the E: \ bbb \ directory, execute:

jar cvf B.jar com

com represents the directory to be packaged, since B.class belong to com.icecoffee.testthis package, the outermost layer of the package is com, jar command will recursively the directory

After the success of compression will generate a B.jar compressed file in the current directory.

We can also run directly jar package B.class

java -classpath E:\bbb\B.jar com.icecoffee.test.B

Running success!

In addition, you need to know is that in the implementation of the java command, in addition to the parameters of the classpath, JVM will go jre \ lib and jre \ lib \ ext both the default directory search category, which is why we import the JDK in addition to the java.langoutside of the package of the class can be compiled to run on the cause.

For example, java.util.Datethis class is located rt.jar under jre \ lib directory in this package

Why is in addition to java.langoutside the bag? Because the classes in the package is very common, almost every custom class needs to use this package in the category, for example String. Therefore, all the classes in this package are implicitly introduced, without going through the java file importexplicitly import.

Guess you like

Origin www.cnblogs.com/cailinfeng/p/12609640.html