Java:如何在命令提示符下编译整个目录结构的代码?
我在window7平台中使用Eclipse和Tomcat7,我也在Eclipse中配置了项目。通常我们使用CMD运行单个java文件。 但是我想通过命令提示符编译和运行整个java代码。 我在单个src文件夹中有很多结构,比如E:\proj\src\com\mycode。mycode文件夹中有7个子文件夹可用&每个子文件夹都有许多.java文件和内部子文件夹。 例如: E:\proj\src\com\mycode\dto\mail.java,E:\proj\src\com\mycode\dto\sms.java E:\proj\src\com\mycode\dto\security\securityFile.java 上面相同的模式其他文件夹都有java文件。所以我需要使用命令提示符编译并运行整个java文件,包括子文件夹和内部子文件夹 提前感谢,尝试使用通配符:Java:如何在命令提示符下编译整个目录结构的代码?,java,jakarta-ee,Java,Jakarta Ee,我在window7平台中使用Eclipse和Tomcat7,我也在Eclipse中配置了项目。通常我们使用CMD运行单个java文件。 但是我想通过命令提示符编译和运行整个java代码。 我在单个src文件夹中有很多结构,比如E:\proj\src\com\mycode。mycode文件夹中有7个子文件夹可用&每个子文件夹都有许多.java文件和内部子文件夹。 例如: E:\proj\src\com\mycode\dto\mail.java,E:\proj\src\com\mycode\dto\
javac *.java
您可以根据需要使用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
-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
用法:javac
可能的选择包括:
-g生成所有调试信息
-g:无生成调试信息
-g:{lines,vars,source}只生成一些调试信息
-nowarn不生成警告
-详细输出有关编译器正在执行的操作的消息
-弃用输出使用弃用API的源位置
-类路径指定查找用户类文件和注释处理器的位置
-cp指定查找用户类文件和注释处理器的位置
-sourcepath指定查找输入源文件的位置
-引导类路径覆盖引导类文件的位置
-extdirs覆盖已安装扩展的位置
-ENDORSEDIR覆盖已认可标准路径的位置
-proc:{none,only}控制是否完成注释处理和/或编译。
-处理器[,,…]要运行的注释处理器的名称;绕过默认的发现过程
-processorpath指定在何处查找注释处理器
-d指定放置生成的类文件的位置
-s指定放置生成的源文件的位置
-隐式:{none,class}指定是否为隐式引用的文件生成类文件
-编码指定源文件使用的字符编码
-源提供与指定版本的源兼容性
-目标生成特定VM版本的类文件
-版本信息
-帮助打印标准选项的概要
-要传递给注释处理器的Akey[=value]选项
-X打印非标准选项的概要
-J直接传递到运行时系统
如果在多个层次目录中有源,可以使用ant 在项目目录的根目录中创建build.xml文件
<project name="MyProject" default="dist" basedir=".">
<description>
simple example build file
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile"
description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean"
description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
当然,这只是一个起点(ant提供了几个有趣的选项,让您可以微调构建/打包的所有方面)
示例build.xml文件取自,我将对代码的结构做出一些(希望是合理安全的)假设:
- 你有一个主程序(我称之为
com.mycode.dto.main
- 它对其他文件具有编译时依赖性(您没有使用反射或任何东西)
- 源文件与包结构匹配(
位于com.foo.Bar
)E:\proj\src\com\foo\Bar.java
javac -d <destination> -sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java
javac-d-sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java
然后编译器将自动遍历文件依赖项,并将类文件输出到
目的地
您可以使用ant,或者只需将所有java文件保存在一个目录中,并使用javac-d outdir*。javaI建议添加a-d,以便至少将类文件保存在分层文件系统中(准备就绪)已经添加了OP可以根据自己的需求使用的选项。我已经有了ant build.xml并尝试过了。但问题是,在使用ant build编译后,当我尝试使用cobertura插入代码时,它显示出一些错误&我的应用程序即使通过tomcat manager也没有启动。位置:com/cts/itdp/util/licensing/SessionTracker.sessionDestroyed(Ljavax/servlet/http/HttpSessionEvent;)V@70:ifnull原因:此位置应为stackmap帧。此位置应为stackmap帧。字节码:0000000:033d 11ff ff3e 120d 1100 1eb8 0013 1202 0000010:b800 25bb 0027 59b7 0028 122a b600 2eb8 0000020:0034 b600 37b6 003b b600 3f12 0d11 0021 0000030:b800 132b b600 4512 47b9 004d 0200 1100 0000040:213d 1100 003e c600 281c 1100 21A00016这似乎与编译无关,可能取决于类路径上缺少的内容。尝试一步一步地工作,首先编译,然后生成一个war,然后部署它,在您看到它正确部署之后,使用Cobertura。
javac -d <destination> -sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java