Weld OSGi + Apache Felix = can't find packages - java

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.

Related

Keycloak-Admin-Client in a JPMS/Jigsaw Java 11 Maven application - Dependency Problems

maybe someone has already figured that out or has some tips, I would be really thankful.
I'm trying to use the Keycloak-Admin-Client maven dependency in a modularized Java 11 application. The Application is modularized via JPMS/Jigsaw.
I'm using the version 9.0.2 of the Keycloak-Admin-Client.
<keycloak-admin-client.version>9.0.2</keycloak-admin-client.version>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>${keycloak-admin-client.version}</version>
My current Problem:
java.lang.module.FindException: Module javax.ws.rs.api not found, required by com.fasterxml.jackson.jaxrs.base
But I already have this in my module-info and POM:
requires java.ws.rs;
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>
The javax.ws.rs-api jar is also in my libs folder (module-path):
module-path lib folder
Some additional informations from the jackson-jaxrs-providers GitHub-Repo:
requires static javax.ws.rs.api;
requires static java.ws.rs;
requires static jakarta.ws.rs.api;
https://github.com/FasterXML/jackson-jaxrs-providers/blob/master/json/src/moditect/module-info.java
I hope my problem is understandable, please ask if more details are needed.
Thanks a lot and best regards.
Pierre
The problem was that the javax.ws.rs-api artifact Version 2.1 has a different artifact-id "java.ws.rs-api" not "javax.ws.rs-api".
In Version 2.0.1. are packages missing.
Version 2.1-m08 works.
<dependency>
<groupId>javax.ws.rs</groupId>
<!-- pwi: JAX-RS 2.x has different artifact-id, "javax.ws.rs-api" and Version 2.0.1 has missing packages -->
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1-m08</version>
</dependency>

Glassfish - java.lang.NoClassDefFoundError: org/primefaces/model/UploadedFile

I have a maven based project. This is how the project looks like in eclipse:
So I have separate front and back end that are packed into one EAR. The application were deployed on Weblogic 12c server but now I have to move it to Glassfish. I can deploy to Glassfish and my application is running. But, i got an error message when I invoke a method which use Primefaces UploadFile class. This is the error message:
java.lang.NoClassDefFoundError: org/primefaces/model/UploadedFile
javax.ejb.EJBException: java.rmi.ServerError: Error occurred in server thread; nested exception is:
java.lang.NoClassDefFoundError: org/primefaces/model/UploadedFile
This two dependency:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0</version>
</dependency>
Are in both POM.xml (front and back end) And the maven dependency is in my classpath as you can see in this picture:
My question is, where should I put the Primefaces jar, to make visible for Glassfish in runtime?
You'll either need to add the dependencies to the /lib folder of Glassfish as this is where the container will check for them, or bundle them up with the deployable artifact - in your case the .ear file. As you are using maven there are a couple of plugins available that may help you e.g. the Maven Assembly or Maven Ear plugins.
-Answer based on my comment above.

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!

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?

Incompatible argument to function

My ear application runs well on Tomcat 6 on local window PC, but it cannot run on Weblogic server 10.3.6 on the same PC.
Caused By: java.lang.VerifyError: (class: org/springframework/web/servlet/FrameworkServlet, method: processRequest signature: (Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V) Incompatible argument to function
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2291)
at java.lang.Class.getDeclaredFields(Class.java:1743)
at weblogic.j2ee.dd.xml.BaseJ2eeAnnotationProcessor.getFields.....
Below is part of my pom.xml which might be related to the problem.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>compile</scope>
</dependency>
From your webapp's /WEB-INF/lib folder, remove all Tomcat-specific libraries like el-ri.jar, el-api.jar. It will make your webapp incompatible with containers of a different make/version resulting in this kind of errors.
For more info clear explanation see here
Possible cause 1:
java.lang.VerifyError can be the result when you have compiled against a different library than you are using at runtime.
Refer here Causes of getting a java.lang.VerifyError
Possible cause 2:
How do I import the javax.servlet API in my Eclipse project?
Specially read this paragraph
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. It would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.

Categories