How to choose which library to use in a class (maven)? - java

I'm started working on an already existing project. In this project there is some JSON-parsing happening with the following exception being thrown by several methods:
JSONException.class
While I was unit-testing these parsers I couldn't import the right org.json.JSONException library.
The maven library used in the codebase was (package org.json.JSONException):
org.json:json:20160810
And the one that was importing in my tests was (package org.json.JSONException):
com.vaadin.external.google:android-json:0.0.20131108.vaadin1
I think the problem lies in both libraries share the same package names. When the test is execute the JSONException is thrown but the test still fails because its probably the other library. Anybody knows why this problem is happening and how to solve it?
Thanks in advance!

I believe you need to updated your pom.xml file and in the dependency section you can exclude a 3rd party dependency from a declared dependency. I guess in your case there is a dependency to json and vaadin, you'll find vaadin is also pulling the different version of json.

You can try removing one of them. As suggested here:
https://github.com/spring-cloud/spring-cloud-deployer-kubernetes/issues/142

Related

Spring MVC application getting runtime exception after Spring framework upgrade to 5.2.20.Release [duplicate]

What are the possible causes for ABstractMethodError?
Exception in thread "pool-1-thread-1" java.lang.AbstractMethodError:
org.apache.thrift.ProcessFunction.isOneway()Z
at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:51)
at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
at com.gemfire.gemstone.thrift.hbase.ThreadPoolServer$ClientConnnection.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
It usually means that you are using an old version of an interface implementation which is missing a new interface method. For example java.sql.Connection interface got a new getSchema method in 1.7. If you have 1.6 JDBC driver and call Connection.getSchema you will get AbstractMethodError.
The simple answer is this: some code is trying to call a method which is declared abstract. Abstract methods have no body and cannot be executed. Since you have provided so little information I can't really elaborate more on how this can happen since the compiler usually catches this problem - as described here, this means the class must have changed at runtime.
From documnentation of AbstractMethodError
Thrown when an application tries to call an abstract method. Normally,
this error is caught by the compiler; this error can only occur at run
time if the definition of some class has incompatibly changed since
the currently executing method was last compiled.
A kind of special case of the above answer.
I had this error, because I was using a spring-boot-starter-parent (e.g. 2.1.0.RELEASE uses spring version: 5.1.2.RELEASE) but I also included a BOM, that also defined some spring dependencies, but in an older version (e.g. 5.0.9.RELEASE).
So one thing to do, is check your dependency tree (in Eclipse e.g. you can use the Dependency Hierarchy) if you are using the same versions.
So one solution could be that you upgrade the spring dependencies in your BOM, another one could be that you exclude them (but depending on the amount, this could be ugly).
If you download any project zip file, after unzipping them and importing into Android Studio, you are unable to run the project because this error happened.
I got out of the problem by deleting my android studio, then download and install the new version.
I truly hope it help.
If you you are getting this error on the implemented methods, make sure you have added your dependencies correctly as mentioned in this thread.
As Damian quoted :
Normally, this error is caught by the compiler; this error can only
occur at run time if [...]
I had the same error that was not caught by the compiler but at runtime. To solve it I only compiled again without giving the code any modification.
if you are getting this error on a minified build using Proguard then check if the class is a POJO class and if so then exclude it from the Proguard using the below rule:
-keep class your.application.package.pojo.** {*;}
I had the same error when I imported an eclipse project into intellij ide.. I tried to import it without .iml file then my problem was solved
I get this problem when I update my kotlin plugin to a new version .... the problem is that my pom file is using the older kotlin version .. I guess it might help someone if he is doing this mistake
I am getting various of these and others infrequently on android.. I have to clean everything change som configuration rebuild change configuration again to normal somehow just the build tools don't rebuild everything they should for whatever reason (Android gradle bug obviously).

Adding dependecy on module in gradle Java

I have a microservice that is Customer and also I have a common module that is called common. I have been trying to use a file that is called ResponseModel from common module in Customer service.
It says that "add dependency on module 'common main'".
Reference is not added even though I click the option.
I am a beginner in Java and using Intellij Idea.
Can anyone help me?
The problem has been solved by removing .idea folder and then rebuilded the project.
Thank you so much, #FloFromYet
Take a look here:
https://docs.gradle.org/current/userguide/declaring_dependencies_between_subprojects.html
So you can add manually, the common dependency in the customer sub-project.
To do this, you need to adapt the build.gradle file of customer:
dependencies {
implementation project(':common')
}
Do not also forgetto reload your project like described here:
https://www.jetbrains.com/help/idea/work-with-gradle-projects.html#gradle_refresh_project

The package org.apache.poi.ss.usermodel is accessible from more than one module: poi, poi.ooxm

in above all reference for this question it is not solved and dont give maven because not doing in maven.
error is The package org.apache.poi.ss.usermodel is accessible from more than one module: poi, poi.ooxm
in both error
i have to use both poi and poi--ooxml, please run this code i need to use it. even this code is sopied from the internet itself and there are many blogs who are showing this type code and it is the actual my requirement but its not working.
If someone have same issue just download new version of POI, old one is not compatible with new versions of java
I faced the same issue and figured out the solution. it's a little late but may help someone else facing the same problem.
in your module remove "requires poi" and just keep requires poi.ooxml;
module com.example.MyModlue {
//other require statement goes here
requires poi.ooxml;
}
now in your code
in place of using (CellType.NUMERIC)
import org.apache.poi.ss.usermodel.CellType;
for getting the cell type use (cell.getCellTypeEnum().NUMERIC) where cell if of type XSSFCell
import org.apache.poi.xssf.usermodel.XSSFCell;
Hope this helps. :)
just in module class add all lib in requires
module MyProjct {
requires poi;
requires poi.excelant;
requires poi.ooxml;
requires poi.ooxml.schemas;
}
This issue is due to multiple jars versions present in your eclipse setup.
Try deleting old/new versions depending on your requirement and try it will work.

Java Annotations simple error

I am newbie at Java EE and I have a simple problem.
I created a project using Maven plugin's default directory structure and I want to annotate a class as #Loggable, but I get a
Loggable cannot be resolved to a type
error. Any annotation I want to use can't be resolved to a type. What's the problem? I suspect that it has to do with dependency or build path issues but I can seem to get it working.
You will have a list of dependencies in the pom.xml file of your maven project. Make sure the jar that has Loggable is listed as a dependency.
Read here how to do it.
Your import java.lang.annotation.*; will not help as Loggable is not in that namespace.
Maybe you should write this Annotation on your own. Read this to learn more.
You have to import annotation classes just like anything else. Use an IDE such as Eclipse; it will handle and many similar tasks for you automatically.

ClassCastException in weblogic when using prefer-web-inf-classes

I had to add
<prefer-web-inf-classes>true</prefer-web-inf-classes>
to weblogic.xml to resolve a Hibernate antr compatibility issue with Weblogic. after adding that I was getting all different kind of classCastException related to XML parsers.
I understood from reading other threads that weblogic is trying to use a different class that what the application is expecting.
I spend all day researching and tried different solutions like removing "xml-apis......." jar files. but everytime I get ClassCastException. The cast "from" class changes when I remove jar files, but I always get
ClassCastException: "some xml parser related class" can not be cast to javax.xml.parsers.DocumentBuilderFactory
is there a way to know which xml parser jars are really causing the issue.
I m using Maven 2 to manage dependencies
Answering my own question:
I removed all jars that contains classes from javax.xml.* package by doing a java search and searching Package and check search in "application Libraries". then I had to remove sax..jar file. everything worked as expected after that.
Good answer to your own question. I also found this method (if you use maven), which generates a really nice dependency chart. I had this same problem, and using the dependency chart I was able to determine that the offending library, xml-apis*.jar, was loaded by maven as a dependency of jasperreports. Adding the exclusion element to the pom.xml for the jasperreports module fixed the problem.
http://maven.40175.n5.nabble.com/where-is-xml-apis-1-0-b2-jar-coming-from-td88057.html

Categories