无法在可执行jar的根目录中找到资源

时间:2015-11-14 06:04:56

标签: java maven classpath embedded-resource shiro

我有一个程序配置为使用Apache Shiro ini文件来存储其用户和权限。

以下代码适用于IDE

代码

public static String loginModule(String heelName, String heelPass) {

        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        Subject usr = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(heelName, heelPass);


        try {
            usr.login(token);
        } catch (AuthenticationException ae) {
            return ae.toString();
        }

但是当使用maven javafx插件编译时,程序无法在其类路径中找到shiro.ini文件。产生以下错误:

错误

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further detail
s.
Exception in thread "JavaFX Application Thread" org.apache.shiro.config.Configur
ationException: java.io.IOException: Resource [classpath:shiro.ini] could not be
 found.
        at org.apache.shiro.config.Ini.loadFromPath(Ini.java:242)
        at org.apache.shiro.config.Ini.fromResourcePath(Ini.java:225)
        at org.apache.shiro.config.IniSecurityManagerFactory.<init>(IniSecurityM
anagerFactory.java:69)
        at Login.loginModule(Login.java:23)
        at LoginController.attemptToLoginbutton(LoginController.java:178)
        at LoginController$1.handle(LoginController.java:98)
        at LoginController$1.handle(LoginController.java:96)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Comp
ositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(C
ompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8411)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Bu
ttonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorS
kinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorS
kinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.h
andleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Comp
ositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventH
andlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(C
ompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDis
patcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispat
chChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(GlassViewEventHandler.java:352)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotificatio
n.run(GlassViewEventHandler.java:275)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEve
nt$355(GlassViewEventHandler.java:388)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Quantum
Toolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Glas
sViewEventHandler.java:387)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.ja
va:191)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: Resource [classpath:shiro.ini] could not be foun
d.
        at org.apache.shiro.io.ResourceUtils.getInputStreamForPath(ResourceUtils
.java:139)
        at org.apache.shiro.config.Ini.loadFromPath(Ini.java:240)
        ... 61 more

由于这是一个maven项目,因此在查看项目源代码时,ini位于/ src / main / resources文件夹中。

我已经验证了shiro.ini文件实际上存在于可执行jar的根目录中。关于如何克服这个错误的任何建议??

潜在资源

https://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/config/IniSecurityManagerFactory.html

http://shiro.apache.org/configuration.html

2 个答案:

答案 0 :(得分:0)

我建议尝试:

new IniSecurityManagerFactory("classpath:/shiro.ini");

而不是:

new IniSecurityManagerFactory("classpath:shiro.ini");

答案 1 :(得分:0)

IDE比编译的程序更宽容。它仍然能够找到&#34; Shiro.ini&#34;给予&#34; shiro.ini&#34;但编译后的程序要求更正此大小写错误。

new IniSecurityManagerFactory("classpath:shiro.ini");

更正为:

new IniSecurityManagerFactory("classpath:Shiro.ini");

纠正问题

相关问题