如何从另一个jar运行jar文件 [英] How to run a jar file from another jar

查看:239
本文介绍了如何从另一个jar运行jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jar文件已经变成一个.exe使用L4J,另一个jar文件在appdata。有两个文件的原因是我需要一个更新机制。



我的问题:
如何在桌面上运行.exe文件,然后从appdata中加载jar?

解决方案

您可以使用 URLClassLoader 在运行时加载第二个JAR。



根据您的需要,您可能需要一个桥接界面(一个存在于两个Jars中),您可以从exe中调用该界面,以获得第二个Jar运行,或者您可以使用第二个Jar的主要方法;)



另一个选择是运行另一个JVM。



更新



为了物理分离应用程序的两个元素。你有一个包装在一个EXE(又名发射器)和另一个罐子,这是你的应用程序(也就是应用程序)(我假设)。



所以。您的启动器应该完全不了解您的应用程序(很少到没有编译时依赖)。



有些如何,我们需要从启动器动态加载应用程序。为了做到这一点,我们需要一些东西。



我们需要能够将应用程序加载到启动器类加载器上下文中(所以我们可以看到它),我们有一些我们可以加载应用程序。



动态ClassLoading



只需使用 URLClassLoader

  URLClassLoader loader = new URLClassLoader (new URL [] {new File(path / to / your / jar / Application.jar)。toURI()。toURL()}); 

申请加载



这可以通过两种方式之一实现。您可以使用 URLClassLoader 来查找启动应用程序 main class ...

  //这跟说
// the.package.name.to.you.main.class.Main.main( new String [] {});
Class<?> mainClass = loader.loadClass(the.package.name.to.you.main.class.Main);
方法mainMethod = mainClass.getMethod(main,String []。class);
mainMethod.invoke(null,new String [] {});

现在,如果您的应用程序Jar没有 main 方法,您可以使用上面的例子来启动任何你想要的类...


I have a jar file that has been turned into a .exe using L4J, and another jar file in appdata. The reason for having two files is that I need an updating mechanism.

My question: How do I run the .exe file on the desktop, then load the jar in appdata from it?

解决方案

You could use a URLClassLoader to load the second Jar at runtime.

Depending on your needs, you may need a bridging interface (one that exists in both Jars) that you would call from your 'exe' to get the second Jar running...or you could simply use the second Jar's main method ;)

The other choice you have is to run another JVM.

UPDATE

In order to physical seperate the two elements of your application. You have a Jar wrapped in a EXE (aka launcher) and another Jar which is your application (aka application) (I assume).

So. Your launcher should have absolutely no idea about your application (little to no compile time dependencies).

Some how, we need to dynamically load the application from the launcher. To do that, we need a few things.

We need to be able to load the application into the launchers class loader context (so we can see it) and we some we to be able to load the application.

Dynamic ClassLoading

This can be achieved simply through the use of URLClassLoader

URLClassLoader loader = new URLClassLoader(new URL[]{new File("path/to/your/jar/Application.jar").toURI().toURL()});

Application Loading

This can be achieved in one of two ways. You could simply use the URLClassLoader to find a launch the applications main class...

// This is essentially the same as saying 
// the.package.name.to.you.main.class.Main.main(new String[]{});
Class<?> mainClass = loader.loadClass("the.package.name.to.you.main.class.Main");
Method mainMethod = mainClass.getMethod("main", String[].class);
mainMethod.invoke(null, new String[]{});

Now, if your application Jar doesn't have a main method, you can use the above example to launch just about any class you want...

这篇关于如何从另一个jar运行jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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