简体   繁体   English

在罐子中执行包装好的罐子文件

[英]execute a wrapped jar file within a jar

I am trying to create a wrapper around a runnable .jar file in order to pass parameters for the JVM. 我试图围绕可运行的.jar文件创建包装器,以传递JVM的参数。 The related code of my wrapper is: 包装器的相关代码是:

public static void main(String[] args) throws IOException
      String[] cmdArray = {"java",
        System.getProperty("os.arch").contains("64") ? "-d64" : "",
        "-XX:+AggressiveHeap",       
        "-jar",
        "/lib/A.jar"  //replace with jar name 
            };
      Runtime.getRuntime().exec(cmdArray);
}

The A.jar file is a referenced library (added in build path and class path) in my Wrapper project. A.jar文件是我的Wrapper项目中的引用库(在构建路径和类路径中添加)。 Example: 例:

 Wrap
   >src
       >org
           >Wrapper.java
   >lib
       >A.jar

When I create the second jar file, from the Wrapper project (lets call it B.jar), its content is 当我从Wrapper项目中创建第二个jar文件时(简称为B.jar),其内容为

lib
   >A.jar
org
   >eclipse
          >jdt
             >... (i.e., all the files for the jarinjarloader)
   >Wrapper.class
META-INF
       >MANIFEST.MF

My manifest file from the second jar looks like this: 我在第二个jar中的清单文件如下所示:

Manifest-Version: 1.0
Rsrc-Class-Path: lib/A.jar
Class-Path: .
Rsrc-Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Main-Class: org.Wrapper

When I launch the B.jar file, it shows me that the process started, but A.jar file is not called/launched. 当我启动B.jar文件时,它表明进程已启动,但未调用/启动A.jar文件。 No error, no exception. 没有错误,也不例外。 The process starts and terminates. 该过程开始并终止。 Tried to launch it via command line and double-click also. 试图通过命令行启动它,也双击。

If I modify the MANIFEST.MF file to 如果我将MANIFEST.MF文件修改为

Rsrc-Class-Path: A.jar

and the element from cmdArray in the main method of the Wrapper.java from 和cmdArray中Wrapper.java的main方法中的元素

"/lib/A.jar" to "A.jar"

and I make a copy of A.jar in the folder where my wrapper jar is present (eg, B.jar), it launches perfectly, calling the second jar. 然后在我的包装jar所在的文件夹(例如B.jar)中复制A.jar,它将完美启动,调用第二个jar。 What am I doing wrong? 我究竟做错了什么? How am I suppose to pass the class path to an inner jar file? 我应该如何将类路径传递给内部jar文件? Any help would be appreciated. 任何帮助,将不胜感激。

While this is not strictly speaking an answer to your question, I think I can propose you a better approach in solving your original problem (ie adding args to jvm). 虽然这并不是严格意义上的问题答案,但我认为我可以为您提出一种更好的方法来解决您的原始问题(即在jvm中添加args)。

This article discusses how to restart java app preserving jvm, command line and other launch options. 本文讨论如何重新启动保留jvm,命令行和其他启动选项的Java应用程序。

Why this might be interesting to you? 为什么这对您可能很有趣? Well, tweaking it a bit, what you can do is to get rid of adding wrapper around you main jar and do something like this in your main: 好吧,对其进行一些调整,您可以做的是摆脱在主jar周围添加包装器的操作,并在主main中执行以下操作:

  1. check for presence of some command line argument that tells that app is runned with tweaked vm options and: 检查是否存在某些命令行参数,该参数表明应用程序是通过调整后的vm选项运行的,并且:
  2. if present - let the app to do it's work 如果有的话-让应用执行它的工作
  3. in not - restart application, tweaking jvm options as well as passing command line argument referred in step 1 在非重新启动应用程序中,调整jvm选项以及传递步骤1中引用的命令行参数

I've done something like this in one of my past projects and it helped to ease build/packaging and debugging compared to wrapped jar approach. 我在过去的一个项目中做了类似的事情,与包装的jar方法相比,它有助于简化构建/打包和调试。

Hope this will be helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM