簡體   English   中英

找不到類:org.springframework.context.ApplicationContext

[英]Class not found: org.springframework.context.ApplicationContext


我是Spring Framework的新手。 我遵循了《 Spring 4 for Professionals》一書中的說明。 在書中,作者通過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版本運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM