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>
I have a piece of code which has JRecord related components and Maven cannot find the dependency for cb2xml, so all the code is red. I looked for info on the internet, but could not find much. So ICobolIOBuilder, JRecordInterface1 and everything related to JRecord cannot be found.
I will post my code, can anyone help me with the dependencies?
final ICobolIOBuilder ioBldr = JRecordInterface1.COBOL
.newIOBuilder(copyBookFile)
.setFont("cp273")
.setDialect(ICopybookDialects.FMT_MAINFRAME);
final AbstractLineReader reader = ioBldr.newReader(dataFile);
AbstractLine l;
while ((l = reader.read()) != null) {
System.out.println(">>>>>" + l.getFullLine());
System.out.print("<<<<<");
final FieldIterator iter = l.getFieldIterator(0);
while (iter.hasNext()) {
final AbstractFieldValue field = iter.next();
System.out.print("|" + field.getFieldDetail().getName() + "=" + field.asString());
}
System.out.println("");
}
reader.close();
The dependencies related to Cobol I have are:
<dependency>
<groupId>net.sf.cobol2j</groupId>
<artifactId>cobol2j</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
You need to build cb2xml with maven first.
If you downloaded JRecord from Source Forge, The source for cb2xml should be in the Source\OtherSource\cb2xml_package directory.
Other options for Getting cb2xml source are
cb2xml on sourceforge
cb2xml on GitHub
Please add following dependency in pom.xml file
<dependency>
<groupId>net.sf</groupId>
<artifactId>jrecord</artifactId>
<version>0.90.2</version>
</dependency
Add repository:
<repositories>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>central</id>
<name>maven2</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>pentaho</id>
<name>omni</name>
<url>https://nexus.pentaho.org/content/groups/omni</url>
</repository>
</repositories>
It will download all necessary files from Pentaho
Now you can do maven update and maven clean install
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.
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.
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