我可以使用类路径来覆盖正在运行的 jar 中的文件吗? [英] Can I use the classpath to override a file in a jar that is being run?

查看:24
本文介绍了我可以使用类路径来覆盖正在运行的 jar 中的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JAR 文件,其中包含一个应用程序以及该应用程序的配置文件.应用程序从类路径加载配置文件(使用 ClassLoader.getResource()),并使用烘焙到 JAR 文件中的配置文件完全满足其依赖关系.

I have a JAR file that contains an application as well as configuration files for that application. The application loads configuration files from the classpath (using ClassLoader.getResource()), and has its dependencies completely satisfied using the configuration files baked into the JAR file.

有时我希望应用程序以稍微不同的配置运行(特别是我想覆盖 JDBC URL 以指向不同的数据库),所以我创建了一个新的配置文件,将它存储在正确的目录结构中(即表示在类路径条目的目录 /config 中),我想做这样的事情:

On occasion I want the application to be run with a slightly different configuration (specifically I want to override the JDBC URL to point to a different database) so I create a new configuration file, store it in the correct directory structure (which means in a directory /config of a classpath entry), and I want to do something like this:

java -cp new-config:. -jar application.jar

但我无法让类路径在应用程序 JAR 的内容之前具有 new-config 路径条目.JAR 的内容始终是类路径上的第一件事是否是硬编码的?

But I can't get the classpath to have the new-config path entry before the application JAR's contents. Is it hard-coded that the JAR's content is always the first thing on the classpath?

推荐答案

为什么不直接调用应用程序而不指定 -jar 而是明确命名应用程序主类?这将允许您按照所需的顺序将 new-config 和 application.jar 放在类路径上:

Why not just invoke the application without specifying -jar and instead name the application main class explicitly? This will allow you to put both your new-config and the application.jar on the classpath in the required order:

例如(假设new-config"是一个包含被覆盖的属性文件的目录)

e.g. (assuming "new-config" is a directory containing the overridden properties file)

java -cp new-config:application.jar Application.Main.Class

我相信可以在 jar 内的 MANIFEST.MF 文件中找到主类的名称....

I believe the name of main class can be found in the MANIFEST.MF file inside the jar ....

这篇关于我可以使用类路径来覆盖正在运行的 jar 中的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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