站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题用java API制作可执行JAR文件

用java API制作可执行JAR文件

摘要: 用java API制作可执行JAR文件

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="272" height="86" align="center" valign="top"> </td> <td width="412" valign="top">

作者:Shawn Silverman 翻译:Sean
本文阐述了如何把一个不可执行的JAVA Archive(JAR)文件变成可执行,而不用直接操作manifest文件。你会学到写出短小的一个程序,通过运行
java -jar
命令或在像windows一样的操作系统里面用双击鼠标运行任何JAR文件。

你可以很容易地把应用程序的一整套class文件和资源文件打包到一个JAR中。事实上这就是jar文件存在的一个目的。另外一个目的就是让用户能很容易地执行被打包到jar文件里面的应用程序。那么为什么jar文件仅仅作为文件在整个java里面占据了次要的地位,而本地执行则被忽视?

要执行一个jar文件,你可以使用java命令的-jar选项。举一个例子来说,假如你有个名叫myjar.jar的文件。这个jar是可以运行的,你可以运行它:
java -jar myjar.jar

另外一个办法就是,当Java Runtime Environment(JRE)已经被安装到一个像windows的操作系统上,将jar文件与JVM关联(关联java.exe跟jar文件)在一起你就可以通过双击jar来运行这个应用程序。当然,jar文件必须是可执行的。

</td> </tr> </table>

现在的问题是:如何做一个可以执行的jar?

manifest文件以及Main-class入口
在大多数jar中,都在一个叫META-INF的目录里面保存了一个叫MANIFEST.MF的文件。那个文件里面,
包含了一个特殊表项名字叫Main-Class,告诉java -jar命令应该执行哪个class.

问题是你必须为manifest文件手工加入适当表项,而且必须在一定的位置和用一定的格式。不幸的是,不是每个人都喜欢打开写字板编辑配置文件。

让API帮你完成任务
自从java1.2发布以来,一个叫java.uil.jar包的出现,让你能够方便处理jar文件。
(注意:该包基于java.util.zip)特别地,jar包让你通过Mainfest类,可以容易操作
那些manifest文件.

就让我们用这个API写一个程序吧。首先,这个程序必须知道三样东西:
1。我们要使之可运行的jar文件。
2。运行jar的主类(这个类必须包含在jar中)。
3。输出新jar文件的文件名,因为我们不能简单地覆盖原来的文件。

编写程序
上面列表的三点要求将组成我们的程序的参数。现在,让我们为这个程序选择一个适当的名字。
MakeJarRunnable听起来觉得怎样?

为main方法检查参数
假设我们的main方法入口点是一个标准的main(String[])方法。我们应该这样检查程序的参数:
if (args.length != 3) {
System.out.println("Usage: MakeJarRunnable "
+ " ");
System.exit(0);
}
请注意参数列表是如何描述的,因为这在以下代码中是很重要的。参数的次序和内容不是固定的;
然而,如果你要改变他们的话,要记住响应修改其他代码。

访问jar和jar的manifest文件
第一,我们必须创建一些了解jar和manifest的对象:
//Create the JarInputStream object, and get its manifest
JarInputStream jarIn = new JarInputStream(new FileInputStream(args[0]));
Manifest manifest = jarIn.getManifest();
if (manifest == null) {
//This will happen if no manifest exists

manifest = new Manifest();
}

设置Main-Class属性
我们把Main-Class入口放到manifest文件的main属性部分。一旦从manifest对象获得这个属性,就可以设置需要的main class。然而,如果main-Class属性已经存在原来的jar当中又如何呢?这里我们只是简单地输出一个警告然后退出。我们能加入一个命令行参数告诉程序使用新的值,而代替了旧的那个:
Attributes a = manifest.getMainAttributes();
String oldMainClass = a.putValue("Main-Class", args[1]);

//If an old value exists, tell the user and exit

if (oldMainClass != null) {
System.out.println("Warning: old Main-Class value is: "+ oldMainClass);
System.exit(1);
}

输出新的JAR
我们需要创建一个新的JAR文件,所以我们必须使用JarOutputStream类。注意:
我们必须确定我们不用跟输入文件相同的名字作为输出文件的名字。还有一个方案就是,
程序应该考虑到一种情况,就是两个jar文件都是相同的,促使用户覆盖原来的文件,如果
他愿意这么做的话。然而,我在保留了这一点,作为读者的一个练习。从如下代码开始:
System.out.println("Writing to " + args[2] + "...");
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(args[2]),manifest);
我们必须从输入JAR写每个表项到输出的JAR,所以迭代每个表项:
//Create a read buffer to transfer data from the input

byte[] buf = new byte[4096];

//Iterate the entries

JarEntry entry;
while ((entry = jarIn.getNextJarEntry()) != null) {
//Exclude the manifest file from the old JAR

if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue;

//Write the entry to the output JAR

jarOut.putNextEntry(entry);
int read;
while ((read = jarIn.read(buf)) != -1) {
jarOut.write(buf, 0, read);
}

jarOut.closeEntry();
}

//Flush and close all the streams

jarOut.flush();
jarOut.close();

jarIn.close();

完成程序
当然,我们必须把这些代码放到一个类的main方法里面,并且需要一大堆import代码。完整程序:

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.jar.*;

/**

 * This utility makes a JAR runnable by inserting a Main-Class

 * attribute into the Manifest.

 *

 * @author Shawn Silverman

 */

public class MakeJarRunnable {

    public static void main(String[] args) throws IOException {

        if (args.length != 3) {

            System.out.println("Usage: MakeJarRunnable "

                               + "  ");

            System.exit(0);

        }

        // Create the JarInputStream object, and get its Manifest

        JarInputStream jarIn = new JarInputStream(new FileInputStream(args[0]));

        Manifest manifest = jarIn.getManifest();

        if (manifest == null) {

            // This will happen if there is no Manifest

            manifest = new Manifest();

        }

        Attributes a = manifest.getMainAttributes();

        String oldMainClass = a.putValue("Main-Class", args[1]);

        // If there was an old value there, tell the user about it and exit

        if (oldMainClass != null) {

            System.out.println("Warning: old Main-Class value is: "

                               + oldMainClass);

            System.exit(1);

        }

        System.out.println("Writing to " + args[2] + "...");

        JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(args[2]),

                                                     manifest);

        // Create a read buffer to be used for transferring data from the input

        byte[] buf = new byte[4096];

        // Iterate the entries

        JarEntry entry;

        while ((entry = jarIn.getNextJarEntry()) != null) {

            // Exclude the Manifest file from the old JAR

            if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue;

            // Write out the entry to the output JAR

            jarOut.putNextEntry(entry);

            int read;

            while ((read = jarIn.read(buf)) != -1) {

                jarOut.write(buf, 0, read);

            }

            jarOut.closeEntry();

        }

        // Flush and close all the streams

        jarOut.flush();

        jarOut.close();

        jarIn.close();

    }

}

程序使用例子
让我们把这个程序应用到一个例子里面来。假设你有一个应用程序,该程序的入口点是一个叫HelloRunnableWorld的 类,再假设你已经创建了一个jar叫myjar.jar,包含了整个程序。运行MakeJarRunnable:

c:\java>java MakeJarRunnable myjar.jar HelloRunnableWorld myjar_r.jar

运行结果:

c:\java>Hello, Runnable World!

正如前面提到的,注意一下我的参数顺序。如果你忘记了顺序,没有参数运行一下程序,它会响应出现一个用法提示信息。

尝试对myjar.jar运行java -jar命令。然后对myjar_r.jar。注意区别不同!好了,你完成了这一切了, 浏览一下每个jar的manifest文件(META-INF/MANIFEST.MF)

</td> </tr> <tr>


↑返回目录
前一篇: JAVA开源的六大帮派
后一篇: 有趣的开窗户游戏