事情的起源是这样的
写了一个需要读取图像文件的程序,图像文件tank.gif和java文件放在一起,当在cmd中编译并运行的时候没有问题。可当使用eclipse的时候就提示找不到图像文件。
后来发现 需要把图像文件定位于项目根目录下,而不是src目录下,这样就不会找不到了
但为什么再命令行处却没有问题呢???
通过File->Export->ant Build可发现其中的原因
以下是生成的ant build.xml
<project basedir="." default="build" name="TankWar">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../Program Files/eclipse_j2ee"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="TankWar.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src" excludes="**/*.launch, **/*.java"/>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="TankWar.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="Student">
<java classname="Student" failonerror="true" fork="yes">
<classpath refid="TankWar.classpath"/>
</java>
</target>
<target name="TankClient">
<java classname="tank_war.TankClient" failonerror="true" fork="yes">
<classpath refid="TankWar.classpath"/>
</java>
</target>
</project>
请注意加粗的部分
这里指定了一个classpath 为bin
这个任务相当于我们在项目的根目录下输入如下的命令 java -cp bin tank_war.TankClient。
由于指定了classpath,虚拟机会在bin目录下查找着个类,类中使用了一个"tank.gif",这个文件是根据java命令执行的当前目录而定位的,也就是说,他会在项目根目录下查找这个文件,既不是src,也不是bin.
作为对比当我们手动编译的时候 回到src目录下 执行javac -d. *.java,然后运行java tank_war.TankClient,此时java命令执行的当前目录是src,所以会在src目录下查找"tank.gif" 。
写了一个需要读取图像文件的程序,图像文件tank.gif和java文件放在一起,当在cmd中编译并运行的时候没有问题。可当使用eclipse的时候就提示找不到图像文件。
后来发现 需要把图像文件定位于项目根目录下,而不是src目录下,这样就不会找不到了
但为什么再命令行处却没有问题呢???
通过File->Export->ant Build可发现其中的原因
以下是生成的ant build.xml
<project basedir="." default="build" name="TankWar">
<property environment="env"/>
<property name="ECLIPSE_HOME" value="../../../../Program Files/eclipse_j2ee"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="target" value="1.6"/>
<property name="source" value="1.6"/>
<path id="TankWar.classpath">
<pathelement location="bin"/>
</path>
<target name="init">
<mkdir dir="bin"/>
<copy includeemptydirs="false" todir="bin">
<fileset dir="src" excludes="**/*.launch, **/*.java"/>
</copy>
</target>
<target name="clean">
<delete dir="bin"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="build-subprojects,build-project" name="build"/>
<target name="build-subprojects"/>
<target depends="init" name="build-project">
<echo message="${ant.project.name}: ${ant.file}"/>
<javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
<src path="src"/>
<classpath refid="TankWar.classpath"/>
</javac>
</target>
<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
<target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
<copy todir="${ant.library.dir}">
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</copy>
<unzip dest="${ant.library.dir}">
<patternset includes="jdtCompilerAdapter.jar"/>
<fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
</unzip>
</target>
<target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
<antcall target="build"/>
</target>
<target name="Student">
<java classname="Student" failonerror="true" fork="yes">
<classpath refid="TankWar.classpath"/>
</java>
</target>
<target name="TankClient">
<java classname="tank_war.TankClient" failonerror="true" fork="yes">
<classpath refid="TankWar.classpath"/>
</java>
</target>
</project>
请注意加粗的部分
这里指定了一个classpath 为bin
这个任务相当于我们在项目的根目录下输入如下的命令 java -cp bin tank_war.TankClient。
由于指定了classpath,虚拟机会在bin目录下查找着个类,类中使用了一个"tank.gif",这个文件是根据java命令执行的当前目录而定位的,也就是说,他会在项目根目录下查找这个文件,既不是src,也不是bin.
作为对比当我们手动编译的时候 回到src目录下 执行javac -d. *.java,然后运行java tank_war.TankClient,此时java命令执行的当前目录是src,所以会在src目录下查找"tank.gif" 。
本文探讨了在Eclipse中编译Java程序与命令行编译的区别。当在命令行中编译并运行包含资源文件的程序时,程序能够正确找到资源。但在Eclipse中,资源文件需要放置在项目根目录而非src目录下,才能被找到。原因是Eclipse的Ant构建过程设置了特定的classpath,使得虚拟机在bin目录下查找类和资源。相比之下,命令行编译时,Java命令的当前目录影响了资源定位。
1014

被折叠的 条评论
为什么被折叠?



