为什么包名不能在jar中解析?

时间:2010-07-13 09:16:57

标签: java eclipse spring eclipse-rcp

我正在使用Spring集成的Eclipse RCP应用程序,但是在应用程序启动时初始化spring框架时出现错误。

我在Activator.java中的代码如下:

...
    public void start(BundleContext context) throws Exception {
        super.start(context);

        initializeApplicationContext();

        plugin = this;
    }

    private void initializeApplicationContext() {
        ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(
                    this.getClass().getClassLoader());
            ctx = new ClassPathXmlApplicationContext(SPRING_CONFIGS);
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            Thread.currentThread().setContextClassLoader(oldLoader);
        }
    }
...

例外是:

2010-07-13 16:38:42,421 INFO  [AbstractApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@be76c7: display name [org.springframework.context.support.ClassPathXmlApplicationContext@be76c7]; startup date [Tue Jul 13 16:38:42 CST 2010]; root of context hierarchy
2010-07-13 16:38:42,656 INFO  [XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [applicationContext.xml]
org.springframework.beans.factory.BeanDefinitionStoreException: I/O failure during classpath scanning; nested exception is java.io.FileNotFoundException: JAR entry net/interttimes/ not found in D:\Workspaces\MyEclipse 8.x\formicary-client2\lib\formicary-remote.jar
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:222)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:201)
    at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
    at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:69)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1297)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at net.interttimes.formicary.rcp.Activator.initializeApplicationContext(Activator.java:62)
    at net.interttimes.formicary.rcp.Activator.start(Activator.java:47)
    ...
Caused by: java.io.FileNotFoundException: JAR entry net/interttimes/ not found in D:\Workspaces\MyEclipse 8.x\formicary-client2\lib\formicary-remote.jar
    at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)
    at sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:71)
    ... 65 more

我的formicary-remote.jar的包名是以'net.interttimes。'开头的,所以我不明白,为什么无法解析包名?

感谢您关注我的问题!

3 个答案:

答案 0 :(得分:2)

谢谢大家!

我的问题解决了。这是因为我使用eclipse导出jar并没有检查'添加目录条目'选项。检查此选项后,它可以正常工作。 ^ _ ^

答案 1 :(得分:0)

我认为这可能是因为JAR文件(基本上是ZIP文件)每个文件都有一个条目,并且该文件的路径存储在其条目中,并且不必是任何条目该文件路径中的目录(虽然这是允许的)。

您可以使用

检查JAR中的文件
jar -tf <jar_file>

如果您没有看到要搜索的目录的单独行,这可以解释为什么会出现FileNotFoundException。

例如,对于条目:

a/b/c.txt

目录'a'和'b'在JAR中不一定有自己的条目(除非你单独添加)。

你会看到这样的事情,如果是这样的话:

a
a/b
a/b/c.txt

答案 2 :(得分:0)

查看jar文件(我们都知道它实际上是一个zip文件),spring需要一个路径

/net/interttimes/

内部。有时候在运行类时我们会意外地将bin文件夹添加到jar中,并且有类似

的内容
/bin/net/interttimes/

(这将是我最常见的错误..)