How to fix the dependencies in order to parse an XML file? - java

I have searched stackoverflow on how to fix this, but nothing fixes it.
The code keeps breaking on line 166. I believe it has something to do with my pom file, but I cant figure out how to fix it.
160 DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
161 System.out.println("DB is"+db);
DB isorg.apache.xerces.jaxp.DocumentBuilderImpl#763334fe
162 InputSource is = new InputSource();
163 System.out.println("is= "+is);
is= org.xml.sax.InputSource#7fc7ed31
164 is.setCharacterStream(new StringReader(strXMLResponse));
165 System.out.println("strXMLResponse" + strXMLResponse);
strXMLResponse
166 Document doc = db.parse("is"+is);
167 System.out.println("doc"+doc);
168 nodes = doc.getElementsByTagName("Value");
169 System.out.println("node"+nodes);
This is my pom file: I'm using jdk 17.0.4.1. Java
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0-b01</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>4.0.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
<target.dir>target2</target.dir>
I have changed the pom dependencies and import statements. I have looked in stackoverflow to find the answer, but nothing works.

If that's your POM file, then your code is breaking because it's not well-formed XML, for example it has a </dependencies> end tag with no corresponding start tag.
However, because you haven't given any error messages or other diagnostics, we can only guess. [I can only think that you omitted the diagnostics because they didn't help you. That doesn't mean they won't help someone else.]

The issue seems to be with the db.parse() method call on line 166. You are passing a string that concatenates the literal string "is" with the object reference of the is variable, instead of passing the is object itself. To fix this, change line 166 to:
Document doc = db.parse(is);
I think this should resolve the issue.
Also, I noticed that the pom.xml file you provided does not contain the javax.xml.parsers dependency that is required for the DocumentBuilder class. You can add the following dependency to your pom.xml file to fix this:
<dependency>
<groupId>javax.xml.parsers</groupId>
<artifactId>javax.xml.parsers-api</artifactId>
<version>1.0.2</version>
</dependency>
Make sure to add this dependency within the dependencies tag in your pom.xml file.

Related

No Such Method Error exception thrown while using OK Http 3 with IPinfo?

I'm running the Java client library from IP Info. When I try to executing the code:
IPinfo ipInfo = new IPinfo.Builder().setToken(apiKey).build();
IPResponse response = ipInfo.lookupIP(ipAddress);
I get a java.lang.NoSuchMethodError: okhttp3.Response.close()V exception.
I tried explicitly changing the version of OK Http 3 from 5.0.0-alpha.2 to 5.0.0-alpha.1 to 4.9.0 (tried many other versions too) but they all yield the same error.
What is causing this error? Which version should I use to gain access to the said method?
My POM file has the following dependencies:
<dependencies>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.40.14</version>
</dependency>
<dependency>
<groupId>co.ipdata.client</groupId>
<artifactId>ipdata-java-client</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>io.ipinfo</groupId>
<artifactId>ipinfo-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
UPDATE:
Perhaps more intriguing, I noticed that when I open the said line with error in Eclipse
at io.ipinfo.api.request.IPRequest.handle(IPRequest.java:34)
It points to Line 34 in io.ipinfo.api.request; but there's absolutely no code on that line at all. The line simply contains the end of a curly brackets. See screenshot below:
Thanks to everyone for all the suggestions and input! I finally found the answer myself..
It turns out that this project dependent on a different project which included the following maven dependencies:
<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.0.2</version>
</dependency>
I deleted those Maven dependencies and now everything works as expected.

Shading of drools libraries leads to changes in text in GeneratorHelper.java class

I have the following set of dependencies in my pom.xml file
start of pom file
.
.
.
<dependency>
<groupId>org.drools</groupId>
<artifactId>knowledge-api</artifactId>
<version>6.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>6.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>6.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>6.3.0.Final</version>
</dependency>
.
.
.
end of pom file
I am using maven-shade-plugin to create the shaded dependency of this library.
Relocations being:
start of pom file
.
.
.
<relocation>
<pattern>org</pattern>
<shadedPattern>**shaded.pattern**.org</shadedPattern>
</relocation>
.
.
.
end of pom file.
When I did that, this line is getting replaced as
invokeStatic(MethodComparator.class, "**shaded.pattern**.compareBytecode", Boolean.TYPE, List.class, List.class);
because of which, it's causing issues.
Any idea why this string is being relocated? Any help on this is appreciated.
I have tried using the transformer specified in https://docs.jboss.org/drools/release/7.23.0.Final/drools-docs/html_single/#_building_and_running_drools_in_a_fat_jar, but it didn't help.
Try to find information about <build><plugins/></build> into pom.xml. It was helpful for me when i try to add my local dependencies into m2 repository. If you also got a problem, i will help you soon.

Using maven dependency "util-compress" in Java

I want to unzip a folder in Java using "util-compress" maven dependency . After adding the dependency to the pom, how to use it in java program. Can someone explain please ? I'm new to this field . Any proper tutorial guidance is welcomed.
Thanks in advance.
https://mvnrepository.com/artifact/io.vertx/mod-unzip/1.0.0-final
Java comes with “java.util.zip” library to perform data compression in ZIp format. The overall concept is quite straightforward.
Read file with “FileInputStream”Add the file name to “ZipEntry” and output it to “ZipOutputStream“
Below link may help to u
https://www.mkyong.com/java/how-to-compress-files-in-zip-format/
I hope you are using below dependency.
<dependency> <groupId>com.madgag</groupId> <artifactId>util-compress</artifactId> <version>1.33</version> </dependency>
This dependency internally deals with below dependencies
<dependencies>
<dependency>
<groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version>
<scope>test</scope>
</dependency>
<dependency> <groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.0</version>
</dependency>
</dependencies/>
So you can find examples in below link
https://commons.apache.org/proper/commons-compress/examples.html

Embedding barcodes using Apache Fop in Java

I'm using Apache Fop to generate a pdf document that includes a PDF417 barcode, and when i use the command line it works:
fop -fo source.fo -pdf result.pdf
But when I use the Java alternative, the barcode appears blank and the log show these warnings:
Unknown formatting object "{http://barcode4j.krysalis.org/ns}barcode" encountered (a child of fo:instream-foreign-object}. (See position 88:1266)
Unknown formatting object "{http://barcode4j.krysalis.org/ns}pdf417" encountered (a child of barcode}. (See position 89:45)
I've included these dependencies in Maven:
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j -->
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
What I'm missing?
Thanks in advance!
The answer was very simple, instead of using the maven dependecy:
<!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j -->
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j</artifactId>
<version>2.1</version>
</dependency>
I needed to use:
<!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j-fop-ext -->
<dependency>
<groupId>net.sf.barcode4j</groupId>
<artifactId>barcode4j-fop-ext</artifactId>
<version>2.1</version>
</dependency>
Pretty obvious now. Thanks to myself! :)

Writing Maven Dependency for javax.persistence

Can someone help me write the dependency for javax.persistence. I have googled it but nothing worked.
I bumped into this page that gives some details on how to write the dependency, but yet i am unable to write it. Can someone help me out?
This is the one for javax.persistence:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
and this is for the whole Java EE 6 stack:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
Edit
Note that I specified a provided scope here, which means that your dependency is available at compile- and test-time, but will not be packaged into your artifacts. This is usually needed if you want to deploy your artifacts in an application server, since they provide their own implementation of the api.
And add this dependency in your pom.xml:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
That "Coping with Sun JARs" page might be a little outdated, this JAR is available in the Maven Central Repository
Updated link:
https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api/2.2 is here.
and the maven dependency is as below:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
For the latest versions javax.persistance is not working instead of that we can use jakarta.persistence to create an entity or resolve the error Cannot resolve symbol 'Entity'. For that need to add the dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.6.Final</version>
<type>pom</type>
</dependency>

Categories

Resources