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.
Related
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'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
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.
i am creating a standalone java application with maven, and i am including the dependencies in the jar file with maven-dependecy-plugin as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>theMainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-my-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
this includes the dependencies in the generated jar file in a lib folder, and the jar runs and works fine, but the issue is in the other generated jar file appname-1.0-jar-with-dependencies.jar.
ISSUE: i am not sure if it's an issue or not, but i noticed in target folder in the generated appname-1.0-jar-with-dependencies.jar, that it contains duplicate application files like:
all sql,property files,xml files exists twice.
all java classes .class file exists twice.
there are lots of overview.html and license.txt files related to the dependencies.
i am not sure if that's feels right or not, also i need someone to clarify for me what is the importance of this generated jar file, since i am not familiar with this plugin.
please advise, thanks.
Since you have mentioned jar-with-dependencies, I assume you are using maven assembly plugin to assemble your project artifacts along with the dependant jars into a single jar.
I suspect that your project artifacts are getting into the jar-with-dependencies twice - due to a property useProjectArtifact of dependencySet which is true, by default. You can set this property to true in your assembly descriptor and see if it addresses your issue.
In the specific case above, maven dependency plugin does not seem to be doing anything useful. maven assembly plugin automatically packages all its dependencies into a single distribution as per configuration.
But do note classpath issues if you create an executable jar-with-dependencies. You may want to create a zip or tar.gz instead.
The configuration used above is the default and does not allow for customization. You may want to use an assembly descriptor file, where you can set the property mentioned earlier or other options.