OSGi的类加载问题 [英] Classloading issue with OSGi

查看:111
本文介绍了OSGi的类加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了Maven回购中的log4j-1.2.17捆绑软件. 我尝试在捆绑软件中执行此代码(我的捆绑软件调用了log4j-1.2.17捆绑软件)

    PropertyConfigurator.configure(props());
private static Properties props() {
    Properties props = new Properties();
    props.put("log4j.rootLogger", "INFO, R");
    props.put("log4j.appender.R",
            "org.apache.log4j.DailyRollingFileAppender");
    props.put("log4j.appender.R.File", "logs/IhtikaClient.log");
    props.put("log4j.appender.R.Append", "true");
    props.put("log4j.appender.R.Threshold", "INFO");
    props.put("log4j.appender.R.DatePattern", "'.'yyyy-MM-dd");
    props.put("log4j.appender.R.layout", "org.apache.log4j.PatternLayout");
    props.put("log4j.appender.R.layout.ConversionPattern",
            //"%d{HH:mm:ss,SSS} %c - %m%n");
            //"[%5p] %d{yyyy-MM-dd mm:ss} (%F:%M:%L)%n%m%n%n");
            "[%5p] %d{yyyy-MM-dd mm:ss} %c (%F:%M:%L)%n%m%n");
    return props;
}

但是我有错误

log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [org.apache.felix.framework.BundleWiringImpl@9da1dd] whereas object of type log4j:ERROR "org.apache.log4j.DailyRollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@4b222f]. log4j:ERROR Could not instantiate appender named "R". log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [org.apache.felix.framework.BundleWiringImpl@9da1dd] whereas object of type log4j:ERROR "org.apache.log4j.DailyRollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@4b222f]. log4j:ERROR Could not instantiate appender named "R".

捆绑包已按顺序加载

.getBundleContext().installBundle("......../log4j-1.2.17.jar")
.getBundleContext().installBundle("......../I_MainForm-1.0-SNAPSHOT.jar")

如何解决此错误? 拜托,对不起我的英语. 最好的问候,亚瑟.

解决方案

由于您的Log4j类似乎已由sun.misc类加载器加载,我认为有些东西是从OSGi外部提供的log4j类.有Log4J条目解释了为什么它不能加载Log4J类的事实,这意味着Log4J的某些版本已经存在.

您是否正在使用Felix的定制版本?如果是这样,您能找到带有org.osgi.framework.system.packages=...条目还是org.osgi.framework.system.packages.extra=...条目的配置文件吗?

如果仅删除Log4j捆绑包会发生什么?还能找到Log4j类吗?

致谢,弗兰克

I have use log4j-1.2.17 bundle from Maven repo. I try to do this code in the bundle (my bundle calls to the log4j-1.2.17 bundle)

    PropertyConfigurator.configure(props());
private static Properties props() {
    Properties props = new Properties();
    props.put("log4j.rootLogger", "INFO, R");
    props.put("log4j.appender.R",
            "org.apache.log4j.DailyRollingFileAppender");
    props.put("log4j.appender.R.File", "logs/IhtikaClient.log");
    props.put("log4j.appender.R.Append", "true");
    props.put("log4j.appender.R.Threshold", "INFO");
    props.put("log4j.appender.R.DatePattern", "'.'yyyy-MM-dd");
    props.put("log4j.appender.R.layout", "org.apache.log4j.PatternLayout");
    props.put("log4j.appender.R.layout.ConversionPattern",
            //"%d{HH:mm:ss,SSS} %c - %m%n");
            //"[%5p] %d{yyyy-MM-dd mm:ss} (%F:%M:%L)%n%m%n%n");
            "[%5p] %d{yyyy-MM-dd mm:ss} %c (%F:%M:%L)%n%m%n");
    return props;
}

But I have error

log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [org.apache.felix.framework.BundleWiringImpl@9da1dd] whereas object of type log4j:ERROR "org.apache.log4j.DailyRollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@4b222f]. log4j:ERROR Could not instantiate appender named "R". log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [org.apache.felix.framework.BundleWiringImpl@9da1dd] whereas object of type log4j:ERROR "org.apache.log4j.DailyRollingFileAppender" was loaded by [sun.misc.Launcher$AppClassLoader@4b222f]. log4j:ERROR Could not instantiate appender named "R".

Bundles has loaded in the sequence

.getBundleContext().installBundle("......../log4j-1.2.17.jar")
.getBundleContext().installBundle("......../I_MainForm-1.0-SNAPSHOT.jar")

How to fix this error? Please, sorry my English. Best regards, Arthur.

解决方案

As your Log4j class seems to have been loaded by a sun.misc classloader, I think something is supplying the log4j classes from outside OSGi. The fact that there are Log4J entries explaining why it can not load Log4J classes means that some version of Log4J is already there.

Are you using a customized version of Felix? If so can you find something like a configuration file with an org.osgi.framework.system.packages=... entry or a org.osgi.framework.system.packages.extra=... entry?

What happens if you simply remove the Log4j bundle? Can it still find Log4j classes then?

Regards, Frank

这篇关于OSGi的类加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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