java 基本命令 ——未完待续

    技术2022-05-20  38

    一:javacjavac [ options ] [ sourcefiles ] [ @files ] 参数可按任意次序排列。

    options 命令行选项。 sourcefiles 一个或多个要编译的源文件(例如 MyClass.java)。 @files 一个或多个对源文件进行列表的文件。  例如一个名字为mysource.txt文本文档有如下内容: C:/javaTest/src/com/abc/HelloWorld.java C:/javaTest/src/com/abc/SayHelloWorld.java 则可以使用javac @mysource.txt对mysource.txt内列出的java文件进行编译。

    在dos命令窗口下可以通过javac命令还直接查看JDK自带的命令参数说明:C:/javaTest>javacUsage: javac <options> <source files>where possible options include:  -g                         Generate all debugging info  -g:none                    Generate no debugging info  -g:{lines,vars,source}     Generate only some debugging info  -nowarn                    Generate no warnings  -verbose                   Output messages about what the compiler is doing  -deprecation               Output source locations where deprecated APIs are used  -classpath <path>          Specify where to find user class files and annotation processors  -cp <path>                 Specify where to find user class files and annotation processors  -sourcepath <path>         Specify where to find input source files  -bootclasspath <path>      Override location of bootstrap class files  -extdirs <dirs>            Override location of installed extensions  -endorseddirs <dirs>       Override location of endorsed standards path  -proc:{none,only}          Control whether annotation processing and/or compilation is done.  -processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process  -processorpath <path>      Specify where to find annotation processors  -d <directory>             Specify where to place generated class files  -s <directory>             Specify where to place generated source files  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files  -encoding <encoding>       Specify character encoding used by source files  -source <release>          Provide source compatibility with specified release  -target <release>          Generate class files for specific VM version  -version                   Version information  -help                      Print a synopsis of standard options  -Akey[=value]              Options to pass to annotation processors  -X                         Print a synopsis of nonstandard options  -J<flag>                   Pass <flag> directly to the runtime system

     

    值得一提的是-d选项,-d选项用来指定编译生成的类文件的存放路径,但是javac命令不会生成这个路径,必须确保该路径已经存在。另外,使用-d选项后,javac命令会自动生成java文件里package语句声明的包路径(一些文件夹)。

     

    例如有HelloWorld.java文件:package com.abc;public class HelloWorld{ public static void main(String args[]){  System.out.println("hello world!"); }}放在C:/javaTest/src/com/abc内,C:/javaTest>tree /fFolder PATH listing for volume APACCNDLBIW1003Volume serial number is 1877-06AAC:.├─classes└─src    └─com        └─abc                HelloWorld.java

    C:/javaTest>cd srcC:/javaTest/src>javac -classpath .;%classpath% -d C:/javaTest/classes com/abc/HelloWorld.javaC:/javaTest/src>cd ..

    C:/javaTest>tree /fFolder PATH listing for volume APACCNDLBIW1003Volume serial number is 1877-06AAC:.├─classes│  └─com│      └─abc│              HelloWorld.class│└─src    └─com        └─abc                HelloWorld.java

    C:/javaTest>

    可以看到javac命令在-d指定的路径C:/javaTest/classes内生成了com.abc.HelloWorld.class文件。                     


    最新回复(0)