说明:最近在学登录框架,记录一下学习刚shiro框架,启动快速入门样例的错误;
场景
把shiro代码download下来,打开samples(样例)包,打开快速入门,启动,报错,一气呵成;
提示有类没找到;
错误信息:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger at org.apache.logging.slf4j.SLF4JServiceProvider.initialize(SLF4JServiceProvider.java:56) at org.slf4j.LoggerFactory.bind(LoggerFactory.java:196) at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:183) at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:486) at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:472) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:421) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:447) at Quickstart.<clinit>(Quickstart.java:38) Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ... 8 more
解决
参考下面这篇文章:
- Shiro第一个程序官方快速入门程序Qucickstart详解教程
“山不向我走来,我便向山走去”,换了一种思路,新增一个Maven项目,将shiro中的文件一个个拷过来,如下:
(pom.xml,依赖相较shiro源码有改动)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hezy</groupId>
<artifactId>shiro-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Archetype - shiro-demo</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.6.0</version>
</dependency>
<!-- configure logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>2.0.0-alpha1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
(Java文件,有两个包报错,去掉即可)
再启动程序,就OK了,可以在对应的代码行打断点,查看执行情况,学习shiro对应的API。
总结
本文介绍了shiro-quickstart程序保存的解决方法,参考下文:
- Shiro第一个程序官方快速入门程序Qucickstart详解教程
注,官方说该快速入门样例为10分钟教程,我搞了1个多小时,人间失格……