在jar中包含库 [英] Including libraries in jar

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

问题描述

好的,这就是我们所拥有的:

Ok, so here's what we've got:

我们有两个库包,我们已经编译成罐子。

We've got two library packages, which we've compiled into jars.


package starwars; 
public class JarJar {

    public void JarSayHello()
    {
        System.out.println("Jaaaaar!!"); 
    }

}




package barwars; 
public class BarBar {

    public void BarSayHello()
    {
        System.out.println("Baaaaa!!"); 
    }

}

我们用


javac -d bin -sourcepath src src/barwars/BarBar.java
jar cvf barwars.jar -C bin . 


javac -d bin -sourcepath src src/starwars/JarJar.java
jar cvf starwars.jar -C bin . 

我们很好地进入罐子里。

All nicely into jars for us.

现在我们要包括这些两个罐进入另一个java项目。

Now we want to include these two jars into another java project.

所以我们已经


  • / project / src / a_pack / HelloWorld.java

  • /project/libs/starwars.jar

  • /project/libs/barwars.jar

  • /project/manifest.txt

  • /project/src/a_pack/HelloWorld.java
  • /project/libs/starwars.jar
  • /project/libs/barwars.jar
  • /project/manifest.txt

package a_pack;
import starwars.JarJar; 
import barwars.BarBar; 

public class HelloWorld {

    public static void main(String[] args) {    
        System.out.println("Hello, World");         
        JarJar myJar = new JarJar();
        myJar.JarSayHello();        
        BarBar myBar = new BarBar(); 
        myBar.BarSayHello(); 
    }

}

Manifest.txt

Manifest.txt


Main-Class: a_pack.HelloWorld
Class-Path: libs/starwars.jar libs/barwars.jar

现在我们用以下代码编译:

Now we compile this with:


javac -d bin -sourcepath src -cp "libs/starwars.jar;libs/*" src/a_pack/HelloWorld.java 
jar cvfm helloworld.jar manifest.txt -C bin . 

这个编译运行正常。

现在我有两个问题。

首先 - 如果我将此jar文件移动到其他地方,并尝试运行它,那么我将得到:

Firstly - if I move this jar file to somewhere else, and try run it, then I'll get:

线程main中的异常java.lang.NoClassDefFoundError:starwars / JarJar

现在我可以修复这通过将libs文件夹移动到我移动jar的地方。但这让我觉得很乱(如果那个位置已经有一个libs文件夹怎么办?)。

Now I can fix this by moving the libs folder to wherever I move the jar. But this strikes me as messy (what if there is already a libs folder in that location?).

理想情况下,我想做的是包括引用的jar在jar里面,所以有一个jar包含在其自身内部运行所需的所有内容。

Ideally what I'd like to do, is include the referenced jars inside the jar, so there's one jar that contains everything that's required to run inside itself.

这可能吗? (这是好习惯吗?)

Is this possible? (And is it good practise?)

推荐答案

可能,是的。好的做法,没有。

Possible, yes. Good practice, no.

罐子只是拉链文件,所以你可以解压缩并重新压缩到你内心的内容。当项目变大时,更大的问题是管理所有这些单独的jar。

Jars are just zip files, so you can unzip and rezip to your heart's content. The bigger problem is managing all of these separate jars as your project gets larger.

大多数项目不使用命令行进行编译。相反,IDE会让您的罐子保持最新状态。大多数现代Java项目使用Maven或Ivy将jar放入存储库并根据需要将它们删除。

Most projects do not compile using the command line. Instead, an IDE keeps your jars up to date. And most modern Java projects use Maven or Ivy to place jars in a repository and fish them out as needed.

查看Eclipse的Eclipse,Netbeans或Intellij。然后看看Maven构建你的项目。

Look at Eclipse, Netbeans, or Intellij for IDEs. And look into Maven for structuring your project.

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

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