Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using slf4j with plugins --> CL problems?! #77

Closed
tuxedo0801 opened this issue Oct 3, 2015 · 1 comment
Closed

using slf4j with plugins --> CL problems?! #77

tuxedo0801 opened this issue Oct 3, 2015 · 1 comment

Comments

@tuxedo0801
Copy link

Hey there,

My plugins use slf4j ... Of course, the main application also use slf4j.

The main application uses the "slf4j-jdk14" logger.

If my plugins not just contain slf4j-api, but also sld4f-jdk14, I get an kind of warning:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/......../.m2/repository/org/slf4j/slf4j-jdk14/1.7.12/slf4j-jdk14-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/......../logicplugin/target/lib/slf4j-jdk14-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]

Makes sense...(but applications works...) --> Having the jdk14-lib two times is confusing. So, If I exclude the slf4j-jdk14 from my plugin (but keep in main application), I get this (and applications does not work):

2015-10-03 11:47:20.700 SEVERE [main] ro.fortsoft.pf4j.DefaultPluginFactory.create: null
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at ro.fortsoft.pf4j.DefaultPluginFactory.create(DefaultPluginFactory.java:61)
    at ro.fortsoft.pf4j.PluginWrapper.getPlugin(PluginWrapper.java:63)
    at ro.fortsoft.pf4j.DefaultPluginManager.startPlugins(DefaultPluginManager.java:226)
    at de.root1.kad.KadMain.<init>(KadMain.java:45)
    at de.root1.kad.KadMain.main(KadMain.java:60)
Caused by: java.lang.LinkageError: loader constraint violation: when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;" the class loader (instance of ro/fortsoft/pf4j/PluginClassLoader) of the current class, org/slf4j/LoggerFactory, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for the method's defining class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:335)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:283)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:304)
    at de.root1.kad.logicplugin.LogicPlugin.<init>(LogicPlugin.java:56)
    ... 9 more

Exception in thread "main" java.lang.NullPointerException
    at ro.fortsoft.pf4j.DefaultPluginManager.startPlugins(DefaultPluginManager.java:226)
    at de.root1.kad.KadMain.<init>(KadMain.java:45)
    at de.root1.kad.KadMain.main(KadMain.java:60)

Anyone else facing this issue?

I've not yet digged into the root-cause, but it seems that this is a classloader issue with pf4j plugin classloader?!

any ideas before I investige time to find the cause?

br,
Alex

@tuxedo0801
Copy link
Author

Issue solved:

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
            <scope>provided</scope>
        </dependency>

The trick is the "provided" scope. I also had to exclude the lib from being copied to target/lib for development mode:

            <!-- Put dependencies to target/lib/ folder, so that we can depend on during development runtime -->
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <id>copy libs</id>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <excludeScope>provided</excludeScope>
                       </configuration>
                    </execution>
                </executions>
            </plugin>

The maven-dependency-plugin part is again special and for my case only. But maybe it helps others too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant