java虚拟机相关命令之javac命令说明
javac命令说明root@k8smaster01 json]# javacUsage: javac <options> <source files>where possible options include:-gGenerate all debugging info-g:none...
javac命令说明
root@k8smaster01 json]# javac
Usage: 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 #指定编译java源文件需要依赖的.class文件的路径,可以是目录,jar文件,zip文件(里面都是class文件)
-sourcepath <path> Specify where to find input source files #指定编译java源文件需要依赖的java文件的路径,可以是目录,jar文件,zip文件(里面都是java文件)
-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
-parameters Generate metadata for reflection on method parameters
-d <directory> Specify where to place generated class files #指定生成的class的位置,会根据包名递归生成
-s <directory> Specify where to place generated source files
-h <directory> Specify where to place generated native header 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 #为特定虚拟机版本生成类文件
-profile <profile> Check that API used is available in the specified profile
-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
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file @一个或多个对源文件进行列表的文件
[root@k8smaster01 json]#
案例
1)简单单个无package的java文件编译
[root@k8smaster01 daima]# mkdir simpledemo #创建目录
[root@k8smaster01 daima]# cd simpledemo/
[root@k8smaster01 simpledemo]# pwd
/data/java/daima/simpledemo
[root@k8smaster01 simpledemo]# vi Test.java #编辑Test.java文件,打印一行文字
[root@k8smaster01 simpledemo]# more Test.java
public class Test {
public static void main(String[] args) {
System.out.println("javac simple demo");
}
}
[root@k8smaster01 simpledemo]# javac Test.java -d . #使用javac编译,class存放在当前目录
[root@k8smaster01 simpledemo]# ls -lrt
total 8
-rw-r--r-- 1 root root 116 Mar 3 23:39 Test.java
-rw-r--r-- 1 root root 419 Mar 3 23:40 Test.class
[root@k8smaster01 simpledemo]# java Test #执行class文件,正常打印javac simple demo数据
javac simple demo
[root@k8smaster01 simpledemo]#
2)简单单个有package的java文件编译
[root@k8smaster01 simpledemo]# mkdir -p src/com/test #新建目录
[root@k8smaster01 simpledemo]# mv Test.java src/com/test/ #拷贝之前的Test的java文件
[root@k8smaster01 simpledemo]# vi src/com/test/Test.java #编辑java文件,新增package
[root@k8smaster01 simpledemo]# more src/com/test/Test.java
package com.test;
public class Test {
public static void main(String[] args) {
System.out.println("javac simple demo");
}
}
[root@k8smaster01 simpledemo]# tree /data/java/daima/simpledemo/ #目录结构
/data/java/daima/simpledemo/
└── src
└── com
└── test
└── Test.java
3 directories, 1 file
[root@k8smaster01 simpledemo]# mkdir clazz #新增clazz目录存放编译后的class文件
[root@k8smaster01 simpledemo]# ls -lrt
total 0
drwxr-xr-x 3 root root 16 Mar 3 23:43 src
drwxr-xr-x 2 root root 6 Mar 3 23:46 clazz
[root@k8smaster01 simpledemo]# javac src/com/test/Test.java -d ./clazz/ #使用javac编译
[root@k8smaster01 simpledemo]# tree /data/java/daima/simpledemo/ #编译后的目录结构
/data/java/daima/simpledemo/
├── clazz
│ └── com
│ └── test
│ └── Test.class
└── src
└── com
└── test
└── Test.java
6 directories, 2 files
[root@k8smaster01 simpledemo]# java -cp clazz/ com.test.Test #运行编译后的文件,正常打印javac simple demo
javac simple demo
[root@k8smaster01 simpledemo]#
3)批量的的java文件编译(涉及有依赖包)
[root@k8smaster01 Test]# tree /data/java/daima/Test #工程代码结构
/data/java/daima/Test
├── clazz
├── config
├── lib
│ └── gson-2.3.jar
└── src
└── com
├── data
│ └── ParaMap.java
└── json
└── Test.java
7 directories, 3 files
[root@k8smaster01 daima]# more src/com/data/ParaMap.java
package com.data;
import java.util.HashMap;
import java.util.Map;
public class ParaMap {
public static Map<String,String> getMap(){
Map<String,String> sysParams = new HashMap<String,String>();
sysParams.put("localForceNewThread", "true");
sysParams.put("uuid", "28bcf60840f8416e8ea55ffe970c1623");
sysParams.put("log", "348a8a27e315e5a26c68a002.348a8a27e315e5a26c68a003.0");
return sysParams;
}
}
[root@k8smaster01 daima]# more src/com/json/Test.java
package com.json;
import java.util.Map;
import com.data.ParaMap;
import com.google.gson.Gson;
public class Test {
public static void main(String[] args) {
Gson gson = new Gson();
Map<String, String> sysParams = ParaMap.getMap();
String json=gson.toJson(sysParams);
System.out.println(json);
}
}
[root@k8smaster01 daima]#
[root@k8smaster01 daima]# more javac.sh #编译脚本,编译后的文件放在clazz中
#!/bin/bash
PROCESS_NAME=test_javac
BASE_APP_HOME="/data/java/daima/Test"
jars=`ls ${BASE_APP_HOME}/lib`
for jar in $jars
do
CLASSPATH=${BASE_APP_HOME}/lib/$jar:$CLASSPATH
done
CLASSPATH="${BASE_APP_HOME}/config:${CLASSPATH}"
export CLASSPATH
echo "CLASSPATH=${CLASSPATH}"
find ${BASE_APP_HOME} -name "*.java" > ${BASE_APP_HOME}/sources.txt
sourcesclazz=`cat ${BASE_APP_HOME}/sources.txt`
echo "${sourcesclazz}"
javac -encoding UTF-8 -cp ${CLASSPATH} -d ${BASE_APP_HOME}/clazz @${BASE_APP_HOME}/sources.txt
[root@k8smaster01 daima]# sh javac.sh #执行编译
CLASSPATH=/data/java/daima/Test/config:/data/java/daima/Test/lib/gson-2.3.jar:.:/data/java/jdk18/lib/dt.jar:/data/java/jdk18/lib/tools.jar
/data/java/daima/Test/src/com/json/Test.java
/data/java/daima/Test/src/com/data/ParaMap.java
[root@k8smaster01 daima]# cp -r Test/clazz/com/ Test/config/ #拷贝编译后的文件到config目录
[root@k8smaster01 daima]# ls -lrt Test/config/
total 0
drwxr-xr-x 4 root root 28 Mar 4 00:08 com
[root@k8smaster01 daima]#
[root@k8smaster01 daima]# more java.sh #运行编译后的文件
#!/bin/bash
PROCESS_NAME=test_java
BASE_APP_HOME="/data/java/daima/Test"
jars=`ls ${BASE_APP_HOME}/lib`
for jar in $jars
do
CLASSPATH=${BASE_APP_HOME}/lib/$jar:$CLASSPATH
done
CLASSPATH="${BASE_APP_HOME}/config:${CLASSPATH}"
export CLASSPATH
echo "CLASSPATH=${CLASSPATH}"
java -cp ${CLASSPATH} com.json.Test
[root@k8smaster01 daima]# sh java.sh #运行编译后的文件,正常打印json串
CLASSPATH=/data/java/daima/Test/config:/data/java/daima/Test/lib/gson-2.3.jar:.:/data/java/jdk18/lib/dt.jar:/data/java/jdk18/lib/tools.jar
{"log":"348a8a27e315e5a26c68a002.348a8a27e315e5a26c68a003.0","localForceNewThread":"true","uuid":"28bcf60840f8416e8ea55ffe970c1623"}
[root@k8smaster01 daima]#
链接:https://pan.baidu.com/s/1ZLHiJ9fapVzjYV7BkIisrg 相关代码
更多推荐
所有评论(0)