Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Ant创建独立jar文件_Java_Ant_Jar - Fatal编程技术网

Java 使用Ant创建独立jar文件

Java 使用Ant创建独立jar文件,java,ant,jar,Java,Ant,Jar,基本Java也使用log4j库,它显示一个带有图像(picture.jpg)和从app.properties读取的文本标签的GUI窗口。 它具有以下结构(build和dist目录由ant'build'和'dist'目标添加): 基本功能: “build”目标将所有源文件编译到“build”目录,并将运行应用程序所需的其他资源复制到“build”目录。然后,可以使用“运行”目标运行此操作。 “dist”目标只是将整个“build”目录压缩到一个jar文件中,并将其放置到“dist”文件夹中 问

基本Java也使用log4j库,它显示一个带有图像(picture.jpg)和从app.properties读取的文本标签的GUI窗口。
它具有以下结构(build和dist目录由ant'build'和'dist'目标添加):

基本功能:
“build”目标将所有源文件编译到“build”目录,并将运行应用程序所需的其他资源复制到“build”目录。然后,可以使用“运行”目标运行此操作。 “dist”目标只是将整个“build”目录压缩到一个jar文件中,并将其放置到“dist”文件夹中

问题
我希望这个罐子完全独立。目前,它使用“runjar”目标运行,也可以在单击jar时运行。 但是,如果我将jar文件移到别处,它将不会启动,因此它仍然依赖于jar之外的文件,尽管jar包含所有必需的文件。 这也可以从jar的MANIFEST.MF文件中看到

Main-Class: com.my.demo.app.Main
Class-Path: ../build/classes/ ../build/img/ ../build/conf/ ../build/ .
 ./build/lib/log4j-1.2.17.jar
此外,如果我做错了其他事情,欢迎任何其他反馈

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="DemoApp" default="build">
    <property file="app.properties"/>

    <path id="compile.classpath" description="Classpath for compiling classes to 'build' dir">
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <path id="run.classpath" description="Classpath for running compiled app in 'build' dir">
        <pathelement location="${build.classes.dir}"/>
        <pathelement location="${build.dir}/img"/>
        <pathelement location="${build.dir}/conf"/>
        <pathelement location="${build.dir}"/>
        <fileset dir="${build.dir}/lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <target name="clean" description="Remove files generated by 'build' and 'dist'">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>

    <target name="compile" description="Compiles the source files">
        <mkdir dir="${build.classes.dir}"/>
        <javac
            srcdir="${src.dir}"
            destdir="${build.classes.dir}"
            debug="true"
            debuglevel="lines,vars,source"
            encoding="utf-8"
            compiler="modern"
            target="1.7"
            source="1.7"
            includeantruntime="false">
            <classpath refid="compile.classpath"/>
        </javac>
    </target>

    <target name="build" depends="compile" description="Builds project">
        <!--Copy app.properties to build/app.properties-->
        <copy todir="${build.dir}">
            <fileset dir="${basedir}" includes="*.properties"/>
        </copy>

        <!--Copy picture.jpg to build/img/picture.jpg-->
        <copy todir="${build.dir}/img">
            <fileset dir="${basedir}/etc/img"/>         
        </copy>

        <!--Copy jars in lib to build/lib-->
        <copy todir="${build.dir}/lib">
            <fileset dir="${lib.dir}" includes="*.jar"/>
        </copy>

        <!--copy log4j.xml to build/conf/log4j.xml -->
        <copy todir="${build.dir}/conf">
            <fileset dir="${basedir}/etc" includes="log4j.xml"/>
        </copy>
    </target>


    <target name="run" depends="build" description="Execute application located in 'build'">
        <java classname="com.my.demo.app.Main" fork="yes" classpathref="run.classpath"/>
    </target>

    <target name="dist" depends="clean,build" description="Create distributable jar">
        <mkdir dir="${dist.dir}"/>
        <jar jarfile="${dist.dir}/DemoApp.jar" basedir="${build.classes.dir}"/>
        <manifestclasspath property="manifest.classpath" jarfile="${dist.dir}/DemoApp.jar">
            <classpath refid="run.classpath" />
        </manifestclasspath>

        <jar destfile="${dist.dir}/DemoApp.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Class" value="com.my.demo.app.Main"/>
                <attribute name="Class-Path" value="${manifest.classpath}"/>
            </manifest>
        </jar>
    </target>

    <target name="runjar" description="Execute jar created with dist target">
        <java jar="${dist.dir}/DemoApp.jar" fork="true"/>
    </target>

</project>

谢谢。

为您嵌入图像。谢谢你告诉我你需要一个有更多代表性的人来做这件事。你能不能尝试使用一个清单:Class Path:build/classes/build/img/etcDoesn不起作用,当执行jar时,我得到一个错误:runjar:[java]错误:找不到或加载主类com.my.demo.app.main
build.dir=${basedir}/build
build.classes.dir=${build.dir}/classes
src.dir=${basedir}/src
lib.dir=${basedir}/lib
dist.dir=${basedir}/dist

read.from.app.properties=This is read from app.properties