使用D96AInterchangeFactory转换EDI遇Smooks报错求助(Java 8)
NoSuchMethodError When Using D96AInterchangeFactory with Java 8 Hey, let's work through this issue you're hitting with EDIFACT/D96A parsing. That NoSuchMethodError tied to sun.misc.Unsafe.defineClass is a common compatibility hiccup between the libraries you're using and Java 8. Here's what's going on and how to fix it:
What's Causing the Error?
Even though you added MVEL2 2.2.0.Final, that version has a mismatch with Java 8's implementation of sun.misc.Unsafe. The method signature for defineClass changed in Java 8, and the older MVEL version is still calling the outdated variant. On top of that, you might be using a version of Smooks (the underlying library for D96AInterchangeFactory) that isn't fully tuned for Java 8.
Step-by-Step Fixes
1. Upgrade MVEL2 to a Java 8-Compatible Version
MVEL2 version 2.4.0.Final and above fixes the sun.misc.Unsafe compatibility issue. Dump the 2.2.0.Final version and use a newer stable release.
For Maven, update your dependency:
<dependency> <groupId>org.mvel</groupId> <artifactId>mvel2</artifactId> <version>2.4.12.Final</version> <!-- Latest stable as of now, any 2.4.x+ works --> </dependency>
For Gradle:
implementation 'org.mvel:mvel2:2.4.12.Final'
2. Ensure Smooks EDI Libraries Are Java 8-Friendly
Make sure your Smooks UN/EDIFACT dependencies are at version 1.7.5 or higher—this version added proper Java 8 support. Here's how to set that up in Maven:
<dependency> <groupId>org.milyn</groupId> <artifactId>milyn-smooks-edi</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.milyn</groupId> <artifactId>milyn-smooks-edifact</artifactId> <version>1.7.5</version> </dependency>
3. Clean Up Dependency Conflicts
It's possible another dependency in your project is pulling in an older MVEL or Smooks version behind the scenes. Use Maven's dependency tree to check for conflicts:
mvn dependency:tree | grep mvel
If you spot an unwanted old version, exclude it from the parent dependency. For example:
<dependency> <!-- Replace with the conflicting dependency in your project --> <groupId>com.example</groupId> <artifactId>some-dependency</artifactId> <exclusions> <exclusion> <groupId>org.mvel</groupId> <artifactId>mvel2</artifactId> </exclusion> </exclusions> </dependency>
Test the Fix
Once you've updated your dependencies and cleared conflicts, re-run your code:
D96AInterchangeFactory factory = D96AInterchangeFactory.getInstance(); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(ediContent.getBytes()); UNEdifactInterchange interchange = factory.fromUNEdifact(byteArrayInputStream);
This should resolve the error and let you parse your EDIFACT content successfully.
内容的提问来源于stack exchange,提问作者Sohaib Jamal

