How to package a library in to a .jar file? - java

I want to put jar into my generated jar file .
i want to include a new dependency (external.jar) for my executable jar file.the external.jar do not contain a main method.

Use jar cf jar-file input-file(s). I recommend reading http://www.skylit.com/javamethods/faqs/createjar.html , https://docs.oracle.com/javase/tutorial/deployment/jar/build.html and Java creating .jar file

If you are using mavan, you can add the next to pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

Related

Making absolute reference to a classpath in Maven

I have a customized jar that the application uses. Instead of installing jar in maven I want to make reference to it by defining absolute location for that jar in the manifest file. I am using the code shown below to update information in manifest file. However, I am not sure how can I reference to the jar location that uses absolute path like : D:/Shared_library/Test.jar . This might not be a good practice but I want to see if there is anything like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
You can add it using <manifestEntries> tag.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>file:///D:/Shared_library/Test.jar</Class-Path>
</manifestEntries>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

mvn jar file : library include failed

I did mvn package and ran my file by doing java -jar target\output.jar
All the jar libraries specified in the dependency of pom.xml are not included. Do advice what's wrong below. Thanks!
Below is my pom.xml to allow jar file generation.
<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.workspez.psg.letrikEtara.PlanetGroupLetrikEtara</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Try and compare/complete your declaration with the ones in "Including dependencies in a jar with Maven", like adding the executions section.
Or, if this is not working, consider the maven shade plugin, whic can achieve a simlar goal.
Got it working. This was missing:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
To the following should work:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.workspez.psg.letrikEtara.PlanetGroupLetrikEtara</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Maven Assembly Plugin : Resources are copied at root of jar

I'm using the maven-assembly-plugin to build a Java 8 project, and package it as a JAR file.
I've got several resources located at src/main/resources (usual maven resources directory).
When the JAR is created, all resources files under src/main/resources are copied at the root of the JAR.
In the code, I'm trying to open files using FileInputStream that are located in src/main/resources, and it works great when running the project without packaging it as a JAR file. However, when I'm running the project from the JAR file, I get many FileNotFoundException because my resources are not anymore at src/main/resources, but at ${project.basedir} instead.
I'd like to have them located at src/main/resources in the JAR file. Is it possible?
My build configuration is as follow:
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>EmployProject.TestAlgo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>TestAlgo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
Edit
If it's not possible, what, in my code, should I use to access my resources files?
Thanks to #khmarbaise and this post: Accessing a Java Resource as a File
I figured out I was using the wrong API to open resources.
Resources in a JAR are not files, thus I changed:
XMLDecoder xmlDec = new XMLDecoder(new InputFileStream(this.getClass().getResourceAsStream(fileName))
to:
InputStream in = EnumTools.class.getResourceAsStream(fileName);
XMLDecoder decoder = new XMLDecoder(in);

No main manifest attribute error when running jar

I have create a maven project. It is running fine. Now i want to run my code through jar. After maven build i got a jar file in .m2 folder. When I try to run this jar using
java -jar "jar path"
getting no main manifest attribute, in "jar path".
My POM.xml is
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>main.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Please suggest how to get over rid of the problem.
Now i want to run my code through jar
exec-maven-plugin is for executing a program during a maven build.
You don't want it.
And as a side note, you don't have specified any goal for it.
So, it will do nothing.
You want to package your jar in a way that it is executable.
So use rather the maven-jar-plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
To create a JAR with dependencies specified in the pom, instead of, use the maven-assembly-plugin with the jar-with-dependencies descriptorRef:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

Maven: Not copying files from src/main/resources to the classpath root - Executable Jar

I am building an executable jar with Maven using the Assembly plugin. I have a resource file(.xml) which is placed at src/main/resources. When I build the executable jar, the file is not getting copied into the jar - Checked by unpacking the jar.
Here is my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</execution>
</executions>
</plugin>
I am trying to call the following resource which is kept under src/main/resources:
reader = Resources.getResourceAsReader("mybatis-configuration.xml");
Getting the following exception while executing java -jar InterfaceRunner.jar
Exception caught while reading or parsing the mybatis config xml :java.io.IOException: Could not find resource mybatis-configuration.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:108)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:95)
at org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:153)
Has anyone faced a similar issue before? Looking for your help, Maven gurus..
You can try replacing your Configuration as follows;
<configuration>
<archive>
<manifest>
<mainClass>xx.com.xxx.xxxx.xx.xxxx.InterfaceRunner</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>InterfaceRunner</finalName>
</configuration>
And then
mvn package
By default, maven-assembly-plugin makes TWO jars, not one (with phase package). Make sure you are running the one called <artifactid>-<version>-jar-with-dependencies.jar
If this is not the problem, are you doing anything weird in your pom here:
<build>
<resources>
<resource>
<targetPath><!-- here?? --></targetPath>
</resource>
</resources>
<!-- etc. -->
</build>

Categories