简体   繁体   中英

How to prevent jar-in-jar-loader loading specific class?

Imagine I have a following project structure of folders

  • A
  • B
  • C

and many external jars.

Now I am trying to build two different executable-jars like this A+B, A+C.

At first, I do Eclipse -> Export -> Executable Jar and save script for Ant. So it generates Ant-script and jar-in-jar-loader. Now I modify ant script to exclude folder B

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

and keep jar-in-jar-loader.zip file unmodified.

After building the jar running gives this error:

    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.

It seems like JarRscrLoader tries to load all listed classes at application startup. How can I prevent that?

Please, don't suggest me to split the code at Eclipse into 2-3 projects.

Create two different Run configurations (Run --> Run configurations...) and modify the included libraries in the classpath tab according to your requirements.

When you export the project, just select the appropriate configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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