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
Related
For my project, I have used the below configuration to generate the thin jar
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
com.aaa.bbb.MainClass
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
</plugin>
My jar is created without the dependent jars and using maven copy commands all the jars get copied into the respective folders.
Going forward I need to separate some selected jars from external jars, I have below two options
I want to add some selected jars(some other jars from the related project) to be a part of the main jar and exclude the external jars like hibernate, springfox, etc.. creating a semi-fat jar. But don't know to add some selected jars to be a part of main jar.
The selected jars can be placed in a folder inside the target folder(the main jar is still a thin jar) and then use the maven copy command to download only the external jars.
If you have any references or links that will also be very helpful.
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!
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>
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.
I'm trying to create automated solution for building with maven. My vision is to have a Maven build, which creates JAR file from my project and then just copies all the dependencies as JARs to some sub-directory in "target" folder.
I do not want to use Shade or Assembly (so I do not want to extract the content of other JARs and include it in one "super-JAR", because the project is more complicated and it breaks when I'm including all the JARs in one file).
How can I do such build POM?
I don't see here any problem. Just create maven pom.xml with <packaging>jar</packaging>
By default it should not pack into your jar all dependent libraries.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Related with your latest comment, use this plugin to add the main class in the manifest:
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.YourMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
You can use the maven-assembly-plugin to create a .zip file from a selection of other files in your directory. I have used this method to create distribution zip files for a project. There are several examples available in the maven-assembly-plugin documentation.