溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Caused by: java.lang.NoClassDefFoundError:

發(fā)布時(shí)間:2020-07-22 00:08:46 來源:網(wǎng)絡(luò) 閱讀:2648 作者:gueryang 欄目:編程語言
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'identityService': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.example.new_oa.identity.service.impl.IdentityServiceImpl] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]

Caused by: java.lang.NoClassDefFoundError: javax/servlet/http/HttpSession
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_181]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_181]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_181]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpSession
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_181]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181]
    ... 24 common frames omitted

當(dāng)使用idea開發(fā)工具時(shí),maven作為項(xiàng)目管理工具時(shí),idea是默認(rèn)不提供servlet-api.jar的,所以當(dāng)javax.servlet-api的scope的值設(shè)置provide(表示該jar在編譯時(shí)需要,而在發(fā)布時(shí)不需要)時(shí)會(huì)提示找不到對(duì)應(yīng)的jar包,我這里是提示不能創(chuàng)建對(duì)應(yīng)的service,是因?yàn)槲业念愔杏玫搅薍ttpSession,依賴于servelt-api.jar.只需要把provide注釋掉即可。
Caused by: java.lang.NoClassDefFoundError:
但是,注釋掉之后,又出現(xiàn)了其他問題:如下:
Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$723b5875] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-06-29 20:05:39.786 WARN 4452 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
2019-06-29 20:05:39.794 INFO 4452 --- [ main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
Disconnected from the target VM, address: '127.0.0.1:49637', transport: 'socket'
2019-06-29 20:05:39.803 ERROR 4452 --- [ main] o.s.boot.SpringApplication : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
Caused by: java.lang.NoClassDefFoundError:
即在啟動(dòng)applicationContext容器失敗。解決辦法和上面大致一樣。如下圖:
Caused by: java.lang.NoClassDefFoundError:

在本地啟動(dòng)一個(gè)新項(xiàng)目時(shí),
報(bào)錯(cuò): java.lang.ClassNotFoundException: javax.servlet.Filter
環(huán)境intellJ idea,我同事的myeclipse就可以直接啟動(dòng)

最后搜了下問題
問題原因是:spring boot項(xiàng)目中引入使用的是scope=provided
而默認(rèn)情況下,idea是不會(huì)提供對(duì)應(yīng)的servlet-api.jar包eclipse會(huì)提供

網(wǎng)上有人說這是idea的bug,這么說顯然是不對(duì)的,是完全沒有理解provided和compile的區(qū)別
對(duì)于scope=compile的情況(默認(rèn)scope),也就是說這個(gè)項(xiàng)目在編譯,測試,運(yùn)行階段都需要這個(gè)artifact對(duì)應(yīng)的jar包在classpath中。而對(duì)于scope=provided的情況,則可以認(rèn)為這個(gè)provided是目標(biāo)容器已經(jīng)provide這個(gè)artifact。

??????? 這么說可能不是很好理解:換個(gè)簡單的,就是你在把這個(gè)項(xiàng)目打成war包時(shí),scope=provided的jar包,不會(huì)出現(xiàn)在WEB-INFO/lib目錄下,而scope=compile的jar包,會(huì)放到WEB-INFO/lib目錄

??????? 這樣可以避免想servlet-api.jar這樣的jar包沖突,但是如果遇到idea下默認(rèn)不提供servlet-api.jar時(shí),使用scope=provided就會(huì)缺少對(duì)應(yīng)的jar包,也就是我遇到的問題找不到類

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI