Java XMLBeans throws NoSuchMethod error - java

I have following code
static {
wsAuthenticate = WsAuthenticate.Factory.newInstance();
...
}
which calls
public static final SchemaType type = (SchemaType)
XmlBeans.typeSystemForClassLoader(WsAuthenticate.class.getClassLoader(),
"schemaorg_apache_xmlbeans.system.sDE164C7F4147229ECE15396F7FA5432D")
.resolveHandle("wsauthenticate751ftype");
in jar file containing .xsb files.
I have used following jar files
xBean1.0.3.jar
xmlBeans2.3.0.jar
xmlSchema1.4.7.jar
But I am facing following exception:
java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(Ljava/lang/ClassLoader;Ljava/lang/String;)Lorg/apache/xmlbeans/SchemaTypeSystem;
at com.travis.webservices.WsAuthenticate.<clinit>(WsAuthenticate.java:19)
at com.travis.webservices.WsAuthenticate$Factory.newInstance(WsAuthenticate.java:119)
at com.mmi.api.ticketvala.TicketWalaMain.<clinit>(TicketWalaMain.java:64)
Exception in thread "main"
at :
XmlBeans.typeSystemForClassLoader(WsAuthenticate.class.getClassLoader(), "schemaorg_apache_xmlbeans.system.sDE164C7F4147229ECE15396F7FA5432D").resolveHandle("wsauthenticate751ftype");
What could be the reason and how to solve it?

This typeSystemForClassLoader method is introduced in version (2.1.0) . See javadoc checking with previous versions you can see this method is missing.
<dependency>
<groupId>xmlbeans</groupId>
<artifactId>xbean</artifactId>
<version>2.1.0</version>
</dependency>

I was also getting same exception when reading .xlsx file
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
Caused By :java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlBeans.typeSystemForClassLoadern
Then I"ve removed my old xmlbeans-2.0.jar and put the latest xmlbeans-2.3.0.jar containing this method and it is solved now

adding this dependency solved my problem:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>

Related

DTD parsing with axiom

I'm trying to use axiom 1.2.22 with woodstox 6.2.6 to parse an XML document with a doctype. (I'm using OpenJDK 11 but that shouldn't make any difference.) I'm getting the same error that was mentioned in How to ignore DTD parsing in Apache's AXIOM :
Cannot create OMDocType because the XMLStreamReader doesn't support the DTDReader extension
According to https://issues.apache.org/jira/browse/AXIOM-475 that was supposed to be fixed with axiom 1.2.16, but it seems the bug is back again.
Example snippet:
InputStream is = Test.class.getResourceAsStream("xml-with-dtd.xml");
OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(XMLInputFactory.newFactory().createXMLStreamReader(is));
OMElement result = builder.getDocumentElement();
Am I using incompatible versions? I also tried using woodstox 5.0.0, which throws the same error. I also verified that it's actually the woodstox XMLInputFactory when using XMLInputFactory.newFactory() that is used. These are the maven dependencies that I use (I've omitted some exclusions related to logging and duplicated classes):
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>6.2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-impl</artifactId>
<version>1.2.22</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>axiom-api</artifactId>
<version>1.2.22</version>
</dependency>
Update: Looks a lot like the axiom code tries to determine a DTDReader class to use from a configuration property. Unfotunately setting the property DTDReader.PROPERTY in the XMLInputFactory to any value results in the following stack trace:
Exception in thread "main" java.lang.IllegalArgumentException: Unrecognized property 'org.apache.axiom.ext.stax.DTDReader'
at com.ctc.wstx.api.CommonConfig.reportUnknownProperty(CommonConfig.java:167)
at com.ctc.wstx.api.CommonConfig.setProperty(CommonConfig.java:158)
at com.ctc.wstx.api.ReaderConfig.setProperty(ReaderConfig.java:35)
at com.ctc.wstx.stax.WstxInputFactory.setProperty(WstxInputFactory.java:400)
I'm not sure why it didn't work when I tried it with woodstox 5, but this little patch against axiom 1.2.22 solves the problem at least for woodstox 6.2.6:
Index: axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialectDetector.java
===================================================================
--- axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialectDetector.java (revision 1891409)
+++ axiom-api/src/main/java/org/apache/axiom/util/stax/dialect/StAXDialectDetector.java (working copy)
## -274,6 +274,7 ##
return new Woodstox4Dialect(version.getComponent(1) == 0 && version.getComponent(2) < 11
|| version.getComponent(1) == 1 && version.getComponent(2) < 3);
case 5:
+ case 6:
return new Woodstox4Dialect(false);
default:
return null;
Update:
Version 1.3.0 of axiom also fixes the problem.

how to fix java.lang.NoClassDefFoundError: org/apache/commons/net/ntp/NTPUDPClient

I downloaded commons-net-3.1.jar on this website:
https://jar-download.com/maven-repository-class-search.php?search_box=org.apache.commons.net.ntp.NTPUDPClient
it worked fine, but now I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/net/ntp/NTPUDPClient
Caused by: java.lang.ClassNotFoundException: org.apache.commons.net.ntp.NTPUDPClient
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
Can someone help me to fix this problem? I already searched for a solution, but I just find android threads without a solution for my problem.
Edit: I am using javaSE-14 (java-JDK) and eclipse. I am using the standard javaSE-14 library and a new created library with commons-net-3.7.jar (from Uroš answer), but I still have the same error. (I am not using a maven projekt, I am using a normal java projekt)
I tried to install the jar into my pom.xml with this text:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.7</version>
<scope>system</scope>
<systemPath>
C:\Users\denni\Downloads\Apache jar\commons-net-3.7.jar
</systemPath>
</dependency>
I also tried the text that was written below, but I still have the same problem
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.7</version>
</dependency>
import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient;
import org.apache.commons.net.ntp.TimeInfo;
public class time {
public static void main(String[] args) throws Exception {
NTPUDPClient client = new NTPUDPClient();
client.open();
// use host name or IP address of target NTP server
InetAddress hostAddr = InetAddress.getByName("pool.ntp.org");
TimeInfo info = client.getTime(hostAddr);
info.computeDetails(); // compute offset/delay if not already done
Long offsetValue = info.getOffset();
Long delayValue = info.getDelay();
String delay = (delayValue == null) ? "N/A" : delayValue.toString();
String offset = (offsetValue == null) ? "N/A" : offsetValue.toString();
System.out.println(" Roundtrip delay(ms)=" + delay
+ ", clock offset(ms)=" + offset); // offset in ms
client.close();
}
}
Hi Hans and welcome to SO.
java.lang.NoClassDefFoundError is not the same as java.lang.ClassNotFoundException.
What is most likely happening to you is that you have included commons-net-3.1.jar, but that doesn't mean commons-net-3.1.jar doesn't depend on some other .jar.
You haven't posted what your classpath looks like or in which IDE you are working in, whatever the case may be I strongly suggest you avoid manually adding .jar files from random sources because:
You will get errors like this one, missing dependencies.
More importantly, it is insecure! You may include some malicious stuff without knowing, and let us be honest here - you sure ain't gonna decompile that .jar and check if it is fishy or not.
Ergo I recommend looking up Maven and how to use it with your IDE, there are plenty of tutorials online about it.
You can use https://mvnrepository.com/ to find the dependecy you need, for example the one you are looking for can be found here, and is currently at version 3.7.
To include it in you maven project all you would need to do is to add it to pom.xml
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.7</version>
</dependency>

Running *.ktr in Java throws KettleMissingPluginsException - Why?

I created a variety of data transformations (*.ktr files) which run perfectly when started from the Spoon GUI (PDI-CE 5.4.0.1-130; Windows 7).
I try to run them from Java with the following code (close to the example code from the documentation):
KettleClientEnvironment.init();
TransMeta metaData = new TransMeta("C:\\examplepath\\test.ktr");
Trans transformation = new Trans(metaData);
transformation.execute(null);
transformation.waitUntilFinished();
...
When executed, I get the following exception:
org.pentaho.di.core.exception.KettleMissingPluginsException:
Missing plugins found while loading a transformation
Step : CsvInput
Step : XMLOutput
at org.pentaho.di.trans.TransMeta.loadXML(TransMeta.java:2882)
at org.pentaho.di.trans.TransMeta.<init>(TransMeta.java:2718)
at org.pentaho.di.trans.TransMeta.<init>(TransMeta.java:2670)
at org.pentaho.di.trans.TransMeta.<init>(TransMeta.java:2647)
at org.pentaho.di.trans.TransMeta.<init>(TransMeta.java:2627)
at org.pentaho.di.trans.TransMeta.<init>(TransMeta.java:2592)
at org.pentaho.di.trans.TransMeta.<init>(TransMeta.java:2555)
at (caller method in my code)
As I am not using any plugins but only native steps (in this example CsvInput, XMLOutput), I do not understand the reason for the thrown Exception. Why is it thrown and how can I fix the code to run?
Am I maybe missing maven dependencies? I use the following repository http://repository.pentaho.org/content/groups/omni/ and dependencies:
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
<version>5.4.0.1-130</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
<version>5.4.0.1-130</version>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-ui-swt</artifactId>
<version>5.4.0.1-130</version>
</dependency>
<dependency>
<groupId>pentaho-library</groupId>
<artifactId>libformula</artifactId>
<version>5.4.0.1-130</version>
</dependency>
Thanks a lot in advance for your answers.
I accidentally used KettleClientEnvironment.init() but I should have used KettleEnvironment.init(). Consequently, the environment was not properly initialized which triggered the Exception. Wow. That's a rookie mistake :)
The corrected code, as it can also be found in the Kettle docs and Rishu's example:
KettleEnvironment.init();
TransMeta metaData = new TransMeta("C:\\examplepath\\test.ktr");
Trans transformation = new Trans(metaData);
transformation.execute(null);
transformation.waitUntilFinished();
...
Thanks lufki and Rishu for the comments and pointers.

Reading DOCX file content

try {
Pattern p = Pattern.compile(b.toUpperCase());
POITextExtractor text = ExtractorFactory.createExtractor(file);
String text1 = text.getText();
Matcher m = p.matcher(text1.toUpperCase());
}
catch(IOException | OpenXML4JException | XmlException e){}
while running this code in NetBeans IDE 8.0 i am getting an error,
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/poi/hwpf/OldWordFileFormatException
How to avoid this error.
package included is poi.ooxml
You need to add the poi-scratchpad jar to your classpath. It contains the missing class: org.apache.poi.hwpf.OldWordFileFormatException.java.
you should add poi-scratchpad.jar to your build path or add this dependency
to your pom.xml if maven project :
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.11</version>
</dependency>
you should use poi-scratchpad with version 3.7 or newer one.

Error unmarshalling xml in java-8 "secure-processing org.xml.sax.SAXNotRecognizedException causing java.lang.IllegalStateException"

The following code worked fine in Java 7
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
String xmlString = '<xml ..... ';
StringReader reader = new StringReader(xmlString);
JAXBContext jc = JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyClass myClass = (MyClass) unmarshaller.unmarshal(reader);
....
Now we had to upgrade to Java 8 and now I get this exception when executing the code:
Sep 03, 2014 1:42:47 PM com.sun.xml.internal.bind.v2.util.XmlFactory createParserFactory
SCHWERWIEGEND: null
org.xml.sax.SAXNotRecognizedException: Feature: http://javax.xml.XMLConstants/feature/secure-processing
at org.apache.xerces.jaxp.SAXParserFactoryImpl.setFeature(SAXParserFactoryImpl.java:100)
at com.sun.xml.internal.bind.v2.util.XmlFactory.createParserFactory(XmlFactory.java:114)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getXMLReader(UnmarshallerImpl.java:139)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
I know that there is a question targeting a similar problem, but stepping back to java 7 is not a solution for me.
I tried to add the following maven dependency
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4</version>
</dependency>
but that did not change the result, so I removed it (thanks to #BlaiseDoughan for the information, that this is included in Java 6)
Any hints are welcome, many thanks.
We had a similar issue - our head developer found a solution that works for us.
We added this dependency to a couple of our pom.xml files
For those that care, the unit tests in Sonar that were failing were apparently failing because Cobatura by default pulls in an old version of xerces. The version it pulls in is incompatible with JAX-B in Java 8. The library is not used in production code – just Cobatura. Therefore, the fix was to add a test dependency on a more recent version of xerces (2.11.0). This is done by adding the dependency to the pom file:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
<scope>test</scope>
</dependency>
Xerces impl is the main culprit here. Remove it. Jdk has inbuilt jaxb parser, you don't need this.
so, if that dependency is coming from a parent project in case of maven
use a exclusion tab in case you can't directly remove it.
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
The reason this problem is so hard to detect is because, when you usually write a jaxb unmarshalling code
you will do a unmarshalling on a try block and then catch jaxb exception and then do whatever with the error.
But this culprit parser of a jar (xercesimpl) throws a runtime exception in the middle causing error to not
get logged and will be only be detected after careful debugging. Look at the code snippet below
try {
JAXBContext context = JAXBContext.newInstance(YourClass.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
YourClass object = (YourClass)unmarshaller.unmarshal(new StringReader("SomeXmlInString"));
}
catch (JAXBException e){
e.printStackTrace();
}
Here xercesImpl causes the unmarshaller to use some other sax parser (instead of the regular jaxb parser)
causing it to throw different exception which
won't be caught in our catch block which is expecting a jaxbexception or one of its subclasses.
Another possible solution to this is to add system variables:
I used these in the maven tomcat plugin which worked for me:
<javax.xml.parsers.DocumentBuilderFactory>com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl</javax.xml.parsers.DocumentBuilderFactory>
<org.xml.sax.parser>com.sun.org.apache.xerces.internal.parsers.SAXParser</org.xml.sax.parser>
<javax.xml.parsers.SAXParserFactory>com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl</javax.xml.parsers.SAXParserFactory>
But you should also be able to set as follows:
java -Dorg.xml.sax.parser="com.sun.org.apache.xerces.internal.parsers.SAXParser" \
-Djavax.xml.parsers.DocumentBuilderFactory="com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl" \
-Djavax.xml.parsers.SAXParserFactory="com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"
or even use System.setProperty:
System.setProperty("org.xml.sax.driver", "com.sun.org.apache.xerces.internal.parsers.SAXParser");
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");
It was a dependency problem.
Here is my way how I solved the problem:
Make a new maven project, with that simple pieces of code, I attached below, the programm crashed normally with an error, that the structure couldn't be parsed which is ok.
Copy your dependencies into the project pom.xml, now the programm should crash (as described above)
no you remove dependencies after your favoured method (good guessing, Bisection , 1-by-1 ..) to find the "bad" dependency. Maybe someone has a better (more professional) method, this one worked for me.
now you can descide what to do, maybe a new version is available, in our case it was out own package of a colleage, where he included a package of a colleage, which i could exclude.
public class Test {
public Test() {
}
public static void main(String[] args) {
try {
StringReader reader = new StringReader("<xml></xml>");
JAXBContext jc = JAXBContext.newInstance(TestXML.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
TestXML testXMLs = (TestXML) unmarshaller.unmarshal(reader);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
and the testXML class
#XmlRootElement(name="rss")
#XmlAccessorType(XmlAccessType.FIELD)
public class TestXML {
public TestXML() {
}
#XmlElementWrapper(name="channel")
#XmlElement(name="item")
private int i ;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
}
BTW: In my case it was
<dependency>
<groupId>jcs</groupId>
<artifactId>jcs</artifactId>
<version>1.3</version>
</dependency>
Hope that helps.
An implementation of JAXB has been included in Java SE since version 6. If you remove the Maven dependency (which is probably causing a version conflict), everything should work.
Both Bernard and Blaise's answers were very helpful. In my case, since I am using JDK 7, the solution was to exclude the xerces subdependency that was being included by one of my dependencies:
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xmlParserAPIs</artifactId>
</exclusion>
</exclusions>
</dependency>
I resolved this problem on my project with the second Mitch ‘solution but just with
java -Djavax.xml.parsers.SAXParserFactory="com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"
Using SAXparser can be a nightmare. This is the most widely used XML parser in java and every one have end up using either directly or indirectly. JDK 8 have JAXB already available . So if you are using JDK 8 then only possible way should be removing the maven dependency.
I had this issue as well so I tried removing the maven dependency but not happened. Then I thought why not revert to older version if java and VOILLA I got success. I am using jdk 7 currently and my tests run smoothly. I guess this is the only solution.
Try to create a XML Document and unmarshal it. It was worked for me.
JAXBContext jc = JAXBContext.newInstance( Message.class );
InputStream stream = new ByteArrayInputStream( string.getBytes( StandardCharsets.UTF_8 ) );
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( stream );
Unmarshaller unmarshaller = jc.createUnmarshaller();
Message msg = ( Message ) unmarshaller.unmarshal( doc );
We also met the issue and noticed that you need to keep the jdk version and jre version the same, otherwise will exist the version mismatch caused the issue.
The guys who met the issue use jdk1.6 and jre 1.8, when changed to both jdk1.6, the issue gone.
I had faced similar issue, this issue occurs when there is big difference in versions of xerces jar and xercesImpl jar. To solve this, I used xerces-2.9.0 and xercesImpl-2.9.1 and the issue gone.
I had a similar issue while trying to solve the sonar vulnerability java:S2755.
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); // added (for sonar)
schemaFactory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); // added (for sonar)
After adding those two lines the first setProperty() call threw a SAXNotRecognizedException.
I guess this happened because there were multiple implementations available of XMLSchemaFactory, one of them being included in the JDK.
My solution was to run mvn dependency:tree on the project and search for any occurences of "xerces". I found two dependencies which I excluded from all dependencies that used them:
<dependency>
<!-- ... -->
<exclusions>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- ... -->
<exclusions>
<exclusion>
<groupId>com.rackspace.apache</groupId>
<artifactId>xerces2-xsd11</artifactId>
</exclusion>
</exclusions>
</dependency>
I had the same issue and was fixed by setting JVM arguements as
-Djavax.xml.accessExternalDTD=all -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

Categories

Resources