Spring Boot Jar file usage problem when using artifact - java

I have a library project that I build and generated jar file in the out folder. Then I was using this Jar file by adding to another Spring Boot project via the Libraries section.
While it was working, the I started to get the following error and I am not sure if something is changed after building the library project:
The following method did not exist:
'void org.springframework.beans.factory.support.DefaultListableBeanFactory.setApplicationStartup(org.springframework.core.metrics.ApplicationStartup)'
The method's class, org.springframework.beans.factory.support.DefaultListableBeanFactory, is available from the following locations:
jar:file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.class
jar:file:/Users/einstein/.m2/repository/org/springframework/spring-beans/5.3.8/spring-beans-5.3.8.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.class
The class hierarchy was loaded from the following locations:
org.springframework.beans.factory.support.DefaultListableBeanFactory: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.AbstractBeanFactory: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.FactoryBeanRegistrySupport: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
org.springframework.core.SimpleAliasRegistry: file:/Users/einstein/project/demo/out/artifacts/gks_jar/gks.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.DefaultListableBeanFactory
So, what is missing and how can I solve the problem?
Here is my lombok depebdency in pom.xml file:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

If your library jar has it's size in MBs then it must be a fat jar. You should change the scope of jar's dependencies in pom.xml to provided and then build jar. For example
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
**<scope>provided</scope>**
</dependency>
Above you can see scope is set to provided that means the dependencies which are required by your library will be provided by parent application which will be using it.

Related

java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil

I am using Windows10, eclipse-neon with JDK1.8 version,
I am getting the following exception.
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bytedeco.javacpp.Loader.load(Loader.java:385)
at org.bytedeco.javacpp.Loader.load(Loader.java:353)
at org.bytedeco.javacpp.avformat$AVFormatContext.<clinit>(avformat.java:2719)
at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:391)
at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:385)
at com.segment.processor.AudioMain.main(ApacheMathAudioMain.java:20)
Error getting static method ID of org/bytedeco/javacpp/Loader/putMemberOffset
here are the dependencies I am using in my pom.xml
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg</artifactId>
<version>3.0.2-1.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.1.0-1.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>0.10</version>
</dependency>
First problem
Your versions don't match. org.bytedeco.javacpp in version 0.10 is from Dec 2014, while all your other versions are from May 2016. You need to use version 1.2 of org.bytedeco.javacpp, or better yet, update all dependencies to the latest version.
You can see the versions here:
org.bytedeco.javacpp-presets » opencv
org.bytedeco.javacpp-presets » ffmpeg
org.bytedeco » javacv
org.bytedeco » javacpp
Second problem
You include the dependencies for Java code only, but you don't include the dependencies for native code (both opencv and ffmpeg are native libraries). You need to include opencv-platform and ffmpeg-platform instead:
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>3.4.2-1.4.1</version>
</dependency>
This will make Maven download and include opencv and ffmpeg libraries for Android, Linux, MacOS and Windows, both x86 and x64.
java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil
Actual cause of error is different versions of dependencies. That's why javacpp package failed during mapping of classes.
Follow these step to resolve this problem:
Download latest version of Javacv library package from here
Copy these three .jar files into libs folder
ffmpeg.jar
javacv.jar
javacpp.jar
Create jniLibs folder in app\src\main
Now, create four different folders for different architectures
arm64-v8a
armeabi
armeabi-v7a
x86
Change extension of these two files ffmpeg-android-arm.jar, ffmpeg-android-x86.jar to .zip then unzip both folders and Copy .so files for each architecture and paste in its respected directory. Your resultant directory should be look like this
Add .jar dependencies in your gradle file as follows:
implementation files('libs/ffmpeg.jar')
implementation files('libs/javacpp.jar')
implementation files('libs/javacv.jar')```
Thanks for reading :)
Try to update the dependencies with the latest versions and check whether class exists in that or not
https://mvnrepository.com/artifact/org.bytedeco/javacv/1.4.1
https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/ffmpeg/3.4.2-1.4.1
https://mvnrepository.com/artifact/org.bytedeco.javacpp-presets/opencv/3.4.1-1.4.1
This means that the class is there at compiletime but missing at runtime. You have a couple of options:
Always execute with a Maven plugin.
Include the library in the classpath when running.
Use the Maven Shade plugin to make an Uber-Jar which includes dependencies.
I hope this helps!

NoClassDefFoundError when importing Tika 1.13 in Eclipse

I've done the following steps per the tika guide:
Add the tika-core and tika-parser dependencies to the pom.xml of the maven project
Run maven install from eclipse to produce tika-core jar and tika-parser jar
Add tika-core jar and tika-parser jar to my eclipse project build path
And I get this runtime exception when trying to run tika:
Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException
at com.ibm.hrl.ace.pdftotext.TikaExtracter.parse(TikaExtracter.java:33)
at com.ibm.hrl.ace.pdftotext.Main.AllPdfsToText(Main.java:116)
at com.ibm.hrl.ace.pdftotext.Main.main(Main.java:34)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException
at java.net.URLClassLoader.findClass(URLClassLoader.java:600)
at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:786)
at java.lang.ClassLoader.loadClass(ClassLoader.java:760)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:326)
at java.lang.ClassLoader.loadClass(ClassLoader.java:741)
... 3 more
As far as I can see, when I build the jars using maven, it does add pdfbox properly... from the build log:
[INFO] Including org.apache.pdfbox:pdfbox:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:fontbox:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:pdfbox-tools:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:pdfbox-debugger:jar:2.0.1 in the shaded jar.
[INFO] Including org.apache.pdfbox:jempbox:jar:1.8.12 in the shaded jar.
And here are my maven dependencies:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-core -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-parsers -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
The problem is that if you manually add tika-core and tika-parsers jars in your build path you will not have the transitive dependencies that are listed in their own POM.
So I would suggest to:
Remove the tika-core and tika-parsers version that you have built yourself. Instead you should rely on the versions that are available on central. This will ensure that another one building your project will get the same jar (and not a locally built one)
You have two options
(Option A, use Maven) Do not add manually into Eclipse build path the jars. Rely either or built-in Maven plugin for Eclipse (m2e for instance) or use Eclipse plugin for maven (call mvn eclipse:eclipse to update .classpath and .project).
(Option B, without Maven) If you cannot use Maven for your project, you will have to add not only tika-parsers and tika-core jars, but all (most of) the transitive dependencies needed by these project (including for instance specific library per format [POI for Office, pdfbox for PDF...). You can get a list of the dependencies by typing mvn dependency:list in the folder containing the pom of tika-parsers.

maven external jar dependency

I am trying to use a jar given by my teacher in my code. So I placed the jar in a lib directory in my project and ran this command :
mvn install:install-file -Dfile=lib/IDLogger.jar -DgroupId=IDLogger -DartifactId=IDLogger -Dversion=1.0 -Dpackaging=jar
Everything is fine.
Then I add the dependency to the pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
But then when I try to use the jar, the compilation fails and tells me that the symbol(the class) can't be found. I even tried adding an import statement :
import IDLogger.IDLogger;
but it tells me that there is no such package.
How can I use this jar in my code in maven?
This is the code :
IDLogger logger = IDLogger.getInstance();
...
logger.logID(id);
I get the symbol IDLogger not found error
This is as much as I know about this custom jar...
Try to add your lib in system scope :
1. add in your project :
Your Project/lib
+IDLogger.jar
2. in your pom :
<dependency>
<groupId>IDLogger</groupId>
<artifactId>IDLogger</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/IDLogger.jar</systemPath>
</dependency>

SonarQube "Class Not Found" during Main AST Scan

My setup:
Sonarqube 5.1.1
Sonar-Maven Plugin 2.6 (also tried 2.7 and 3.6)
JDK 1.7.0_51
Example of the error:
16:00:54 [INFO] [23:00:54.219] Sensor JavaSquidSensor
16:00:55 [INFO] [23:00:55.030] Java Main Files AST scan...
16:00:55 [INFO] [23:00:55.030] 1532 source files to be analyzed
16:00:58 [ERROR] [23:00:57.927] Class not found: javax.annotation.Nullable
16:00:58 [ERROR] [23:00:57.928] Class not found: javax.annotation.CheckReturnValue
16:00:58 [ERROR] [23:00:58.114] Class not found: javax.annotation.Nullable
According to this stackoverflow question, javax.annotation should be part of java 1.7 and up. Furthermore, I've tried putting it in the local maven repository but that didnt help.
So where is Sonar trying to find this package? Any help?!?
Update:
I've tried modifying the sonar-maven-plugin to include a dependency on javax.annotation
I've tried putting the dependency in my maven's settings.xml
Upgrading my JDK to 1.8 has not helped.
According to http://docs.oracle.com/javase/7/docs/api/index.html?javax/annotation/package-summary.html the classes you expect are not part of JDK 7.
The classes you're looking for are part of google JSR-305 implementation that was initiated here https://code.google.com/p/jsr-305/source/browse/trunk/ri/src/main/java/javax/annotation/Nullable.java?r=24 and which moved to Findbugs:
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.0</version>
</dependency>
According to https://jcp.org/en/jsr/detail?id=305 the JSR-305 is finished, but is in dormant status and has not been added to a JDK release yet.
Hope it helps.
To avoid adding SonarQube specific dependencies to your project, define a profile like this:
<profile>
<id>sonarqube</id>
<dependencies>
<dependency>
<groupId>org.joda</groupId>
<artifactId>joda-convert</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</profile>
Then run your sonar analysis with a command like
mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.0.1:sonar -Psonarqube,sonarqube-dev
The sonarqube-dev profile is defined in my ~/.m2/settings.xml and it just specifies where my development environment SonarQube installation is
<profile>
<id>sonarqube-dev</id>
<properties>
<!-- no direct db connections in new sonar -->
<sonar.host.url>
http://localhost:9000/
</sonar.host.url>
</properties>
</profile>
What is achieved by all this?
sonarqube analysis specific dependencies don't pollute the project unnecessarily
no sonarqube maven plugin defined in pom.xml. Each developer and Jenkins can use whatever sonar plugin and server installation they wish
This is more an addendum to the latest answer:
I see similar problems and adding the google findbugs dependency to the project dependencies helps. Similar problems occured with joda convert like
[ERROR] [20:44:25.247] Class not found: org.joda.convert.ToString
Hence I also added
`<dependency>
<groupId>org.joda</groupId>
<artifactId>joda-convert</artifactId>
<version>1.8.1</version>
<scope>provided</scope>
</dependency>`
But note, that I set the scope to provided to prevent these new dependencies to be added to a resulting war file.
However, I still wonder why these errors occur since none of the analyzed classes seem to use these annotations?

Weld OSGi + Apache Felix = can't find packages

I use Apache Felix and weld-osgi for a Java SE application. The problem is that in injected bean I use #ApplicationScoped from package javax.enterprise.context.ApplicationScoped. But there is no such package in weld-osgi-bundle-2.1.2.Final.
This package exist in weld-se but it's not in the OSGi bundle. How can I solve this problem?
I would try running the following dependency as separate bundle:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.1-20130918</version>
</dependency>
(Maven Central link)
Be careful, you need version 1.1-20130918. Version 1.1 does not have OSGi headers in the MANIFEST.MF. You can unzip the jar and check the META-INF/MANIFEST.MF file for OSGi headers like Bundle-ManifestVersion and Bundle-SymbolicName. You can also check here the required packages of that bundle, it's in the Import-Packages header.
How to figure out
Check the dependencies of weld-osgi-bundle on Maven Central (or in its pom.xml). It contains the following:
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-api</artifactId>
</dependency>
This weld-api refers to the cdi-api above which contains the missing annotation:
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
</dependency>
Another way is pressing F3 (Open Declaration) in Eclipse while the cursor in the ApplicationScoped annotation then in the Project Explorer View enable the Link with Editor and it will show that ApplicationScoped.class is inside the cdi-api-1.1.jar.
Finding OSGi version of another jars
You probably need more bundles than this one (transitive dependencies or it was only the first one which stopped the installation).
Not all well-known jar has OSGi headers, like the following one:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
In that case search for the group id on Maven Central. Two results which contain the javax.inject package and have OSGi headers:
org.glassfish.hk2.external
org.apache.servicemix.bundles
If you can't find anything you can convert any jar to OSGi bundle by hand. Actually, you can do this with the weld-se.jar but installing dependencies separately looks cleaner.

Categories