900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > javac 编译java_如何使用Javac工具编译Java源代码?

javac 编译java_如何使用Javac工具编译Java源代码?

时间:2018-11-08 21:07:13

相关推荐

javac 编译java_如何使用Javac工具编译Java源代码?

javac 编译java

javac is a tool used to compile Java applications or source code. javac reads class and interface definitions and compiles them into bytecode class files. These class files have *.class extension.

javac是用于编译Java应用程序或源代码的工具。 javac读取类和接口定义,并将其编译为字节码类文件。 这些类文件具有* .class扩展名。

句法 (Syntax)

javac usage syntax is a bit complex according to generally Linux tools.

根据一般Linux工具,javac使用语法有点复杂。

javac OPTIONS SOURCEFILES CLASSES ARGFILES

OPTIONS are used to specify command-line options provided by javacOPTIONS用于指定javac提供的命令行选项 SOURCEFILES are used to specify one or more source files to be compiledSOURCEFILES用于指定一个或多个要编译的源文件CLASSES are used to specify one or more classes to be processed for annotations such as YourPackage.YourClassCLASSES用于为注释(如YourPackage.YourClass)指定要处理的一个或多个类。ARGFILES are used to specify one or more files that contain options and sources filesARGFILES用于指定一个或多个包含选项和源文件的文件

显示Javac版本(Show Javac Version)

Java Development Kit(JDK) and Java Runtime Environment(JRE) can have different versions we target this will also affect the version of the javac. If we want to use specific Java programming language version we need to check the javac version with the--version.

Java开发套件(JDK)和Java运行时环境(JRE)可以具有我们所针对的不同版本,这也将影响javac的版本。 如果要使用特定的Java编程语言版本,则需要使用--version检查javac版本。

$ javac --version

Show Javac Version
显示Javac版本

编译Java源代码(Compile Java Source Code)

We will start with a simple javac example where we will compile a single Java source code file. As we know Java source code files generally use *.java extension. We will just provide the file name to the javac compiler which is my.java in this example.

我们将从一个简单的javac示例开始,在该示例中,我们将编译一个Java源代码文件。 众所周知,Java源代码文件通常使用* .java扩展名。 我们仅将文件名提供给javac编译器,在此示例中为my.java。

We will use the following sample HelloWorld.java source code file. We will try to compile this Java source file with the javac command line tool.

我们将使用以下示例HelloWorld.java源代码文件。 我们将尝试使用javac命令行工具编译此Java源文件。

/*FileName : "HelloWorld.java" Example Application" */class HelloWorld { public static void main(String args[]) { System.out.println("Hello, World"); } }

Below we will provides the java source file name HelloWorld.java which will compile and create the compiled byte code file namedHelloWorld.class. As you can see the new extension is.classwhich is a compiled Java class file.

下面,我们将提供Java源文件名称HelloWorld.java,它将编译并创建名为HelloWorld.class的已编译字节代码文件。 如您所见,新扩展名是.class,它是一个已编译的Java类文件。

$ javac HelloWorld.java$ file HelloWorld.class

编译多个Java文件 (Compile Multiple Java Files)

In the previous example, we have compiled a single Java file. But this is insufficient for big projects where we need to compile multiple Java files in a single command. We can specify multiple Java files to the javac in order to compile to the byte code. We will just append the file names by separating them with spaces.

在前面的示例中,我们已经编译了一个Java文件。 但这对于大型项目是不够的,在大型项目中,我们需要在一个命令中编译多个Java文件。 我们可以为javac指定多个Java文件,以便编译为字节码。 我们将通过用空格分隔它们来追加文件名。

$ javac HelloWorld.java HiWorld.java IAmHere.Java

编译所有Java文件 (Compile All Java Files)

We can also compile all java files in the specified directory we will just use asterisk and .java to specify files with java extension in the current directory.

我们还可以编译指定目录中的所有Java文件,我们将仅使用星号和.java在当前目录中指定带有Java扩展名的文件。

$ javac *.java

We can also specify a different directory from the current working directory. In this example, we will compile all Java files residing in/home/ismail/MyProject.

我们还可以指定与当前工作目录不同的目录。 在此示例中,我们将编译/home/ismail/MyProject所有Java文件。

$ javac /home/ismail/MyProject/*.java

在详细模式下打印详细的编译日志 (Print Detailed Compile Log In Verbose Mode)

During compilation, we may need to print detailed information like libraries, operations warnings etc. We can use the-verboseoption which will provide detailed debug information about the compile operation.

在编译期间,我们可能需要打印详细信息,例如库,操作警告等。我们可以使用-verbose选项,该选项将提供有关编译操作的详细调试信息。

$ javac -verbose HelloWorld.java

Print Detailed Compile Log In Verbose Mode
在详细模式下打印详细的编译日志

指定Java文件和文件中的选项(Specify Java Files and Options From File)

Up to now, we have provided the Java files and options from the interactive shell or command line. If there are a lot of detailed file names and options where we will run them over and over again we can use files to specify the Java file named and options. We will use a file named config which will provide Java files and javac option to the javac command. We will use @ sign before the config file.

到目前为止,我们已经从交互式外壳程序或命令行中提供了Java文件和选项。 如果有很多详细的文件名和选项,我们将一遍又一遍地运行它们,我们可以使用文件来指定名为和选项的Java文件。 我们将使用一个名为config的文件,它将为javac命令提供Java文件和javac选项。 我们将在配置文件之前使用@符号。

LEARN MORE Php Tutorial了解更多PHP教程

Here is the config file content which is used in previous examples.

这是在先前示例中使用的配置文件内容。

HelloWorld.java-verbose

We will provide this config file with the@sign like below. Keep in mind that we can use a different name than config for this feature.

我们将为该配置文件提供@符号,如下所示。 请记住,对于此功能,我们可以使用与config不同的名称。

$ java @config

Specify JavaFiles and Options From File
从文件中指定JavaFiles和选项

指定目标路径(Specify The Destination Path)

After given Java files are compiled into byte code the new files are stored in the current working directory. These files will have the same name as the source file and extension will be class. We can specify different output or destination path to put created byte code or binary files We will use-doption and the destination path.

在将给定的Java文件编译为字节代码之后,新文件将存储在当前工作目录中。 这些文件将与源文件具有相同的名称,并且扩展名将为class。 我们可以指定不同的输出或目标路径来放置创建的字节码或二进制文件。我们将使用-d选项和目标路径。

$ javac -d /home/ismail/binary HelloWorld.java

指定类路径 (Specify Class Path)

If we need to provide types required to compile our source file we can use the-cpoption and provide classpath like below. If this option is not provided the CLASSPATH environment variable will be used.

如果我们需要提供编译源文件所需的类型,则可以使用-cp选项并提供如下所示的类路径。 如果未提供此选项,则将使用CLASSPATH环境变量。

$ javac -cp /bin/lib/MyProject HelloWorld.java

指定模块或库路径 (Specify The Module or Library Path)

We can also use-poption in order to specify modules location. But this option can be used after Java 9.

我们也可以使用-p选项来指定模块的位置。 但是此选项可以在Java 9之后使用。

$ javac -p /bin/lib/MyProject HelloWorld.java

翻译自: /how-to-compile-java-source-code-with-javac-tool/

javac 编译java

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。