简体   繁体   English

如何在jar文件中执行jar文件

[英]How to execute a jar file within a jar file

I have a project who's structure cannot be changed. 我有一个项目,其结构无法更改。 What i have to do is that 我要做的是

I have a main jar file that have directores 我有一个有导演的主要jar文件

/classes
/lib
/main
/manifest

I have to call a Test1 class when i execute the main jar file. 执行主jar文件时,我必须调用Test1类。 so in the manifest file i gave the path of the Main-Class : path.to.file. 所以在清单文件中,我给出了Main-Class的路径:path.to.file。

I have another jar file inside the main folder that contains the actual application. 我在包含实际应用程序的main文件夹中还有另一个jar文件。 What can i do to execute the jar in main folder and execute its Test2 class mentioned in the Manifest file. 我该怎么做才能执行主文件夹中的jar并执行清单文件中提到的Test2类。

Please give me a solution or any direction to work on. 请给我一个解决方案或任何指导。 Any help appreciated. 任何帮助表示赞赏。

You can not do this in the current ClassLoader, you need a new one. 您不能在当前的ClassLoader中执行此操作,需要一个新的。

If your jar named foobar.jar and you have a main -method in the class test.abc.Main this may work: 如果您的jar名为foobar.jar并且在test.abc.Main类中具有main -method,则此方法可能有效:

    URL[] urls={new URL("classloader:/main/foobar.jar")};
    ClassLoader parentClassLoader = this.getClass().getClassLoader();
    URLClassLoader childClassLoader=new URLClassLoader(urls,parentClassLoader);
    Class clazz = childClassLoader.loadClass("test.abc.Main");
    String[] arguments = new String[]{};
    clazz.getMethod("main", arguments.getClass()).invoke(null,arguments);
  1. You could use JarClassLoader http://www.jdotsoft.com/JarClassLoader.php if you're okay with using GPLv3'd code to make a launcher class. 如果可以使用GPLv3的代码创建启动器类,则可以使用JarClassLoader http://www.jdotsoft.com/JarClassLoader.php

  2. Extract the jars inside of the main jar to a temporary location, execute and delete upon exit 将主罐中的罐提取到临时位置,退出后执行并删除

  3. Simply change the file structure, since you haven't listed any reasons for "not doing that", I don't see why not. 只需更改文件结构,因为您没有列出“不这样做”的任何原因,所以我不明白为什么不这样做。 :D :d

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

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