近一个周研究一个开源的项目opencustomer(http://sourceforge.net/forum/forum.php),下载部署,确怎么都不成功,发生错误如下:
24 May 2007 15:17:46 - org.apache.slide.common.Domain - INFO - Namespace configu
ration complete
ERROR [24.05.07 15:17:52]| 6599 a].[localhost].[/opencustomer]: StandardWrappe
r.Throwable
java.lang.NullPointerException
at org.opencustomer.framework.util.LocaleUtility.load(LocaleUtility.java
:88)
at org.opencustomer.webapp.struts.ActionServlet.init(ActionServlet.java:
162)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
java:1105)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:93
2)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContex
t.java:3951)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
225)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:73
9)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.jav
a:904)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.j
ava:867)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474
)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442
)
at org.apache.catalina.core.StandardService.start(StandardService.java:4
50)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709
)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
ERROR [24.05.07 15:17:52]| 6609 a].[localhost].[/opencustomer]: Servlet /openc
ustomer threw load() exception
java.lang.NullPointerException
at org.opencustomer.framework.util.LocaleUtility.load(LocaleUtility.java
:88)
at org.opencustomer.webapp.struts.ActionServlet.init(ActionServlet.java:
162)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
java:1105)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:93
2)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContex
t.java:3951)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4
225)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase
.java:759)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:73
9)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.jav
a:904)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.j
ava:867)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474
)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1122)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:310)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1021)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1013)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442
)
at org.apache.catalina.core.StandardService.start(StandardService.java:4
50)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:709
)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:432)
把我郁闷坏了,经过不断研究,发生是当TOMCAT在启动这个项目时,需要加载classes目录下的一些配置文件,而这个空指标的错误是找不到配置文件
原始代码在获取classes用的方法如下:
File defaultLocale = new File(LocaleUtility.class.getClassLoader().getResource(name+ending).getFile());
File[] localeFiles = defaultLocale.getParentFile().listFiles(new FileFilter() { //运行到此时,就报了空指针,因为listFiles
返回的localeFiles是空值,可是我输出了defaultLocale的文件都是正确的,路径也没错,可为什么就是不能列出这个文件正面的目录了??
。。。。。。。。
}
代码是没有问题的,想来想去一直以为是安全问题,到处找寻答案,走错了道,后来写了一段测试代码,输入整个类的路径看看
System.out.println(Test.class.getClassLoader().getResource("")); System.out.println(ClassLoader.getSystemResource(""));
System.out.println(Test.class.getResource(""));
System.out.println(Test.class.getResource("/"));
System.out.println(new File("").getAbsolutePath());
System.out.println(System.getProperty("user.dir"));
结果是:
file:/d:/Tomcat%205.5/webapps/opencustomer/WEB-INF/classes/
file:/d:/Tomcat%205.5/webapps/opencustomer/WEB-INF/classes/
null
file:/d:/Tomcat%205.5/webapps/opencustomer/WEB-INF/classes/org/opencustomer/fram
ework/util/
file:/d:/Tomcat%205.5/webapps/opencustomer/WEB-INF/classes/
d:\Tomcat 5.5
d:\Tomcat 5.5
看到有什么不同了吗???对了,就是最后两个输出的路径和前面几个的输出路径完全不一样
file:/d:/Tomcat%205.5/webapps 和 d:\Tomcat 5.5
问题就出在这里,也就是说在加载时,寻找类的路径错了,于是改代码为下面的
File defaultLocale = new File(path);//new File(LocaleUtility.class.getClassLoader().getResource(name+ending).getFile());
File[] localeFiles = defaultLocale.getParentFile().listFiles(new FileFilter() {
OK,问题解决,哈哈,高兴之余贴了出来,供大家参考,虽然很简单,也能给一些新手一些帮助,希望遇到困难时能够多看看源码,多写些测试的东西,多输出并比较,还请高手不要见笑,哈哈