我是Spring Framework的新手。我紧随其后的是《 Spring 4 Professional》一书中的说明。在本书中,作者通过xml配置了框架,但我想使用Java对其进行配置。在运行时,我得到 ClassNotFoundException
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
我已经在
github。
我做错了什么?
最佳答案
在这里工作正常,运行时会打印Hello World!
。
您如何尝试运行它?
我想您尝试做java -jar spring-framework-example.jar
。
这当然是行不通的,因为那样会错过类路径中的库。
如果你交换
jar {
manifest {
attributes 'Main-Class': 'com.bakhtiyor.sample.Application'
}
}
与
apply plugin: 'application'
mainClassName = 'com.bakhtiyor.sample.Application'
然后使用
gradle run
,应用程序运行正常。PS:即使在最小的项目中,也应始终使用Gradle Wrapper。然后,其他人可以更轻松地构建您的项目,因为无需安装任何内容即可构建,并且该构建始终以该构建所针对的Gradle版本运行。
关于java - 找不到类:org.springframework.context.ApplicationContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782535/