简体   繁体   English

在运行时将jar文件下载并添加到classpath而不进行反射

[英]Downloading and adding jar files to classpath at runtime without reflection

We have a standalone java-application. 我们有一个独立的Java应用程序。 This application has lots of functionality, but most customers does only use a small bit of this functionality. 该应用程序具有很多功能,但是大多数客户只使用其中的一小部分。 We want to distribute the application with just the "normal" minimum of third-party jar-files. 我们只希望使用最少“正常”的第三方jar文件来分发应用程序。 In the eclipse project all external jar-files are in the class path. 在eclipse项目中,所有外部jar文件都在类路径中。 So no reflection is used to instanciate classes. 因此,无需使用反射来实例化类。 So when a user are going to use a more advanced function, eg taking webcamera pictures, we will then download all the javacv jar files and then somehow add these to the classpath/classloader before calling our class that takes a picture. 因此,当用户要使用更高级的功能(例如,拍摄网络摄像头照片)时,我们将下载所有javacv jar文件,然后以某种方式将这些文件添加到classpath / classloader中,然后再调用需要拍照的类。 Is this possible? 这可能吗? I allready have a working solution for a very small module that downloads some jar-files on the fly and adds them in a custom classloader, but I then have to painfully instanciate every class and call every method by reflection. 我已经为一个非常小的模块准备了一个可行的解决方案,该模块可以动态下载一些jar文件并将其添加到自定义的类加载器中,但是我不得不痛苦地实例化每个类并通过反射来调用每个方法。 Eg: 例如:

URLClassLoader loader = new URLClassLoader(urls,w.getClass().getClassLoader());
Class grabberClass = Class.forName("org.bytedeco.javacv.FrameGrabber",true,loader);
Object grabber = grabberClass.getMethod("createDefault", int.class).invoke(null, 0);
grabberClass.getMethod("start", null).invoke(grabber, null);
//And so on...

This is not what we want. 这不是我们想要的。 All our source code is just fine, and I don't want to obfuscate it by turning it into reflection-calls. 我们所有的源代码都很好,我不想通过将其转换为反射调用来对其进行混淆。 But how may I use my old code and still be able to add the jar-files at runtime? 但是,如何使用我的旧代码并且仍然能够在运行时添加jar文件?

There is no need for reflection. 无需反思。 Try running your application without the javacv jar files in the classpath. 尝试在类路径中没有javacv jar文件的情况下运行应用程序。 If you never touch the webcam functions, and if you haven't coded your application in a monolithic manner, you should not have any problems, because Java doesn't try to load a class unless and until some other class refers to it in code (or inherits from it, or uses it in a method signature or as a field type). 如果您从未接触过网络摄像头功能,并且还没有以整体的方式编写应用程序,那么您应该不会有任何问题,因为Java不会尝试加载类,除非并且直到其他类在代码中引用了它为止(或继承自它,或在方法签名中或作为字段类型使用它)。

As for downloading the .jar files on demand, consider using Java Web Start and making them lazy components in a .jnlp file: 至于按需下载.jar文件,请考虑使用Java Web Start并使它们成为.jnlp文件中的惰性组件:

<!DOCTYPE jnlp PUBLIC "-//Sun Microsystems, Inc//DTD JNLP Descriptor 6.0.10//EN"
                      "http://java.sun.com/dtd/JNLP-6.0.10.dtd">
<jnlp version="1.6"
      codebase="http://www.example.com/ourstandaloneapp/"
      href="OurStandaloneApp.jnlp">
    <information>
        <title>Our Stand-alone App</title>
        <vendor>Example, Inc.</vendor>
        <homepage href="http://www.example.com/ourstandaloneapp/"/>
        <description>A stand-alone application with many capabilities.</description>
        <icon kind="default" href="standalone.gif"/>
        <offline-allowed/>
    </information>

    <security>
        <all-permissions/>
    </security>

    <resources>
        <j2se version="1.7+"/>
        <jar href="OurStandaloneApp.jar" main="true"/>
        <jar href="http://search.maven.org/remotecontent?filepath=org/bytedeco/javacv/0.10/javacv-0.10.jar"
             download="lazy"/>
    </resources>

    <application-desc/>
</jnlp>

A Web Start application doesn't have to be downloaded/installed from the web. 无需从Web下载/安装Web Start应用程序。 You can actually package it on a CD or flash drive, with a shortcut or script on the installation media that executes javaws -codebase file:/path/to/media -import OurStandAloneApp.jnlp . 实际上,您可以使用执行javaws -codebase file:/path/to/media -import OurStandAloneApp.jnlp的安装介质上的快捷方式或脚本,将其打包到CD或闪存驱动器上。 See here for details. 有关详细信息,请参见此处 (Why that page was removed for Java 6 and later, I don't know. It's pretty valuable.) (为什么不为Java 6和更高版本删除该页面,所以它很有价值。)

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

相关问题 将.jar文件添加到classpath - Adding .jar files to classpath 在运行时使用反射将文件添加到类路径的危险 - Dangers of using reflection to add files to classpath at runtime 将多个jar文件添加到类路径 - Adding multiple jar files into classpath 在Java中运行时下载JAR文件 - Downloading JAR files at runtime in Java Java反思:如何在不将另一个项目或JAR添加到我的类路径的情况下从另一个项目加载一个类? - Java reflection: How can I load a class from another project without adding that project or JAR to my classpath? 将JAR文件添加到测试类路径: - Adding JAR files to your testing classpath: 将绝对路径中可用的jar文件添加到类路径中 - Adding jar files available in absolute path into classpath 即使将jar文件包含在类路径中,也无法在运行时找到类 - Cannot find class on runtime even after including the jar files in the classpath flutter - 错误:类路径中的某些运行时 JAR 文件具有不兼容的版本。 考虑将它们从类路径中移除 - flutter - ERROR: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath 在运行时动态添加jar文件时出现NoClassDefFoundError - NoClassDefFoundError when dynamically adding jar files at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM