如何在Unix系统中使用javac编译所有子文件夹中的Java文件?
在Windows操作系统上...
创建一个批处理文件:
for /r %%a in (.) do (javac %%a\*.java)
然后在顶层源文件夹中执行它。
在Linux上...
javac $(find ./rootdir/* | grep .java)
这两个答案取自于此主题...
http://forums.oracle.com/forums/thread.jspa?threadID=1518437&tstart=15
但正如其他人所建议的,构建工具可能会证明很有用。
find
更好地管理依赖关系。而且,两者均可定义自定义任务以进行编译之外的其他操作。此外,Maven 还提供了用于管理远程存储库中的外部依赖项以及运行单元测试的约定,并支持连续集成的功能。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,那么转换到Maven会更容易。
我不知道这是否是最好的方法,但这应该可以工作:
find . -name "*.java" | xargs javac
find . -name "*.java" | xargs -I {} javac {}
。 - tedtanner如果您知道有多少个文件夹级别,另一种(不太灵活)的方法是:
javac *.java */*.java */*/*.java */*/*/*.java */*/*/*/*.java ...
根据您的shell,您可能需要将其设置为将非匹配模式扩展为空白,在bash中使用shopt -s nullglob
。例如,我正在使用以下shell函数在我的java文件中查找文本:
function jgrep ()
{
(
shopt -s nullglob
egrep --color=ALWAYS -n "$@" *.tex *.java */*.java */*/*.java */*/*/*.java */*/*/*/*.java */*/*/*/*/*.java
)
}
jgrep String
但是,像其他人所说的那样,真的要使用构建工具。
%%a 在此处不是预期。
。 - Lei Yang