打包java,打包java项目为jar包

发布时间:2023-01-09

本文目录一览:

1、JAVA程序怎样打包
2、怎么把java类打包成
3、java程序如何打包成apk?
4、怎样将编译好的java程序打包成可执行文件?
5、java如何打包
6、怎么把java文件打包

JAVA程序怎样打包

第一:普通类导出jar包,我说的普通类就是指此类包含main方法,并且没有用到别的jar包。

  1. 在eclipse中选择你要导出的类或者package,右击,选择Export子选项;
  2. 在弹出的对话框中,选择java文件---选择JAR file,单击next;
  3. 在JAR file后面的文本框中选择你要生成的jar包的位置以及名字,注意在Export generated class files and resources和Export java source files and resources前面打上勾,单击next;
  4. 单击两次next按钮,到达JAR Manifest Specification。注意在最底下的Main class后面的文本框中选择你的jar包的入口类。单击Finish,完成。
  5. 你可以在dos环境下,进入你的jar所在的目录,运行 java -jar 名字.jar,检测运行是否正确。 第二、你所要导出的类里边用到了别的jar包。比如说你写的类连接了数据库,用到数据库驱动包oracl.jar。
  6. 先把你要导出的类按照上面的步骤导出形成jar包,比如叫test.jar
  7. 新建一个文件夹main,比如在D盘根目录下;
  8. 把test.jar和oracl.jar拷贝到main文件夹下,右击test.jar,解压到当前文件夹。把META-INF\MANIFEST.MF剪切到另外一个地方 (比如是桌面!);
  9. 右击oracl.jar,解压到当前文件夹。
  10. 在dos环境下,进入到D盘的main文件夹下,执行 jar cvfm new.jar meta-inf/manifest.mf .,不要忘了最后面的点。
  11. 用压缩工具打开你新生成的new.jar,用你放在桌面的META-INF\MANIFEST.MF覆盖new.jar原有。
  12. 你可以在dos环境下,进入你的jar所在的目录,运行 java -jar 名字.jar,检测运行是否正确。

怎么把java类打包成

使用Eclipse的Export功能:

一、打包成一般的jar包:

步骤如下:

  1. 在要打包的项目上右击,选择Export
  2. 在弹出的窗口中,选择Java - JAR File,然后点击next按钮
  3. 在JAR File Specification窗口中,设置打包成的文件名和存放位置,点击两侧next
  4. 在JAR Manifest Specification窗口中,设置MANIFEST.MF清单文件的配置,
    • 若仅仅打包成单纯的jar包的话,不用做任何修改,采取默认即可
    • 若打包成可执行jar包的话,可以使用已存在的MANIFEST文件或者直接选择Main class
  5. 点击Finish按钮,完成打包。

二、打包成可运行的jar包

步骤如下:

  1. 在要打包的项目上右击,选择Export
  2. 在弹出的窗口中,选择Java - Runnable JAR File,然后点击next按钮
  3. 在Runnable JAR File Specification窗口中,选择Launch configuration和Export destination
  4. 点击Finish按钮,打包完成。

java程序如何打包成apk?

首先下载eclipse与android插件,安装完毕后,建立android项目,编码完成后,生成keystore,使用jdk自带的keytool-genkey -aliasandroid.keystore -keyalg RSA -validity 100000 -eystore android.keystore 然后选择要打包的项目,右键点击_Androidtools_Export Signed Application Package,选择哪个项目,下一步后,选择刚才生成的那个key和生成时录入的密码,下一步设置输出位置。 完成APK生成。 WEB项目不可能生成APK ,如果你现有项目可以把相关的源码文件复制到新建的android项目内,但是lib包有可能不支持,要看你用了哪里lib。

怎样将编译好的java程序打包成可执行文件?

需要的工具myeclipse 、jar2exe 步骤:

  1. 将Java项目导出成.jar包,鼠标右键项目名,选择导出
  2. 导出的方式选择Runnable JAR file,点击next继续
  3. 选择导出文件的路径
  4. jar文件成功导出到桌面
  5. 用工具将jar文件打包成EXE可执行文件,选择刚才导出的jar文件
  6. 选择要导出程序的类型
  7. 选择主类,图片可不选
  8. 导出程序的具体设置
  9. 选择辅助jar文件没有则不选
  10. 最后为EXE文件命名和更换图标
  11. 成功生成!

java如何打包

建议使用ANT打包工具,下载地址: 此工具用java编写,跨平台,能实现很多功能:编译、打包、运行、文件操作、数据库操作、自定义任务等。 主要使用方法:在工程目录下编写build.xml配置文件,然后运行ant即可:

#ant

#java -jar ant.jar

下面给你提供一个例子,是jakarta-oro-2.0.8的包的build.xml

<?xml version="1.0"?>
<project default="jar">
    <!-- Allow properties following these statements to be overridden -->
    <!-- Note that all of these don't have to exist. They've just been defined
    incase they are used. -->
    <property file="build.properties"/>
    <!--
    <property file=".ant.properties"/>
    <property file="${user.home}/.ant.properties"/>
    <property file="default.properties"/>
    -->
    <!-- prepare target. Creates build directories. -->
    <target name="splash">
        <splash imageurl="./ant_logo_medium.gif"/>
    </target>
    <target name="prepare" depends="splash" description="Creates build directories.">
        <tstamp>
            <format property="DATE" pattern="yyyy-MM-dd hh:mm:ss" />
        </tstamp>
        <mkdir dir="${build.dest}"/>
        <mkdir dir="${build.dest}/META-INF"/>
        <copy todir="${build.dest}/META-INF">
            <fileset dir="${top.dir}">
                <include name="LICENSE"/>
            </fileset>
        </copy>
        <mkdir dir="${build.tests}"/>
        <available file="${jakarta-site2.dir}/lib" type="dir"
            property="AnakiaTask.present"/>
    </target>
    <target name="prepare-error" depends="prepare"
        description="Prints error message for prepare target."
        unless="AnakiaTask.present">
        <echo>
            AnakiaTask is not present! Please check to make sure that
            velocity.jar is in your classpath.
        </echo>
    </target>
    <!-- lib target. Compiles the library classes only -->
    <target name="lib" depends="prepare"
        description="Compiles the library classes only.">
        <javac srcdir="${build.src}"
            destdir="${build.dest}"
            excludes="examples/**,tools/**"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}"/>
    </target>
    <!-- examples target. Compiles the example classes. -->
    <target name="examples" depends="prepare,lib"
        description="Compiles the example classes.">
        <javac srcdir="${build.src}"
            destdir="${build.dest}"
            includes="examples/**"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}"/>
    </target>
    <!-- tools target. Compiles the tool classes. -->
    <target name="tools" depends="prepare,lib"
        description="Compiles the tool classes.">
        <javac srcdir="${build.src}"
            destdir="${build.dest}"
            includes="tools/**"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}"/>
    </target>
    <!-- tests target. Compiles and runs the unit tests. -->
    <target name="tests" depends="prepare,lib"
        description="Compiles and runs the unit tests.">
        <javac srcdir="${build.src}/tests"
            destdir="${build.tests}"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}"/>
    </target>
    <!-- jar target. Compiles the source directory and creates a .jar file -->
    <target name="jar" depends="lib" description="Compiles the source directory and creates a .jar file.">
        <jar jarfile="${top.dir}/${final.name}.jar"
            basedir="${build.dest}"
            includes="org/**,META-INF/**"
            excludes="**/package.html,**/overview.html">
            <manifest>
                <section name="org/apache/oro">
                    <attribute name="Specification-Title"
                        value="Jakarta ORO" />
                    <attribute name="Specification-Version"
                        value="${version}" />
                    <attribute name="Specification-Vendor"
                        value="Apache Software Foundation" />
                    <attribute name="Implementation-Title"
                        value="org.apache.oro" />
                    <attribute name="Implementation-Version"
                        value="${version} ${DATE}" />
                    <attribute name="Implementation-Vendor"
                        value="Apache Software Foundation" />
                </section>
            </manifest>
        </jar>
    </target>
    <!-- javadocs target. Creates the API documentation -->
    <target name="javadocs" depends="prepare"
        description="Creates the API documentation.">
        <mkdir dir="${javadoc.destdir}"/>
        <javadoc packagenames="org.apache.oro.io,org.apache.oro.text,org.apache.oro.text.regex,org.apache.oro.text.awk,org.apache.oro.text.perl,org.apache.oro.util"
            sourcepath="${build.src}"
            destdir="${javadoc.destdir}"
            overview="${build.src}/org/apache/oro/overview.html"
            author="true"
            version="true"
            windowtitle="${name} ${version} API"
            doctitle="${name} ${version} API"
            header="a href='' target=_top><img src='{@docroot}/../images/logoSmall.gif' alt='Jakarta ORO' width=48 height=47 align=center border=0 hspace=1 vspace=1/></a>"
            bottom="Copyright © ${year} Apache Software Foundation. All Rights Reserved."/>
        <replace file="${javadoc.destdir}/overview-frame.html"
            token="{@docroot}" value="."/>
        <replace dir="${javadoc.destdir}" includes="**/*.html"
            token="@version@" value="${version}"/>
    </target>
    <!-- docs target. Creates project web pages and documentation. -->
    <target name="docs" depends="prepare-error,lib,examples"
        description="Creates the project web pages and documentation."
        if="AnakiaTask.present">
        <taskdef name="anakia" classname="org.apache.velocity.anakia.AnakiaTask">
            <classpath>
                <fileset dir="${jakarta-site2.dir}/lib">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </taskdef>
        <anakia basedir="${docs.src}" destdir="${docs.dest}/"
            extension=".html" style="./site.vsl"
            projectFile="stylesheets/project.xml"
            excludes="**/stylesheets/** manual/** empty.xml"
            includes="**/*.xml"
            lastModifiedCheck="true"
            templatePath="${jakarta-site2.dir}/xdocs/stylesheets"/>
        <copy todir="${docs.dest}/images" filtering="no">
            <fileset dir="${docs.src}/images">
                <include name="**/*.gif"/>
                <include name="**/*.jpeg"/>
                <include name="**/*.jpg"/>
            </fileset>
        </copy>
    </target>
    <!-- package target -->
    <target name="package" depends="jar,javadocs,docs"
        description="Creates a distribution directory tree.">
        <mkdir dir="${final.dir}"/>
        <copy todir="${final.dir}/src">
            <fileset dir="${code.src}"/>
        </copy>
        <!-- BEGIN_REMOVE_THIS -->
        <!-- Remove this when there's a first draft of the manual. -->
        <copy todir="${final.dir}/docs">
            <fileset dir="${docs.dest}"
                exclude name="manual/**"/>
        </copy>
        <!-- END_REMOVE_THIS -->
        <copy file="${top.dir}/build.xml" tofile="${final.dir}/build.xml"/>
        <copy file="${top.dir}/build.properties"
            tofile="${final.dir}/build.properties"/>
        <copy file="${top.dir}/LICENSE" tofile="${final.dir}/LICENSE"/>
        <copy file="${top.dir}/ISSUES" tofile="${final.dir}/ISSUES"/>
        <copy file="${top.dir}/CHANGES" tofile="${final.dir}/CHANGES"/>
        <copy file="${top.dir}/COMPILE" tofile="${final.dir}/COMPILE"/>
        <copy file="${top.dir}/CONTRIBUTORS"
            tofile="${final.dir}/CONTRIBUTORS"/>
        <copy file="${top.dir}/README" tofile="${final.dir}/README"/>
        <copy file="${top.dir}/STYLE" tofile="${final.dir}/STYLE"/>
        <copy file="${top.dir}/TODO" tofile="${final.dir}/TODO"/>
        <copy file="${top.dir}/${final.name}.jar" tofile="${final.dir}/${final.name}.jar"/>
    </target>
    <!-- package-zip target. Packages the distribution with ZIP -->
    <target name="package-zip" depends="package"
        description="Packages the distribution as a zip file.">
        <zip zipfile="${top.dir}/${final.name}.zip" basedir="${top.dir}/"
            includes="**/${final.name}/**" excludes="**/.cvsignore"/>
    </target>
    <!-- Packages the distribution with TAR-GZIP -->
    <target name="package-tgz" depends="package"
        description="Packages the distribution as a gzipped tar file.">
        <tar tarfile="${top.dir}/${final.name}.tar"
            basedir="${top.dir}" excludes="**/**">
            <tarfileset dir="${final.dir}/.."
                include name="${final.name}/**"
                exclude name="**/.cvsignore"/>
        </tar>
        <gzip zipfile="${top.dir}/${project}-${version}.tar.gz" src="${top.dir}/${project}-${version}.tar"/>
    </target>
    <!-- Packages the distribution with ZIP and TAG-GZIP -->
    <target name="package-all" depends="package-zip, package-tgz"/>
    <!-- Makes an attempt to clean up a little. -->
    <target name="clean"
        description="Removes generated artifacts from source tree.">
        <delete dir="${build.dest}"/>
        <delete dir="${javadoc.destdir}"/>
        <delete dir="${final.dir}"/>
        <delete file="${top.dir}/${final.name}.jar"/>
        <delete file="${top.dir}/${final.name}.tar"/>
        <delete file="${top.dir}/${final.name}.tar.gz"/>
        <delete file="${top.dir}/${final.name}.zip"/>
        <delete>
            <fileset dir="${top.dir}" includes="velocity.log*"/>
        </delete>
    </target>
</project>

build.xml文件的编写可参见ant发行版中的使用文档 你也可以自己编写脚本打包,使用jdk发行版中的jar命令,例如:

#jar cmf myManifestFile myFile.jar *.class

注意还需要自己编写MANIFEST.MF文件配置包属性。具体可以参见javadoc 当然还可以使用集成开发环境提供的打包工具,如JBuilder提供打包工具,但这样程序的移植性不强,建议不要使用,就使用ant比较好。

怎么把java文件打包

  1. 右键要导出的项目,选择Export
  2. 选择Java -- JAR file
  3. 选择要打包的文件,和存放路径,点击Finish