如何让打的可执行jar包可以包含外部库的解压文件

时间:2022-06-06 15:29:04


1、把别的jar包库,解压到你的classes目录下面,然后一起打成可执行jar包

build.xml 如下:

<?xml version="1.0"?>
<project name="parser" default="compile" basedir="."> 
    <target name="init">
        <mkdir dir="bin"> </mkdir>
        <mkdir dir="dest"> </mkdir>
    </target>

    <path id="classpath.local">
        <pathelement location="./lib/log4j-1.2.14.jar"/>
        <pathelement location="./lib/mysql-connector-java-5.0.7-bin.jar"/>
        <pathelement location="./lib/je-3.2.43.jar"/>
        <pathelement location="./lib/nekohtml.jar"/>
        <pathelement location="./lib/Tidy.jar"/>
        <pathelement location="./lib/xercesImpl.jar"/>
        <pathelement location="bin"/>
        <pathelement location="bin/com"/>
        <pathelement location="."/>
    </path>

    <target name="compile" depends="init">
        <javac destdir="bin" srcdir="src">
            <classpath refid="classpath.local"/>
        </javac>
    </target>

    <target name="run" depends="compile">
        <java classname="bin/Parser" classpath="classpath.local"> </java>
    </target>

    <target name="dest" depends="compile">
        <mkdir dir="dest"></mkdir>
        <jar jarfile="dest/parser.jar" basedir="bin"></jar>
    </target>

    <target name="clean">
        <delete dir="bin"> </delete>
        <delete dir="dest"> </delete>
    </target>

    <target name="unziplib">
        <unjar dest="bin">
            <fileset dir="lib" includes="*.jar" />
        </unjar>
    </target>

    <target name="jar" depends="compile,unziplib">
        <jar jarfile="dest/Parser.jar" basedir="bin" >
        <manifest>
            <attribute name="Main-class" value="com.dyoldfish.parser.Parser"/>
        </manifest>
    </jar>
    <copy  todir="dest">
        <fileset dir="doc">
            <include name="Parser.bat"/>
            <include name="clean.bat"/>
        </fileset>
    </copy>
  </target>

</project>  

2、把别的jar包库,放在你的可执行jar包外面,然后打包
build.xml如下:

<?xml version="1.0"?>
<project name="parser" default="compile" basedir="."> 
    <target name="init">
        <mkdir dir="bin"> </mkdir>
        <mkdir dir="dest"> </mkdir>
    </target>

    <path id="classpath.local">
        <pathelement location="./lib/log4j-1.2.14.jar"/>
        <pathelement location="./lib/mysql-connector-java-5.0.7-bin.jar"/>
        <pathelement location="./lib/je-3.2.43.jar"/>
        <pathelement location="./lib/nekohtml.jar"/>
        <pathelement location="./lib/Tidy.jar"/>
        <pathelement location="./lib/xercesImpl.jar"/>
        <pathelement location="bin"/>
        <pathelement location="bin/com"/>
        <pathelement location="."/>
    </path>

    <target name="compile" depends="init">
        <javac destdir="bin" srcdir="src">
            <classpath refid="classpath.local"/>
            <!--include name="com/**"/-->
        </javac>
        <copy  todir="bin">
            <fileset dir="doc" includes="run.bat"/>
        </copy>
    </target>

    <target name="run" depends="compile">
        <java classname="bin/Parser" classpath="classpath.local"> </java>
    </target>

    <target name="dest" depends="compile">
        <mkdir dir="dest"></mkdir>
        <jar jarfile="dest/parser.jar" basedir="bin"></jar>
        <!--copy todir="dest"/-->
            <!--fileset dir="bin" includes="*.class" /-->
        <!--/copy-->
    </target>

    <target name="clean">
        <delete dir="bin"> </delete>
        <delete dir="dest"> </delete>
    </target>

    <target name="jar" depends="compile">
        <jar jarfile="dest/Parser.jar" basedir="bin" 
        includes="com/**/*.class">
        <manifest>
            <attribute name="Main-class" value="com.dyoldfish.parser.Parser"/>
            <attribute name="Class-path" value=". ../lib/log4j-1.2.14.jar ../lib/mysql-connector-java-5.0.7-bin.jar ../lib/je-3.2.43.jar ../lib/nekohtml.jar ../lib/Tidy.jar ../lib/xercesImpl.jar"/>
        </manifest>
        <!--fileset dir="lib" includes="*.jar" /-->
    </jar>
    <copy  todir="dest">
        <fileset dir="doc" includes="Parser.bat"/>
    </copy>
  </target>

</project>