eclipse中编译java程序,究竟跟命令行中编译java程序究竟有什么区别

来源:互联网 发布:软件监理测试方法 编辑:程序博客网 时间:2024/04/27 15:58
事情的起源是这样的
写了一个需要读取图像文件的程序,图像文件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" 。



原创粉丝点击