ClassNotFound in Embedded-Jetty and Maven - java

To do some Surface-Tests I start an embedded Jetty for JUnit-Tests. My tests call some pages from the server. When doing this tests from eclipse everything works fine, the classpath is created by "mvn eclipse:eclipse".
When running those tests with "mvn test" the jsp-Compiler raises a lot of ClassNotFoundExceptions: javax.servlet., javax.servlet.jsp and evene some of my self generated classes. All requests directly answered by servlet work fine.
Doing in my testcases something like System.out.println(HttpServlet.class) works fine, too. So the Jetty-JSP compiler seems to habe some "specials" when compiling.
Anybody knows how to persuade Jetty to compile my JSPs?

There is a maven plugin for that:
http://wiki.eclipse.org/Jetty/Feature/Jetty_Jspc_Maven_Plugin

If you look pom of one jetty module you'll see:
<dependency>
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>javax.servlet</artifactId>
<scope>provided</scope>
</dependency>
It dependes on own servlet-api classes.
So maybe if you specify dependencies on real servlet-api in your project pom it will work:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>

After all the problem seems to have been in mavens dependency-management (or what I allowed maven to do).
I had a real mess of javax-artifacts in my classpath (jsp-2.1, 2.2.3, 2.2.1, servlet 2.5, 3.0, 3.0.glassfish-style). So after I cleaned up dependencies everything works fine. Now I understand why jetty from maven-.build didnn't start. Finally I dont understand why at all jetty came up in eclipse ;)

Related

Maven - "Package from transitive module dependency referenced, declare a direct dependency to fix."

I never worked with Maven before, but after having a lot of problems with mockito dependencies in my Netbeans module, I decided to create a new one using Maven and move everything to this new project. After some time doing so, I managed to get the module running just ok.
The problem is that when I use some functionalities that require the Netbeans API I get an exception.
My guess is that the problem is in the dependencies declared in pom.xml. In some of the imports that I use in my application, I get the following message:
Package from transitive module dependency referenced, declare a
direct dependency to fix.
When I clean and build or run mvn clean install in the root directory I get this error:
Project uses classes from transitive module
org.netbeans.api:org-netbeans-modules-projectapi:jar:RELEASE73 which
will not be accessible at runtime.
To fix the problem, add this module as direct dependency. For OSGi bundles that are supposed to be wrapped in NetBeans modules, use the
useOSGiDependencies=false parameter
In the org.codehaus.mojo plugin declaration (which was the only one that had the useOSGiDependencies tag) I tried to change useOSGiDependencies=true to useOSGiDependencies=false but that didn't work. I also tried to change RELEASE73 to RELEASE82, since I am using Netbeans 8.2 but that didn't work as well. I also tried a bunch of other possible solutions that I found on google, but none seemed to work for me.
These are the Netbeans api dependencies that I have in my pom.xml file:
<dependencies>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-project-libraries</artifactId>
<version>RELEASE73</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-api-annotations-common</artifactId>
<version>RELEASE82</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-util</artifactId>
<version>RELEASE82</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-awt</artifactId>
<version>RELEASE82</version>
</dependency>
<dependency>
<groupId>org.netbeans.modules</groupId>
<artifactId>org-netbeans-core</artifactId>
<version>RELEASE73</version>
</dependency>
<!-- ... -->
</dependencies>
I looked everywhere but couldn't find a straightforward answer on how to declare a direct dependency.
Can anyone explain to me what am I doing wrong here?
After digging a bit more, I found that I had to add all the dependencies manually. This answer helped me a lot.
I just lack experience with maven. Basically I googled org-netbeans-modules-projectapi:jar:RELEASE73 maven entered the first link and added the needed dependency to my project.

javax.jnlp as maven dependency

Using JNLP (javax.jnlp) in one java project, I reaized that it is not part of the normal JDK.
As it is a Maven project I would like to add it as a dependency to my POM.
The one dependency I found working is:
<dependency>
<groupId>javax.jnlp</groupId>
<artifactId>jnlp-api</artifactId>
<version>8.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/javaws.jar</systemPath>
</dependency>
But depending on a system path looks bad to me - really bad.
system is marked as deprecated here: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
Is there no other way?
Getting it from repositories as an reasonable up to date version (java 8)?
Or what would be the clean way?

JDBC driver not found while debugging with Intellij IDEA

I have a Maven project that I can successfully compile and run through the command line. When I manually deploy a jar with dependencies in Tomee it works. However when I run the same project through IDEA I get
java.lang.ClassNotFoundException: org.firebirdsql.jdbc.FBDriver
I think this is because my jar with dependencies has the jdbc driver included, but the IDEA run configuration deploys my classes and not the jar, so the driver is missing.
It seems to me that I need to somehow configure IDEA additionally even though I already have this in my pom:
<dependencies>
<dependency>
<groupId>org.firebirdsql.jdbc</groupId>
<artifactId>jaybird-jdk18</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
Am I indeed supposed to configure something else beside the pom?
I created the project without any IDE at first, because I was afraid it wouldn't work outside the IDE.

Jetty wants Jersey, but I'm not using it

Building a relatively simple jetty app, following the instructions here:
http://www.eclipse.org/jetty/documentation/9.4.x/maven-and-jetty.html
I'm not using Jersey, but mvn jetty:run complains about
Provider com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer not found
My pom.xml does not include any reference to Jersey. In fact, it is quite simple:
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jettyVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>6.0.1</version>
</dependency>
What is making jetty look for Jersey?
Search all of your dependencies for META-INF/services/javax.servlet.ServletContainerInitializer files.
The one that has the entry for com.sun.jersey.server.impl.container.servlet.JerseyServletContainerInitializer is the one causing you problems.
Look at your project dependencies (aka <project><dependencies>) and your project's configuration of jetty-maven-plugin to see if that <plugin> has any extra dependencies added to it (aka <plugin><dependencies>).
Well, after much machination, and gnashing of teeth, I think I stumbled about the answer. Whilst learning about maven, I was playing with shaded uber-jars. I had compiled one of the packages as an uper-jar, and installed it. When maven put it all together, it added a bit too much and broke my dependencies. Removing the shaded jar for local installation and just using it for distribution worked just fine.

Cannot load classes from Guava r08 running in Tomcat. Cannot compile from source. Missing javax.annotations

I'm trying to use RichFaces in my learning JSF application. I have set up Maven using
https://repository.jboss.org/nexus/content/groups/public-jboss/
I have included the dependencies
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-components-ui</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.richfaces.core</groupId>
<artifactId>richfaces-core-impl</artifactId>
<version>4.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-components-api</artifactId>
<version>4.0.0.Final</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.richfaces.core</groupId>
<artifactId>richfaces-core-api</artifactId>
<version>4.0.0.Final</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
This has fetched guava-r08.jar.
When I try to run the project in Tomcat7 I see a lot of class load exceptions - failing to load classes that I can see exist within guava-r08.jar along with sac-1.3 and cssparser-0.9.5.
If I try to use the source instead - taking source from guava-r09 - Eclipse tells me that it cannot find classes such as javax.annotation.Nullable. Problem is neither can I!
Where can I find these classes, or am I taking the wrong approach from the start?
Thanks
Richard
Maven configuration for RichFaces dependencies
Tomcat fails to load these classes, because RichFaces' dependency on Guava has a runtime scope. Contrary to the compile scope, these dependencies are not added to the classpath when compiling. You must include them yourself.
To achieve this, you should include richfaces-bom in the dependency management section of your POM, as explained in this JBoss wiki article. This will include Guava and all other required dependencies RichFaces might need.
This is the "Bill of Materials" (BOM) pattern. The JBoss wiki explains this pattern far better than I would, and links to other articles on the subject.
javax.annotation.Nullable warnings in the Guava source
These occur because Guava uses JSR 305 annotations. It is not required to depend on the JSR 305 jar when using Guava, because annotations do not require to be present on the classpath once compiled. Of course, if you want to use #Nullable and other such annotations in your code (you definitely should), you'll need to add a dependency on the JSR 305 jar.
I have started from scratch using instructions at jboss.org
I installed Maven 3.0.3
I copied the supplied settings.xml to my .m2 directory and added the JBOSS section from 1
I used the command line to create the project:
mvn archetype:generate -DarchetypeGroupId=org.richfaces.archetypes \
-DarchetypeArtifactId=richfaces-archetype-simpleapp \
-DarchetypeVersion=4.0.0-SNAPSHOT \
-DgroupId=uk.m0rjc \
-DartifactId=jsfplay
I then built the project. I had to remove the previously downloaded guava jars from my m2 repository because they were corrupt - perhaps my initial problem.
mvn clean package
I copied the WAR file it produced to Tomcat and navigated to the sample page at localhost:8080/jsfplay-1.0-SNAPSHOT/
I had some issues making the project work in Eclipse. These may be due to my setup from previous experiments.
I used the "Import -> Maven -> Existing Maven Project" to import it
I had to switch the project to JDK 1.6 and 1.6 compatibility mode.
I was unable to use the JavaServer Faces facet. This does not seem to matter.
I had to set my Server Profile to use the right JDK
I had to map *.xhtml to the Faces Servlet in web.xml. It may have been corrupted when I pressed a wrong button to accept a JSF addin.
Then it worked!!
Now to try porting my existing code to the new project.

Categories