Issue in running executable jar built by jenkins - java

I am having an executable jar ,when I try to run this jar ,it is giving me an error saying one of the bean is not available.But however, if i unzip this jar file
and replace any of the class file and zip it back, the application starts without any issue.No matter which class is replaced, after zipping it back, the jar starts without any issue. Can anyone please tell what could be the issue here?
Please note that When the jar is created in local, it doesnt have any issue. But when I download the jar that had been uploaded to jenkins and try running it, It is giving this issue. As mentioned earlier if I replace any class file inside this non working jar and zip it back, even this jar starts working

Your char in local environment cold be placed in such manner that the executable jar can "see" the necessary dependencies.
You need to package all necessary dependencies into your executable jar in order to make it run when you move your jar to other location. You can utilize maven-assembly-plugin to solve this, just add the following in your maven's pom.xml in your build setup:
<!-- BUILD SETUP -->
<build>
<plugins>
....
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>path.to.your.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Hope it helps!

Related

How to create a JAR file with all classes and byte code in intelliJ or eclipse?

I am new to java and currently doing a java project. This is the instruction on how to submit the project. Could anyone tell me how to do this in either intelliJ or eclipse?
Please submit a Java Archive (jar file containing all java classes you have written). Your jar file should
contain:
a) Contain source code for all classes
b) Contain executable (byte code) for all classes
This question has been already answered here multiple times.
Since you also need to include the sources, you will have to change the resource patterns so that .java files are also copied to the compiler output and are therefore included in the .jar file.
By default .java files are excluded from copying, so you need to remove !?*.java; pattern that excludes them:
!?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj
becomes
!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj;!?*.aj
Don't forget to change it back for your real world applications.
If you need a sample project for IntelliJ IDEA, you can download it from my another answer. It shows a more complicated example where additional dependent jars are included into the project using different ways (single jar and multiple jars).
If you you are using eclipse you can follow one of the below ways as per your requirements.
To Export the project you are working as jar:
1) Right Click on the project you are working > Select Export from context menu.
2) Select Java > JAR File
3) Select the project to export as JAR. Enter the name for the generating jar file and then click on finish.
If you are using Maven do following configurations to the pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.xxxxx.xxxxx</mainClass>
</manifest>
</archive>
<outputDirectory>C:\tmp</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<outputDirectory>C:\tmp</outputDirectory>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

Packing images for SikuliX in Maven

I'm writing an application that utilizes SikuliX, and I'm having problems generating a jar from my code.
I have the images used for my code stored in src/main/resources, and I'm building the jar using this code in my pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.dustinroepsch.leadtimetool.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I open the created JAR with 7-zip, I can see that my images are being added to the root of the jar. I think that's what I want, but I'm not sure.
I'm referencing the images in my my code via
Screen s = new Screen();
ImagePath.add("src/main/resources");
s.hover("1462980188453.png");
And when I launch my code through the Netbeans "play" button it runs great.
However, in when I run the jar with dependencies from the command line, I get the error
FindFailed: Region: doFind: Image not loadable: 1462980188453.png
The images are packed in the jar, and the jar has the sikuli dependencies in side of it. Is there a imagepath for me to add, such that my executable jar will find the images correctly?
Thanks!
I figured out a good solution.
Maven automatically packs everything in your resources directory into the root of the jar.
What you should do is make a folder in your resources directory called "images" (or anything), and place your folder inside of it.
Then you can use ImagePath.add("com.yourcompany.yourpackage.AnyClass/images");
Things will then work as intended

assembled jar won't find hbase-site.xml in the same folder

I've found some useful Q&A here already, but the problem still stucks. I'm using the following plugins in my pom file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>hbase-site.xml</exclude>
<exclude>log4j.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>myfinalname</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest><mainClass>com.company.MainClass</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
as you can see, I exclude the properties files (that are located in src/main/resources), and also add the Class-Path: . to the manifest. But when initializing hbase it can't find my hbase-site.xml which is located in the same folder as the jar file.
when I remove the excludes everything is ok, but I need the files to not be within the resulting jar, making them editable for the user.
As this should work I'm wondering if this is a problem with HBase, maybe HBase is using a different Loader?
How do I get HBase to load the configuration from the file located in the same directory as the executing jar file?
EDIT:
I create the executable jar and when I run it with java -jar, and also put hbase-site.xml into the same folder as the jar is located, when initializing hbase, it doesn't read my configuration
I found the following workaround. starting the jar with the following script
java -cp .:/path/to/myapp.jar com.company.MainClass
makes it read the hbase-site.xml located in the same folder. (this is based on the answers provided for this question here: How to include hbase-site.xml in the classpath) but I'd prefer using the default
java -jar myapp.jar
command. If you know how to achieve this (making the maven assembled jar including the folder in the path), please let me know. in the meanwhile I'll just stick to this workaround.

Problems running executable jar with dependencies

Hey so I have been working on a project that I want to be able to run as an executable jar from the command line. I have been able to create the jar with dependencies using Mavens assembly:single command. My pom looks like this.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<mainClass>org.openmetadata.main.OmadUpdate</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
The build is successful and creates the jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar. I go to my projects target folder in the command line and type
java -jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar
I have also tried
java -cp omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar org.openmetadata.main.OmadUpdate
Unfortunately in each case I am given a java.lang.NoClassDefFoundError: org/openmetadata/main/OmadUpdate. I am confused because I know my main class is in the package org.openmetadata.main and yet it is not found. I find this especially confusing because in my pom I specify that class as my main class. I have tried changing the name of the main class to src.main.java.org.openmetadata.main.OmadUpdate and simply OmadUpdate as well but neither seems to have an effect. Thanks for any help in advance.
I do not see a Class-Path entry in the manifest above, but your very long filename mentions dependencies. If there are jars within this jar file that your program is dependent on, you must enumerate them in the Class-Path section. See Adding Classes to the JAR File's Classpath for more details.
Another option might be to use the onejar-maven-plugin. Unfortunately the usage page is a bit scarce, but the plugin does what is supposed to when configured correctly.
I finally have been able to get this to work by adding the following code to my pom.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.openmetadata.omadupdate.OmadUpdate</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Without the executions tag in the pom along with its children only the maven dependencies will be added to the jar and the classes from the project itself will not be added.

java runnable jar from netbeans 6.9.1

I have a project in NetBeans6.9.1.
It works fine from inside the IDE.
But when I try to run the jar, that NetBeans has automatically created under the dist directory, I get a NoClassDefFoundError
for classes inside my project. What am I doing wrong? Should I be using Ant or something (don't know Ant)
In eclipse I do a "create runnable jar", and the jar runs without issues. Is there something equivalent in NetBeans?
UPDATE: In the dist/myJar, I extracted the jar, and in the manifest, the current path and the root path of my project were missing. I added them manually, and re-created the jar from command line. And it works. But why doesn't NetBeans add these in the classpath of the jar's manifest.I do not understand
UPDATE 2 I found the problem. I think this is a serious NetBeans bug. I had done refactoring and changed the package names from myPackage.model to mypackage.model. But NetBeans did not do it correctly. It indeed changed the name of the package to mypackage as seen in the tree navigator, but the package name inside the file remained as myPackage.
The program executed fine inside the IDE and no errors were reported (although all the classes were declaring as belonging to myPackage and in the tree they were under mypackage), but when I tried to run the jar inside the dist directory I got a class not found exception. Today I noticed that the class was reported as myPackage/model instead of mypackage/model. I looked into the classes and the refactoring completely meshed up everything. I mannually changed the package name from inside my classes from myPackage to mypackage, and corrected all the imports (which were importing myPackage). Is this a known issue of NetBeans????
Thanks
With NetBeans your Java project will be either Maven or Ant based. What you want to do is to create a "fat" jar that has all it's dependencies included. For Maven you can add the following lines to your pom to accomlish that:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>package.and.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
So for Maven it's a two-step thing:
You must tell Maven to produce a "shaded" (including dependencies) jar,
and you have to specify the main class (the one containing the static main(..)method to run.
If you're using Ant you might want have a look a this blog post.
I've been having this same prolem, and all I can say is that it seems to be a glitch with NetBeans 6.9.1, as it worked in 6.8 and 6.9.
For convenience's sake, you can open it in WinZip or WinRar and just change the manifest file from there without having to jar it yourself. That is what I do.
I solved following this post in an external blog:
You have to add two plugins (maven-assembly-plugin and maven-jar-plugin) to the build section of your pom.xml file. You build section would be similar to this:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>(select version, i.e: 2.3)</version>
<executions>
<execution>
<goals>
<goal>attached</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>your.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>(select version, i.e: 2.3.2)</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>your.main.Class</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
NetBeans creates a self sufficient JAR file for your project when you execute the "Clean and Build" command from Menu or Tool Button.
Ideally the classpath of the Jar file shall not contain root folder of the project, NetBeans adds library JARS if you have added them to the project in the Class-Path property in the manifest.mf file automatically.
If you have any custom requirement you can even modify the manifest.mf file which gets included in the built JAR. The manifest.mf file is available from the Files panel in the project root directory.
For making the JAR executable with the java -jar command you shall specify the Main class in the project configuration. (If the project was created through NetBeans project wizard then this class is already defined, otherwise we have to do that manually)
Specify the main class through Menu option as follows:
File > Project Properties > Run (Category) > Main Class (textbox)
Please give details of your project like
Is the project created from NetBeans New Project wizard?
Is the project Ant based or Maven based?
Is the project free form project created from existing source?
This additional information will help me answer the question in specific details. Hope this has helped.
with regards
Tushar

Categories

Resources