创建包含其他库文件的 JAR 文件 [英] Creating a JAR file which contains other library files

查看:24
本文介绍了创建包含其他库文件的 JAR 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含其他 JAR 库的可执行 JAR 文件.但是当我执行JAR时,它给出了类路径问题的错误.

I want to create one executable JAR file which contains other JAR libraries. But when I execute the JAR, it gives an error of class path problem.

如何解决类路径问题?

推荐答案

我觉得你可以这样试试;

I think you can try it like this;

这里有一个简单的例子来回答你的问题.首先,我们假设我们有一个像 D:javademo 这样的项目目录.在这个工作目录中,我们然后创建一个主类 HelloWorld.java 并且它包含我们的其他 JAR 文件,如 commons-lang.jar.现在,我们必须将我们的主要类 HelloWorld 和 commons-lang.jar 归档到 test.jar 文件中.

Here is a simple example for you question. First, we assume we have a project directory like D:javademo. In this working directory we then create a main class HelloWorld.java and thtat contains our other JAR files, like commons-lang.jar. Now, we must archive our main classes HelloWorld and commons-lang.jar into test.jar file.

首先我们必须编辑我们的清单文件,以便我们可以指定我们的类路径和主类像这样:

First we must edit our manifest file so that we can specify our class-path and main-class like this:

Manifest-Version: 1.0 
Created-By: tony example
Class-Path: test.jar commons-lang.jar
Main-Class: org.tony.java.HelloWorld

我们将此文件命名为 test.mf.现在我们使用 jar 命令来生成我们的 JAR 文件,如下所示:

We named this file test.mf. Now we use the jar command to generate our JAR file like this:

jar -cvfm test.jar test.mf -C ./ .

然后它会生成JAR文件test.jar.您可以使用此命令使用 java 命令运行此主类:

Then it will generate the JAR file test.jar. You can use this command to run this main class using java command:

java -jar test.jar

这就是我的解决方案.我希望它能给你一些有用的东西...

That is my solution. I hope it give you something helpful...

这篇关于创建包含其他库文件的 JAR 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆