【问题标题】:Execute JAR from lib directory从 lib 目录执行 JAR
【发布时间】:2015-02-18 20:39:55
【问题描述】:

我有具有以下结构的“SomeJarA.jar”:

SomeJarA
|
|--lib/SomeJarB.jar
|--com/.../SomeClass.class

我试图在“SomeClass.java”中执行“SomeJarB.jar”,使用:

Runtime.getRuntime().exec("java -jar SomeJarB.jar");

这可能吗?

【问题讨论】:

    标签: java maven jar


    【解决方案1】:

    据我所知,如果有办法实现这一点,那将是一个棘手且高度不受支持的安排。我会四处寻找一些支持文档,但我倾向于说“不”。

    不过,这个相关的 link 可能会有所帮助。

    编辑:找到我想要的参考。这是here。具体来说,有一条注释说:“Class-Path 标头指向本地网络上的类或 JAR 文件,不是 JAR 文件中的 JAR 文件或可通过以下方式访问的类互联网协议。”

    【讨论】:

      【解决方案2】:

      这是来自用户@Tezar 和问题Classpath including JAR within a JAR

      You can use "Class-Path" in your manifest file.
      
      For example:
      
      Create manifest file MANIFEST.MF
      
          Manifest-Version: 1.0
          Created-By: Bundle
          Class-Path: ./custom_lib.jar
          Main-Class: YourMainClass
      
      Compile all your classes and run
      jar cfm Testing.jar MANIFEST.MF *.class custom_lib.jar
      
      c stands for create archive f indicates that you want to specify file v
      is for verbose input m means that we will pass custom manifest file
      
      Be sure that you included lib in jar package. You should be able to run
      jar in the normal way.
      
      based on: http://www.ibm.com/developerworks/library/j-5things6/
      

      查看链接的问题。这似乎是 jars in jars 最明智的方法,并且避免了第三方类加载器。如果您想投票,请转到 Tezar 的答案并投票该条目,而不是这个条目,因为这只是一个副本。

      【讨论】:

        猜你喜欢
        • 2011-07-25
        • 2016-12-13
        • 1970-01-01
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        • 2015-04-22
        相关资源
        最近更新 更多