调试模式下实例化XSSFWorkbook抛出ClassNotFoundException(org.apache.xerces.util.SecurityManager缺失)问题求助
org.apache.xerces.util.SecurityManager When Instantiating XSSFWorkbook Hey there, let's walk through this weird issue you're facing—especially since it only pops up in debug mode and your old projects worked fine. Here are some targeted fixes to try:
1. First, Fix the Missing Xerces Dependency
Even though your IDE's auto-completion works, your runtime/debug classpath is missing the Xerces library that POI's XSSFWorkbook relies on to parse XML (since .xlsx files are just zipped XML under the hood).
If you're using Maven, add this to your pom.xml:
<dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.12.2</version> <!-- Use a version compatible with your POI version; check POI's docs if unsure --> </dependency>
For Gradle, add this to your build.gradle:
implementation 'xerces:xercesImpl:2.12.2'
Pro tip: If you're using POI's BOM (bill of materials) to manage versions, you can skip the version number here—it'll inherit the correct one from the BOM.
2. Adjust Your Debugger's Exception Breakpoints
You mentioned the exception is hitting a breakpoint, which is why disabling it breaks your debugging flow. Let's tweak that:
- In IntelliJ: Press
Ctrl+Shift+F8(or go to View → Tool Windows → Breakpoints) to open the breakpoints panel. Look forClassNotFoundExceptionin the list of exception breakpoints. - In Eclipse: Go to Window → Show View → Other → Debug → Breakpoints, then find the
ClassNotFoundExceptionentry.
Once you locate it:
- Either uncheck the breakpoint entirely (if you don't need to catch this specific exception)
- Or add a condition to exclude the Xerces-related instance: for example, set the condition to
!className.equals("org.apache.xerces.util.SecurityManager")so the breakpoint only triggers for other ClassNotFound exceptions.
3. Clean Up Build Caches & Fix Dependency Conflicts
Debug mode sometimes behaves differently if there's cached or conflicting code/dependencies:
- Run a clean build: For Maven, execute
mvn clean install; for Gradle, run./gradlew clean build. - Clear your IDE's cache:
- IntelliJ: Go to File → Invalidate Caches... → select "Invalidate and Restart"
- Eclipse: Go to Project → Clean, then select your project and rebuild.
- Check for dependency conflicts: Use
mvn dependency:tree(Maven) or./gradlew dependencies(Gradle) to see if there are multiple versions of Xerces or conflicting XML parsers (like xml-apis, xalan) in your project. If you find conflicts, exclude the unwanted versions from your dependencies.
4. Force POI to Use JDK's Built-in XML Parser
If adding Xerces doesn't work, you can try making POI use the Xerces implementation that comes bundled with your JDK instead of relying on external dependencies.
Either add these lines before instantiating XSSFWorkbook:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); System.setProperty("javax.xml.parsers.SAXParserFactory", "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
Or add these VM arguments to your debug configuration:
-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
Start with steps 1 and 2 first—those are the most likely fixes given your scenario. Since your old project worked, it's almost certainly a missing dependency or a debugger setting that got changed.
内容的提问来源于stack exchange,提问作者Mimmo

