Detailed explanation of java basics 1----package introduction & CLASSPATH

1. For public classes (public), the java source code file name must be consistent with the class name, otherwise an error will be reported

D:\project\helloWorld>javac hello.java hello.java:1: Error: Class HelloWorld is public, public class HelloWorld
should be declared in a file named HelloWorld.java { ^ 1 errors


2. There can only be one public class in a java file, and you can also add a new class of default type.

3. When there is only a default class in a file, the class name does not need to be the same as the file name, and there can be multiple default classes in the file.

4. Source files in the same directory belong to the same package (package)

5. The source code file does not need to be named as a package, but it is not suitable for use in projects and is only used for simple testing.

HelloWorld.java

//package main;
public class HelloWorld {
    
    

    public static void main(String args []){
    
    

        System.out.println("hello world");

    }
}

Comment out the package, compile and run in the current directory

javac HelloWorld.java
java HelloWorld

6. Introduction of package

1. The package name must be consistent with the physical path of the source file, that is, the package name iscom.hello, then all source files in the package must be incom/hellopath, which brings up the fact that all source files under the same path belong to the same package.

2. Import will only introduce a single package member or the entire contents of the package

import graphics.Rectangle;
import java.awt.*;

7. How are packages introduced when javac compiles source files?

java -help
PS C:\Users\love1\Documents\technology\java\project\hello> javac -help
用法: javac <options> <source files>
其中, 可能的选项包括:
  -g                         生成所有调试信息
  -g:none                    不生成任何调试信息
  -g:{
    
    lines,vars,source}     只生成某些调试信息
  -nowarn                    不生成任何警告
  -verbose                   输出有关编译器正在执行的操作的消息
  -deprecation               输出使用已过时的 API 的源位置
  -classpath <路径>            指定查找用户类文件和注释处理程序的位置
  -cp <路径>                   指定查找用户类文件和注释处理程序的位置
  -sourcepath <路径>           指定查找输入源文件的位置
  -bootclasspath <路径>        覆盖引导类文件的位置
  -extdirs <目录>              覆盖所安装扩展的位置
  -endorseddirs <目录>         覆盖签名的标准路径的位置
  -proc:{
    
    none,only}          控制是否执行注释处理和/或编译。
  -processor <class1>[,<class2>,<class3>...] 要运行的注释处理程序的名称; 绕过默认的搜索进程
  -processorpath <路径>        指定查找注释处理程序的位置
  -parameters                生成元数据以用于方法参数的反射
  -d <目录>                    指定放置生成的类文件的位置
  -s <目录>                    指定放置生成的源文件的位置
  -h <目录>                    指定放置生成的本机标头文件的位置
  -implicit:{
    
    none,class}     指定是否为隐式引用文件生成类文件
  -encoding <编码>             指定源文件使用的字符编码
  -source <发行版>              提供与指定发行版的源兼容性
  -target <发行版>              生成特定 VM 版本的类文件
  -profile <配置文件>            请确保使用的 API 在指定的配置文件中可用
  -version                   版本信息
  -help                      输出标准选项的提要
  -A关键字[=]                  传递给注释处理程序的选项
  -X                         输出非标准选项的提要
  -J<标记>                     直接将 <标记> 传递给运行时系统
  -Werror                    出现警告时终止编译
  @<文件名>                     从文件读取选项和文件

After javac javac, directly root the java source file. When compiling the source file, all imported packages will be based on the current path and go to the physical path corresponding to the package to load the corresponding members.
-classpath or -cp can also specify the base path

8. How to run a program in java

C:\Users\love1\Documents\technology\java\project>java -help
用法: java [-options] class [args...]
           (执行类)java [-options] -jar jarfile [args...]
           (执行 jar 文件)

java package name. Class name will be based on the current path, go to the physical path corresponding to the package name to find the corresponding class, and load other introduced classes based on this path.

9. Set CLASSPATH

windows

1、C:> sdkTool -classpath classpath1;classpath3…

2、set CLASSPATH=classpath1;classpath3…

3、C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses …

4、java -classpath C:\java\MyClasses\myclasses.jar utility.myapp.Cool

5、java -classpath C:\java\MyClasses\myclasses.zip utility.myapp.Cool

linux

1、setenv CLASSPATH path1:path3 (unsetenv CLASSPATH)

2、CLASSPATH = path1:path3:… && export CLASSPATH (unset CLASSPATH)

3、java -classpath /java/MyClasses:/java/OtherClasses …

4、java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool

5、java -classpath /java/MyClasses/myclasses.zip utility.myapp.Cool

Guess you like

Origin blog.csdn.net/qq_41768644/article/details/132815961