How to compile java into class file
This article focuses on "how to compile java into class files", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to compile java into a class file.
1. Overview
General generation compiles java into class files, which can be written as follows:
a)。 Javac [- cp xxx.jar] xxxx.java
b)。 Javac [- cp xxx.jar]-d. Xxxx.java
c)。 Javac [- cp xxx.jar]-d. / classes xxxx.java
First, the second way is to generate the class file in the current directory, and the third way is to generate the class file under the classes folder
Depending on whether there is a package name package in the xxx.java file, the path of the class file generated by the first and the two methods is also different, which is illustrated by an example:
The demo.java file is as follows (no package name package):
Public class demo {public static void main (String [] args) {System.out.println ("hello world!");}} 2. No package explanation
The first way to run the result:
The second way to run the result:
The third way to run the result:
3. When there is a package name
There is a package name in demo.java:
Package cj.test;public class demo {public static void main (String [] args) {System.out.println ("hello world!");}}
The first method cannot be executed after compilation:
The second way:
The third way:
4. Third-party jar
If you use a third-party jar package when compiling (the java file has the package name package, the third-party jar used is placed in the jar folder, and the compiled file is placed in classes):
That is:
Javac-cp compilation using jar file-d compiled class location xxx.java
Java-cp executes the class location; executes the third-party jar compiled class file used (including package name)
At this point, I believe you have a deeper understanding of "how to compile java into class files". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!