Problems with servlets and maven - servlets

i am trying to build my first servlet using maven but don't know what i should add to the POM so the servlet
I have tried to add the below dependencies to the POM file (I found in one of the posts). The addition enables me to compile my servlet but when i try to run mvn package or to test my JUnits i am getting a ClassFormatError:
Initial SessionFactory creation failed.java.lang.ClassFormatError:
Absent Code attribute in method that is not native or abstract
in class file javax/validation/Validation
The pom.xml is:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<repository>
<id>java.net2</id>
<name>Repository hosting the Java EE 6 artifacts</name>
<url>http://download.java.net/maven/2</url>
</repository>
can someone explain and guide?
Thanks
:-)

At the outset, you may want to follow Senthil's comment above.
As for the error, it is because the specified dependency only has the APIs (method definitions) and not the implementation.
Typically, the implementation is provided by the app server. Hence the application should work in an app server which implements Java EE 6 (like Glassfish).
mvn package should not give any error - it is the test phase before packaging which fails, which you can circumvent, if interested using mvn package -DskipTests.

Related

Maven Dependencies Jar Missing for Slf4j wrapper

I have a project in Maven for ExtentReports. When I run it, the test fail and says.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
When I check the project properties -> java build path -> libraries. ->Maven Dependencies
rxjava-3.0.4.jar (missing)
freemarker-2.3.30.jar (missing)
lombok-1.18.12.jar (missing)
when I go to POM.xml
4.0.0
it says:
could not transfer artifact io.reactivex.rxjava3:rxjava:jar3.0.4
I tried downloading the said jars in mvn repository but it doesnt make any changes.
SLF4j wrapper is generally provided by Lombok.
I recommend you put these dependencies in your pom.xml:
You need the SLF4j wrapper AS WELL AS a dependency for which implementation you want to use. (Logback in this case).
<!--region Lombok Configuration -->
<!-- logger guide https://gist.github.com/stykalin/8c77ad2a705eabddc2424eff0e99d1ec -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<!-- endregion -->
Also, you will need to setup a src/main/resources/logback.xml file. Also, put #Slf4j Lombok annotation at the top of your class files where you need to do a log.info("Message").
IF you still have problems, the command mvn dependency:tree can perhaps help you find dependency conflicts.

ImageJ as JAR library

I tried to google this, looking for code examples but I had no luck.
Anyone knows a place where I can find a tutorial or just examples on how to use Imagej in Java to open and process images?
I was able to get ImageJ in a Jar
What I'd like to do is make a simple image difference processor
Thanks for your help and time
There are two major versions of ImageJ you can program against: the original ImageJ 1.x, and the still-in-beta ImageJ2.
Either way, I strongly suggest structuring your code as a Maven project. By doing this, you avoid manually managing JAR files, and can develop your project in any Maven-enabled IDE (Eclipse, NetBeans, IDEA, etc.) or from the command line.
The ImageJ artifacts (for either v1 or v2) are not yet available on Maven Central, but will be soon. Until then, you'll need to add a <repository> reference to maven.imagej.net. Here is a sample snippet for your pom.xml:
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>1.15</version>
</parent>
...
<dependencies>
<dependency>
<groupId>net.imagej</groupId>
<artifactId>ij</artifactId>
<version>${imagej1.version}</version>
</dependency>
</dependencies>
...
<!-- NB: for project parent -->
<repositories>
<repository>
<id>imagej.releases</id>
<url>http://maven.imagej.net/content/repositories/releases</url>
</repository>
</repositories>
Or if you want to depend on ImageJ2:
<dependency>
<groupId>net.imagej</groupId>
<artifactId>ij-app</artifactId>
<version>${imagej.version}</version>
</dependency>
For documentation of ImageJ1, ImageJ2 and related projects including many of their dependencies, see the javadoc at:
http://javadoc.imagej.net/
For tutorials on how to use ImageJ2, see:
https://github.com/imagej/imagej-tutorials
And for more information on programming against ImageJ1, see:
http://imagej.net/developer/

m2e-webby and provided dependencies with jetty

I have a maven project with some provided dependencies and I am trying to run the application with webby but it give me a java.lang.ClassNotFoundException exception.
In this link it says: "Webby allows to initialize a launch configuration from the configuration of the jetty-maven-plugin".
My question is it is possible to also pass the dependencies defined in the jetty-maven-plugin? If not how can webby resolve the provided dependencies?
Want I did was to create a profile in the pom project:
<profile>
<id>webby</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>version</version>
</dependency>
</dependencies>
</profile>
And then right click on the project -> properties -> Maven and added webby to the Active Maven Profiles

HttpServletRequest getPart(") not found

I don't know why it is not working.
I am trying to accept multipart-formdata in my servlet but request.getPart("") is not showing. I am using glassfish 3.1.2 server. Tha framework used is vaadin for developing application. Can any one help me to fix this problem?
Assuming that your concrete problem is indeed that your IDE didn't show the getPart() method on autocomplete, then that can only mean that the project is not configured as a Servlet 3.0 compatible project. That method was namely introduced in Servlet 3.0.
You didn't tell anything about which IDE exactly you're using and your question history doesn't give any clues as well, so let's assume that it's Eclipse which is rather widely used. In that case, you need to configure it at 2 places, provided that you've correctly associated the Dynamic Web Project with a Servlet 3.0 compatible container as Targeted Runtimes in project's properties (otherwise HttpServletRequest and consorts wouldn't have compiled at all):
In the Project Facets section of project's properties, the Dynamic Web Module version must be set to 3.0.
If your IDE has generated a /WEB-INF/web.xml file, then you need to make sure that its <web-app> root declaration also matches Servlet 3.0, otherwise it would still fail during runtime.
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
I had the same problem. Here's what eventually turned out to be the problem for me. I had a dependency on:
<dependency>
<groupId>com.googlecode.jsontoken</groupId>
<artifactId>jsontoken</artifactId>
<version>1.1</version>
</dependency>
And it turned out jsontoken has a dependency on servlet-api 2.5. So what fixed the problem was simply this:
<dependency>
<groupId>com.googlecode.jsontoken</groupId>
<artifactId>jsontoken</artifactId>
<version>1.1</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
This worked for me
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
To those that stumble on this later, if the resolution suggested by BalusC does not resolve the problem for you please check your build path for provided libraries whose runtime does not match the runtime of your server.
For example if your maven pom.xml contains entries that provide java EE libraries like so...
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>5.0-1</version>
<scope>provided</scope>
</dependency>
... they can interfere with what your IDE perceives as the runtime environment, which will be used to provide code completion suggestions.
The easiest way to track the problem is to figure out where the HttpServletRequest class is sourced from.
maven.. pom.xml.. modify this dependency
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
servlet api 2.5 is not work
I had the same problem, I want to share it with you in case anyone else bumps in the same problem:
In my case I had a j2ee.jar which was automatically downloaded and its version was probably old. It was located under Libraries --> j2ee Runtime Library (stackoverflow doesn't let me add an image so in eclipse - in Project Explorer - expand the project --> Java Resources --> libraries --> J2ee Runtime Library --> j2ee.jar)
I went to the file system where this file was located (you have the location next to the file in eclipse) and replaced it with a newer jar. You can take the jar from here.
Then clean and rebuild the project.
One possible reason, as pointed out in Bjørn's answer, is a conflicting artifact servlet-api (Servlet 2.5 spec).
In my case it was this dependency
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>1.30.10</version>
To find out which dependency compiles the conflicting library just build and print the dependency tree with:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree -Dverbose=true
Part of the output:
Finally exclude the artifact from the dependency
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-appengine</artifactId>
<version>1.30.10</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
Hi friends I too faced simillar problem and I will tell you what I did.
I'm using eclipse juno and Tomcat 7 server. Make sure you use latest servlet API jar. Tomcat 7 will only support dynamic module version 2.5 not more than that. So use higher Tomcat version or lower dynamic module version then it will work.
One more thing: open your servlet api jar and see whether it contains "Part" you can easily check it.

Sonar Analysis for website project - ASP.NET

I was trying to build and analyze a asp.net website project using maven
Command I used :
mvn sonar:sonar -e
I have single pom file in my root directory(same directory where website.sln is stored).
Directory structure:
|---Website (website project file)
|
|---website.sln
|---pom.xml
|
Result of executing command was : Build Error -Embedded error: Unable to execute maven plugin
I did same things and I could successfully build a web application project and console application project(which have visual studio project file). I thing reason for not working for website project is it does not have .csproj file inside website folder.
So how could successfully build and analyze a website project using maven.
Could someone please help me to fix this issue.
this is my pom file:
http://maven.apache.org/maven-v4_0_0.xsd">
<groupId>MindTree</groupId>
<artifactId>webbssite</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<name>Maya</name>
<packaging>sln</packaging>
<url>http://maven.apache.org</url>
<properties>
<!--
NOTE : the versions and parameters may be defined as properties.
Prefer this option to the plugin configuration as it may be accessible to several plugins
-->
<!-- Name of the solution file, located in the same directory as the pom.xml -->
<visual.studio.solution>website.sln</visual.studio.solution>
<!-- Name pattern to recognize the test assemblies, so that unit tests are only launched on those,
and so that those are excluded from code coverage -->
<visual.test.project.pattern>*.Tests</visual.test.project.pattern>
<!-- Version of the .Net tools, which may be 2.0 or 3.5 only -->
<dotnet.tool.version>4.0</dotnet.tool.version>
<sonar.language>cs</sonar.language>
<msbuild.configurations>Debug</msbuild.configurations>
<maven.site.generateReports>false</maven.site.generateReports>
<sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
<sonar.cpd.skip>true</sonar.cpd.skip>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.sonar-plugins.dotnet</groupId>
<artifactId>maven-dotnet-plugin</artifactId>
<version>0.6</version>
<configuration>
<solutionName>website.sln</solutionName>
<language>cs</language>
<toolVersion>3.5</toolVersion>
<Platform>x86</Platform>
<buildConfigurations>Release,Debug</buildConfigurations>
<rebuild>true</rebuild>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
You should rather send a mail to the user mailing list.
Anyway I have the feeling there is a misunderstanding between the "maven dotnet plugin" and the "sonar maven plugin".
The "maven dotnet plugin" provides the ability to build dotnet visual studio solution with maven. It relies mostly on msbuild.
The "sonar maven dotnet plugin" alows to bootstrap a sonar analysis with maven.
Check out the second pom example on the following page : Maven .NET Plugin
Both plugins need to be configured if you want to use maven to build and analyse your solution.
That being said, you should know that neither maven nor those two plugins are mandatory to analyse a C# project with sonar. You could use whatever build tool you want to compile your solution (i.e. msbuild, nant)and you could use the simple sonar java runner to trigger the analysis.
Check out the official wiki and feel free to ask questions to the user mailing list.
You can subscribe easily here : Support

Resources