Server can not find class in .ear - java

I have some problems with my .ear file. The structure of the file is:
app.ear
|-xxx.jar
-yyy.jar
-zzz.jar
-ektorp.jar
-app-ejb.jar
-app-web.war
|-WEB-INF
|-lib
|-xxx.jar
|-yyy.jar
|-zzz.jar
|-ektorp.jar
When I try to deploy my application, I get ClassNotFoundException, with class wihch is in ektorp.jar. This file is used by ejb module.
I also don't know why these jars are doubled? In ear and in war module are the same .jar files.
Ear is built by maven2.

When I try to deploy my application, I get ClassNotFoundException, with class which is in ektorp.jar. This file is used by ejb module.
Does the EJB-JAR reference ektorp.jar in the Class-Path: entry in the manifest (see Packaging EJB 3 Applications for more background on this)? The FAQ explains how you can configure the plugin to generate a Class-Path: entry in the manifest:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.2.1</version>
...
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>
Just in case, do you know that you can package EJBs in a .war with Java EE 6 (the difference is that all classes are loaded with the same classloader when using the .war packaging)? If you don't have strong modularization requirements, the .war packaging is simpler.

Related

Netbeans 8.2 and maven - no main manifest attribute error

i am using netbeans 8.2 to create a simple java project. i've chosen Maven-Java Application. I've created a simple hello world java class. I've selected the java class as the main class from Properties -> Run and if i run the project from Netbeans, it print the hello world. After that, i do "Clean" and "Build with dependencies" the editor create a target folder with the jar file. When i go to that folder and execute "java -jar XXX.jar", i get the no main manifest attribute error. Do I have to manually update the POM.xml file? Am I missing a step?
I am answering my own question.
Apparently you have to manually update the pom.xml file. I've added the dependency for maven-jar-plugin and added below section to the pom.xml file.
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.mypackage.XXX</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Combinate .jar library with my .jar

I have a project in javaFX and I'm using ControllerFX Library , the problem here in the .jar file it's necessery to keep .jar file of the library with my .jar project.
is there a way to combine .jar of library with .jar of project using intellij idea ?
does Maven help in this situation? (I don't know exactly what maven can do)
thanks for answers
The jars that you use with your project are called as the dependent jars. You can bundle them while creating a jar of your own project.
Intellij Approach
File
|
Project Structure
|
Artifacts
|
create new artifact choose --> jar --> From modules with dependencies.
Next goto
Build
|
Build artifacts --> choose your artifact
Maven Approach
Create a Maven project and keep the packaging as jar.
Add the executable jar plugin and add your Main class details and your dependent jars
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Java dependency is absent in MANIFEST.MF

I have some problems with some of my dependencies, declared in pom file dependencyManagement section: dependency is not included to MANIFEST.MF file. This dependency is used in web project in section. However, there is no related declaration about this dependency in MANIFEST and deployed app fails with exception. How to guarantee adding this dependency to MANIFEST file?
Thanks.
Maven does not put dependencies in the MANIFEST.MF file. If you have web project which means having a war file all dependencies will be packaged into the war file. Furthermore it's not needed to put the dependencies into the MANIFEST.MF file.
If you really like having the dependencies into your MANIFEST.MF file you have to configure the maven-war-plugin like this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
If you define a dependency only in dependencyManagement it will not be a real dependency. This is only a definition of the version to be used.
To make a dependency really be used to your project you must define the dependency in dependencies.

How to package EJB3 with external domain-jar

I am facing another problem in my little test-webapp.
I have an EJB module (created via maven-pom) that basically wraps the data-access, so all it does is some DAOs implemented as Stateless-SessionBeans. My domain-model (simple POJOs with JPA2 annotations) is located in another, simple java, project that will be packaged as jar-file.
When I create the enterprise-archive, maven only puts the webapp and the ejb-module into the application.xml and even when I change this manually the ejb-module cannot find the classes from the domain-module at deployment time.
I read something about that an ejb has to have all its dependent jars within its own archive but I cant believe that since this domain-module is used by other projects as well.
How would I package this (or set it up in maven) so my ejb can load classes from an external jar?
thanks
If I remember well, simply generate a manifest with a Class-Path entry in your EJB-JAR:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.0</ejbVersion>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
And add your external jars to the EAR. To do so, declare them as jarModule in the Maven EAR plugin configuration. See modules configuration.

how to configure <manifest> only once in pom.xml in a desktop application

I'm making a swing application with maven and I try to keep the pom.xml under tight reins (this file tends to become a pile of junk after pasting there whatever we find in google).
My pom is with jar and I use the maven-assembly-plugin with "jar-with-dependencies" descriptor.
So I need to define twice the part of my project (main class and versions), once for the normal jar and the other once for the jar-with-dependencies.
I think the problem comes from the jar-with-dependencies assembly descriptor, instead of unpacking the normal jar and fusion the manifests, it creates a new jar from ${project.build.outputDirectory} wich smells strange to me.
Does anybody have a compact and elegant idea to avoid this repetition of my manifest configuration ?
Thanks,
Nicolas.
You can configure both the assembly and jar plugins to use a [particular manifest file. From the Creating an Executable Jar section of the assembly docs:
As you've no doubt noticed, the Assembly Plugin can be a very useful way to create a self-contained binary artifact for your project, among many other things. However, once you've created this self-contained jar, you will probably want the ability to execute it using the -jar JVM switch.
To accommodate this, the Assembly Plugin supports configuration of an element which is identical to that supported by the maven-jar-plugin (see Resources). Using this configuration, it's easy to configure the Main-Class attribute of the jar manifest:
Note the manifest you define is merged with the generated content. From the referenced jar page:
The content of your own manifest file will be merged with the entries generated by Maven Archiver. If you specify an entry in your own manifest file it will override the value generated by Maven Archiver.
The following configuration binds the assembly-plugin execution to the pre-integration-test phase (the next phase after package) and will also include the MANIFEST.MF defined under src/main/resources/META-INF.
So following this approach, you can define the common manifest contents in your MANIFEST.MF and have them be merged into the final manifest.
contents of src/main/resources/META-INF/MANIFEST.MF
Created-By: Me
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0
Update:
Based on the comments about using addDefaultSpecificationEntries, the suggested approach will work in conjunction with that specification on the jar produced by the jar plugin. However it looks like the assembly plugin doesn't yet support merging the additional declarations. The jar plugin merges the manifest from src/main/resources with the additionally specified values to give this content on my test project:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Me
Built-By: Rich Seller
Build-Jdk: 1.5.0_15
Specification-Title: Unnamed - org.test:test:jar:1.0.0
Specification-Version: 1.0.0
Implementation-Title: Unnamed - org.test:test:jar:1.0.0
Implementation-Version: 1.0.0
Implementation-Vendor-Id: org.test
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0
Whereas the output from the assembly-plugin is:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Me
Foo: bar
Main-Class: com.foo.bar.MyMainClass
Bundle-Version: 4.0.0
config:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>jar-with-deps</id>
<phase>pre-integration-test</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestFile>${manifest.file}</manifestFile>
<!--these properties are ignored-->
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifestFile>${manifest.file}</manifestFile>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
...
<properties>
<manifest.file>src/main/resources/META-INF/MANIFEST.MF</manifest.file>
</properties>

Categories