By installing the Java environment, we know the respective functions of Java and Javac:

Java is used for execution. If you only want to run external programs, install jre.

Javac is used for compilation and running. This command is only available in the JDK directory. Developers must first compile.java files into.class files. In this case, install the latter.

See the Java | why need two JRE when installation environment

Now that you know what Java and JavAC can do, you’re ready to go.

1. Java files without package names

This one is the most native, opening the command line at the root of the file.

javac Hello.java
A.class file with the same name will appear in the current path of the file. This file is a bytecode file that can be executed.
javac Hello
Output from the Hello program.
Copy the code

2. Java file with package name

In real development, however, Java is often written under packages, which act as our folders for easy categorization. In this case, simply change the original file to a path format and let the compiler find the file through the path.

Note that if you are opening the command line in file Explorer, make sure the current path is in the folder of the highest level package. For example, if used in an IDE, ensure that the current path is directly below the SRC path.

For example, if the project path is in D: program\se-eclipse\ 01Type \, I need to go one notch further into the SRC file to continue the operation. Otherwise, the main class file will not be found.

Change the full file name of the original Java file directly added later to the full file name in path format (compilation). For example, my package name and class are jin-test.helloDemo.java. So the result is that

D:\program\se-eclipse\01type\src> javac jin\test\HelloDemo.java
# compile successfully, no prompt will be given. Also, a compiled.class file is generated, which is executed directly
# Therefore, subsequent files do not need suffixes
D:\program\se-eclipse\01type\src> java jin\test\HelloDemo
hello world
# execute complete
D:\program\se-eclipse\01type\src>
Copy the code

If you want to go directly to the current path of the.java file, you can compile the generated file, but the generated file will contain the package information. Apparently not. Does it still work if there is no information about the package when it is compiled?

When you run the compiled file, you see a prompt similar to the following.

D:\program\se-eclipse\01type\src\jin\test>javac _Number.java

D:\program\se-eclipse\01type\src\jin\testJava _Number error: main class _Number could not be found or loadedAfter all, the program does have the package name information in it. Package specific.
Copy the code

conclusion

Javac also finds programs by path.

2. Java is used to run programs. Javac is used to compile Java files into class files.

3. Javac is designed to compile.java files, so add the.java suffix. A.class file is also called a platform-independent bytecode file. Bytecode is an executable in and of itself, an application in and of itself, without suffixes.

Ps: This article focuses on how to compile and run Java programs with package names.

Refer to the link

Docs.oracle.com/javase/9/to…