简体   繁体   English

如何防止jar-in-jar-loader加载特定类?

[英]How to prevent jar-in-jar-loader loading specific class?

Imagine I have a following project structure of folders 想象一下,我有一个文件夹的以下项目结构

  • A 一种
  • B
  • C C

and many external jars. 和许多外部罐子。

Now I am trying to build two different executable-jars like this A+B, A+C. 现在,我正在尝试构建两个不同的可执行jar,例如A + B,A + C。

At first, I do Eclipse -> Export -> Executable Jar and save script for Ant. 首先,我执行Eclipse->导出->可执行Jar并保存Ant脚本。 So it generates Ant-script and jar-in-jar-loader. 因此,它将生成Ant脚本和jar-in-jar-loader。 Now I modify ant script to exclude folder B 现在,我修改ant脚本以排除文件夹B

    <fileset dir="C:/Users/Nikolay/bin">
        <exclude name="org/B"/>
    </fileset>

and keep jar-in-jar-loader.zip file unmodified. 并保持jar-in-jar-loader.zip文件jar-in-jar-loader.zip

After building the jar running gives this error: 构建完jar之后,运行会出现此错误:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/B
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassNotFoundException: org.B
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 3 more

But I am sure B class is not used in the A. 但我确定A中不会使用B类。

It seems like JarRscrLoader tries to load all listed classes at application startup. JarRscrLoader似乎在应用程序启动时尝试加载所有列出的类。 How can I prevent that? 我该如何预防?

Please, don't suggest me to split the code at Eclipse into 2-3 projects. 请不要建议我将Eclipse的代码分成2-3个项目。

Create two different Run configurations (Run --> Run configurations...) and modify the included libraries in the classpath tab according to your requirements. 创建两个不同的运行配置(运行->运行配置...),然后根据需要修改classpath选项卡中包含的库。

When you export the project, just select the appropriate configuration. 导出项目时,只需选择适当的配置。

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

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