Java 11 และวิธีแก้ไข java.lang TypeNotPresentException: พิมพ์ javax.xml.bind.JAXBContext ข้อยกเว้น?
เผยแพร่แล้ว: 2018-12-15คุณกำลังเผชิญกับ java.lang.TypeNotPresentException : พิมพ์ javax.xml.bind.JAXBContext ไม่มี ข้อยกเว้นหรือไม่
วันนี้ในขณะที่ใช้บริการ RESTful บน Eclipse ฉันสังเกตเห็นข้อผิดพลาด javax.xml.bind.JAXBContext
ในคอนโซล eclipse ของฉัน โปรแกรมเดียวกันทำงานได้ดีตั้งแต่ปีที่แล้วโดยไม่มีปัญหาใดๆ
ฉันเริ่มคิดถึงการเปลี่ยนแปลงที่เกิดขึ้นกับสภาพแวดล้อมในท้องถิ่นของฉัน ไม่มีการเช็คอินโค้ดใดๆ เกิดขึ้นตั้งแต่ปีที่แล้ว ดังนั้นฉันคิดว่าสาเหตุหลักมาจากการอัปเกรด Java 11 ล่าสุดของฉัน
ดังที่คุณเห็นในทุกอย่างเกี่ยวกับ Java 11 ก่อนหน้านี้ไม่มีการสนับสนุนโมดูล Java EE ในนั้นอีกต่อไป Java 11 ลบการสนับสนุนสำหรับ java.xml.bind
ซึ่งกำหนด Java Architecture for XML Binding (JAXB) API
เธอเป็นร่องรอยสแต็คที่สมบูรณ์:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Dec 14 , 2018 1 : 23 : 19 PM com . sun . jersey . api . core . servlet . WebAppResourceConfig init INFO : Scanning for root resource and provider classes in the Web app resource paths : / WEB - INF / lib / WEB - INF / classes Dec 14 , 2018 1 : 23 : 20 PM com . sun . jersey . server . impl . application . WebApplicationImpl _initiate INFO : Initiating Jersey application , version 'Jersey: 1.17.1 02/28/2013 12:47 PM' Dec 14 , 2018 1 : 23 : 20 PM org . apache . catalina . core . ApplicationContext log SEVERE : StandardWrapper . Throwable java . lang . TypeNotPresentException : Type javax . xml . bind . JAXBContext not present at java . base / sun . reflect . generics . factory . CoreReflectionFactory . makeNamedType ( CoreReflectionFactory . java : 117 ) at java . base / sun . reflect . generics . visitor . Reifier . visitClassTypeSignature ( Reifier . java : 125 ) at java . base / sun . reflect . generics . tree . ClassTypeSignature . accept ( ClassTypeSignature . java : 49 ) at java . base / sun . reflect . generics . visitor . Reifier . reifyTypeArguments ( Reifier . java : 68 ) at java . base / sun . reflect . generics . visitor . Reifier . visitClassTypeSignature ( Reifier . java : 138 ) at java . base / sun . reflect . generics . tree . ClassTypeSignature . accept ( ClassTypeSignature . java : 49 ) at java . base / sun . reflect . generics . repository . ClassRepository . computeSuperInterfaces ( ClassRepository . java : 117 ) at java . base / sun . reflect . generics . repository . ClassRepository . getSuperInterfaces ( ClassRepository . java : 95 ) at java . base / java . lang . Class . getGenericInterfaces ( Class . java : 1138 ) at com . sun . jersey . core . reflection . ReflectionHelper . getClass ( ReflectionHelper . java : 533 ) at com . sun . jersey . core . reflection . ReflectionHelper . getClass ( ReflectionHelper . java : 529 ) at com . sun . jersey . core . spi . factory . ContextResolverFactory . getParameterizedType ( ContextResolverFactory . java : 202 ) at com . sun . jersey . core . spi . factory . ContextResolverFactory . init ( ContextResolverFactory . java : 89 ) at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 183 ) at org . apache . catalina . core . StandardServer . startInternal ( StandardServer . java : 801 ) at org . apache . catalina . util . LifecycleBase . start ( LifecycleBase . java : 183 ) at org . apache . catalina . startup . Catalina . start ( Catalina . java : 695 ) at java . base / jdk . internal . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method ) at java . base / jdk . internal . reflect . NativeMethodAccessorImpl . invoke ( NativeMethodAccessorImpl . java : 62 ) at java . base / jdk . internal . reflect . DelegatingMethodAccessorImpl . invoke ( DelegatingMethodAccessorImpl . java : 43 ) at java . base / java . lang . reflect . Method . invoke ( Method . java : 566 ) at org . apache . catalina . startup . Bootstrap . start ( Bootstrap . java : 350 ) at org . apache . catalina . startup . Bootstrap . main ( Bootstrap . java : 492 ) Caused by : java . lang . ClassNotFoundException : javax . xml . bind . JAXBContext at org . apache . catalina . loader . WebappClassLoaderBase . loadClass ( WebappClassLoaderBase . java : 1328 ) at org . apache . catalina . loader . WebappClassLoaderBase . loadClass ( WebappClassLoaderBase . java : 1157 ) at java . base / java . lang . Class . forName0 ( Native Method ) at java . base / java . lang . Class . forName ( Class . java : 398 ) at java . base / sun . reflect . generics . factory . CoreReflectionFactory . makeNamedType ( CoreReflectionFactory . java : 114 ) . . . 59 more |
และนั่นคือเหตุผลที่ฉันเริ่มได้รับข้อยกเว้น แพ็คเกจ Java 11 สำหรับ javax.xml.bind
ไม่มีอยู่แล้ว

- เวอร์ชัน Java 7: รวมและใช้งานได้
- เวอร์ชัน Java 8: รวมและใช้งานได้
- เวอร์ชัน Java 9: เลิกใช้แล้ว
- เวอร์ชัน Java 10: เลิกใช้แล้ว
- เวอร์ชัน Java 11: ลบออก
ในการแก้ไขปัญหานี้ คุณต้องเพิ่มการพึ่งพา JAXB Maven ด้านล่างด้วยตนเองในโครงการของคุณ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
< dependency > < groupId > com . sun . xml . bind < / groupId > < artifactId > jaxb - core < / artifactId > < version > 2.3.0.1 < / version > < / dependency > < dependency > < groupId > javax . xml . bind < / groupId > < artifactId > jaxb - api < / artifactId > < version > 2.3.1 < / version > < / dependency > < dependency > < groupId > com . sun . xml . bind < / groupId > < artifactId > jaxb - impl < / artifactId > < version > 2.3.1 < / version > < / dependency > |
นี่คือไฟล์ pom.xml ที่สมบูรณ์ของฉัน หากจำเป็น:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
< 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/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > CrunchifyRESTfulServiceVersion < / groupId > < artifactId > CrunchifyRESTfulServiceVersion < / artifactId > < version > 0.0.1 - SNAPSHOT < / version > < packaging > war < / packaging > < build > < sourceDirectory > src < / sourceDirectory > < plugins > < plugin > < artifactId > maven - compiler - plugin < / artifactId > < version > 3.8.0 < / version > < configuration > < release > 10 < / release > < / configuration > < / plugin > < plugin > < artifactId > maven - war - plugin < / artifactId > < version > 3.2.1 < / version > < configuration > < warSourceDirectory > WebContent < / warSourceDirectory > < / configuration > < / plugin > < / plugins > < / build > < dependencies > < dependency > < groupId > com . sun . xml . bind < / groupId > < artifactId > jaxb - core < / artifactId > < version > 2.3.0.1 < / version > < / dependency > < dependency > < groupId > javax . xml . bind < / groupId > < artifactId > jaxb - api < / artifactId > < version > 2.3.1 < / version > < / dependency > < dependency > < groupId > com . sun . xml . bind < / groupId > < artifactId > jaxb - impl < / artifactId > < version > 2.3.1 < / version > < / dependency > < / dependencies > < / project > |
ลองทำความสะอาดโปรเจ็กต์ Eclipse ของคุณและลองเรียกใช้เซิร์ฟเวอร์ Tomcat อีกครั้ง คุณไม่ควรเห็นปัญหานี้อีกต่อไป
ขอให้สนุกกับการเขียนโค้ดและแจ้งให้เราทราบหากคุณยังคงเผชิญกับข้อยกเว้นนี้หลังจากเพิ่มการพึ่งพา Maven ด้านบน