【问题标题】:Java: How can I compile an entire directory structure of code in command prompt? [duplicate]Java:如何在命令提示符下编译代码的整个目录结构? [复制]
【发布时间】:2015-01-09 14:28:58
【问题描述】:

我在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 文件,包括子文件夹和内部子文件夹。

提前致谢,

【问题讨论】:

  • 你可以使用ant,或者简单地将所有java文件保存在一个目录中并使用javac -d outdir *.java

标签: java jakarta-ee


【解决方案1】:

我将对代码的结构做出一些(希望是相当安全的)假设:

  • 你有一个主程序(下面我称之为com.mycode.dto.Main),
  • 它在编译时依赖于其他文件(您没有使用反射或其他),
  • 您的源文件与包结构匹配(com.foo.BarE:\proj\src\com\foo\Bar.java 中)。

在这种情况下,你可以这样做:

javac -d <destination> -sourcepath E:\proj\src E:\proj\src\com\mycode\dto\Main.java

然后编译器会自动遍历文件依赖,将class文件输出到destination

【讨论】:

    【解决方案2】:

    如果你有多个分层目录中的源代码,你可以使用 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,确保你的路径中有它的bin目录,然后你就可以运行了

    ant -f build.xml
    

    当然,这只是一个起点(ant 提供了几个有趣的选项,让您可以微调构建/打包的所有方面)。

    示例 build.xml 文件取自 here

    【讨论】:

    • 我已经有 ant build.xml 并尝试过。但问题是,当我在使用 ant build 编译后尝试使用 cobertura 检测代码时,它显示一些错误并且我的应用程序尚未启动,即使通过 tomcat 管理器也无法启动。
    • 位置:com/cts/itdp/util/licensing/SessionTracker.sessionDestroyed (Ljavax/servlet/http/HttpSessionEvent;) V @70: ifnull 原因:此位置的预期堆栈图帧。此位置的预期堆栈图帧。 BYTECODE:0000000:033D 11030:B800 30013B6 0000020:21 21a0 0016
    • 这似乎与编译无关,可能取决于你的类路径中缺少的东西。尝试一步一步地工作,首先编译,然后产生一个战争,然后部署它,在你看到它正确部署之后,使用 Cobertura。
    【解决方案3】:

    尝试使用通配符:

    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
    

    【讨论】:

    • 我建议添加一个 -d 以至少将类文件放在分层文件系统中(准备好被淘汰)
    • 添加了 OP 可以根据自己的要求使用的选项。
    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2015-10-27
    • 2010-11-22
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多