Spring Boot executable jar "invalid or corrupted jar file" - spring

I create fully executable jar by spring-boot-maven-plugin(v1.3.6) this way:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
When I start result jar like executable (like "./app.jar", not via "java -jar app.jar"), from target directory - the app is correctly starts and works, but if I copy this jar to another directory - I get error: "Invalid or corrupt jar file /home/user/Spring". (I use Linux Mint OS, if this important)
When I start it by "java -jar" command - it works properly in any directory.
How can i copy spring-boot executable jar correctly?

Solved. The problem was in the "space" symbol in the directory name.

Related

Include and use native lib files (.so, .dylib) in "executable JAR"

I have a multimodule Spring Boot project with Maven. I am using spring-boot-maven-plugin to package this application into an executable JAR. This JAR is deployed to PCF (Pivotal Cloud Foundry).
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>my.AppMain</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Now I would like to use PDFTron library - they provided me with a JAR ,.so and .dylib files (.dll too but I dont need those). They said that the JAR has to be in the same directory as the so and dylib files in order to work properly.
I tried to use this library in the project but I cant make it work. I am running out of ideas, I believe I have read almost all possible posts to this topic.
I have tried:
load each of this files to maven repo, include each one as (dylib, so and pdfnet.jar), but it resulted in error message: Caused by: java.lang.UnsatisfiedLinkError: no PDFNetC in java.library.path:
put all the files (dylib, so and pdfnet.jar) into the src/main/resources, include just provided PDFNet.jar as but it resulted in error message: Caused by: java.lang.UnsatisfiedLinkError: no PDFNetC in java.library.path:
Only way that worked was when I used maven-dependency-plugin to unpack the PDFTron dependencies into the target directory and update the -Djava.library.path to point at this created directory - but this can be done only at my local machine, because PDFTron dependencies were unpacked at "package" phase. To the PCF we are depolying only the executable JAR, so there is no target directory at PCF, so this was not an "production ready" option.
My question is, how can I include these files into this "executable JAR" so it will work properly ? Is there a way to unpack them at runtime ? Or do I have to use other plugin than spring-boot-maven-plugin ?
I can modify the -Djava.library.path at PCF, but I would like to avoid deploying another zip/jar/folder along with this JAR (basically I want to avoid changing the CI/CD deploy script as it is not under my control - and right now we are deploying only the executable JAR)
I am using java version "11.0.8" and spring-boot version "2.2.8.RELEASE"
You only need two files, per-platform.
PDFNet.jar is for all platforms.
libPDFNetC.so for Linux
PDFNetC.dll for Windows
PDFNetC.dylib for macOS
You can see in the samples, there are RuntTest.[sh|exe] files that show the settings that you can use. For example
#!/bin/sh
TEST_NAME=AddImageTest
javac -cp ../../../Lib/PDFNet.jar *.java
java -Djava.library.path=../../../Lib -cp .:../../../Lib/PDFNet.jar $TEST_NAME
So -Djava.library.path= points to the folder containing libPDFNetC.so and/or PDFNetC.dll.
It sounds like you have deployment specific issue with your tech stack, but I assume there is a way, same as how you would deploy any images/fonts or other resources. Though maybe your tech stack puts some restrictions on where java.library.path can point to, but that should be in the documentation supplied by what you are using.

how to add jar dependency to xtext maven build

What is the correct way to use a maven jar file in my xtext dsl project?
What I have tried is this:
use the maven-dependency-plugin in the pom.xml file of the *.dsl project to download the .jar file from a maven repository into the ./lib/ directory. This is done as early as possible in the build process: in the maven validate phase
in MANIFEST.MF: add the jar to the classpath: e.g. Bundle-ClassPath: ., lib/value-2.5.6-annotations.jar
in build.properties: add it to the bin.includes
The problem is, that the build only works when I call mvn install twice.
The first time, the .jar file is downloaded to the lib directory as expected (early in the build process), but then the build fails because it cannot resolve the types in my jar file.
When I then run mvn install again (the .jar file now already exists in the lib directory before the build), it works fine.
Any ideas how to resolve this?
Short answer
Currently it does not work as expected, because of bugs in Tycho
#353889: Defer target&dependency resolution to the normal build
#393978 maven-dependency-plugin:copy-dependencies goal does not work reliably with Tycho projects - "error copying ....jar.jar"
Long answer
Here is what I did to make it work (for now) in the xxx.dsl project:
pom.xml file
I use the maven-dependency-plugin to download the jar file in the maven validate phase (as early in the build as possible) to the lib directory.
Note, that I use stripVersion=true so that the file in the lib dir is called value-annotations.jar (and not value-2.5.6-annotations.jar). If I ever want to update the version in the future, I only need to update it in one place in the pom.xml file.
The jar file must also be specified as a dependency, because otherwise the users of the dsl plugin cannot build the project: i.e. the generateXtext task of the xtext-gradle-plugin will fail because it cannot find the classes in the jar file.
Relevant pom.xml code:
<project ...>
<properties>
<xtextVersion>2.13.0</xtextVersion>
<immutablesVersion>2.5.6</immutablesVersion>
...
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-libraries</id>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutablesVersion}</version>
<classifier>annotations</classifier>
<outputDirectory>lib</outputDirectory>
</artifactItem>
</artifactItems>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
...
</build>
<dependencies>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutablesVersion}</version>
<classifier>annotations</classifier>
</dependency>
</dependencies>
</project>
META-INF/MANIFEST.MF file
Add the jar file to the Bundle-ClassPath, so that we can use it: e.g. in the DslJvmModelInferrer.xtend
Add the package of the jar file to Export-Package, so that these files can be accessed by the xxx.dsl.tests project
Relevant parts of MANIFEST.MF:
Bundle-ClassPath: ., lib/value-annotations.jar
Export-Package: xxx.xtext,
...
xxx.xtext.validation,
org.immutables.value
build.properties file
Add the jar file to the bin.includes so that it will be copied to the generated jar file (in the target directory):
bin.includes=model/generated/,\
.,\
META-INF/,\
lib/value-annotations.jar,\
plugin.xml
Build
Now the build works in Eclipse.
On the command line (and in my continuous integration server script), I must execute maven twice (because of the mentioned bugs):
mvn verify (to download the jars)
mvn install

How to include external configuration resources to classpath in Spring (Spring Boot)?

I have 3rd party library that is configured by placing properties file on the root of the classpath. That library is using getClass().getResourceAsStream("/file.properties") to load that file. As it is 3rd party, it is unmodifiable. I have placed that configuration file into external resources directory (not to be mistaken with resources from eg. Maven's or Gradle's directory structure.
Directory structure is like this.
How to run/configure Spring boot to include content of resources directory to the classpath so getResourceAsStream wil work?
On SE application I would simply do java -jar myApp.jar with classpath in MANIFEST and that would work.
EDIT:
Just a word of clarification - putting configuration file inside project resources (along sources) is missing the whole point. I want to keep configuration externalized.
Here is how you can do it:
1.- Change your spring-boot-maven-plugin configuration to enable the Spring Boot PropertiesLauncher:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>
2.- Launch your Spring Boot Application setting the location of the external properties file:
java -jar -Dloader.path=PATH_TO_PROPERTIES_FOLDER spring-ms-0.0.1-SNAPSHOT.jar
Assuming this folders:
/home/user/
|--- file.properties
|--- spring-mg-0.0.1-SNAPSHOT.jar
You should launch it like this: java -jar -Dloader.path=/home/user spring-ms-0.0.1-SNAPSHOT.jar

Spring boot application with apache axis

I am trying to run a spring boot jar which has axis2 dependencies in it. I am using spring boot maven plugin to build the jar (with dependencies). When I try to run my jar, I get the following exception in my console:
org.apache.axis2.AxisFault: The G:application\myapp\target\myapp.jar!\lib\axis2-1.6.1.jar file cannot be found.
at org.apache.axis2.deployment.repository.util.DeploymentFileData.setClassLoader(DeploymentFileData.java:111)
at org.apache.axis2.deployment.ModuleDeployer.deploy(ModuleDeployer.java:70)
at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:813)
at org.apache.axis2.deployment.RepositoryListener.loadClassPathModules(RepositoryListener.java:222)
at org.apache.axis2.deployment.RepositoryListener.init2(RepositoryListener.java:71)
at org.apache.axis2.deployment.RepositoryListener.<init>(RepositoryListener.java:64)
at org.apache.axis2.deployment.DeploymentEngine.loadFromClassPath(DeploymentEngine.java:175)
at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:135)
at ...
I then checked the structure of my jar. It has lib folder inside it, which contained all the jars (including the above mentioned axis jar). Attached is the screen shot of lib folder.
Following are the solutions which I have tried:
Placed axis jar in the same directory as application jar.
Created lib folder in the same directory as application jar and placed axis jar inside it.
Modified manifest file to include Class-Path: /lib/
None of the solutions has worked. However, when I run the application class in eclipse, the app starts and runs perfectly. But, once I create the jar, nothing seems to run.
Can anyone please help? Thanks in advance.
It looks like Axis can't cope with being run from a jar that's nested within another jar. It works fine in Eclipse as the Axis jar is available directly on the filesystem rather than being nested inside your Spring Boot application's jar file.
You can configure your application's fat jar file so that Spring Boot knows to unpack the Axis jar into a temporary location when it's run. If you're using Maven:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<requiresUnpack>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
</dependency>
</requiresUnpack>
</configuration>
</plugin>
</plugins>
</build>
And if you're using Gradle:
springBoot {
requiresUnpack = ['org.apache.axis2:axis2']
}
See the Spring Boot documentation for some further details.

File name encoding in jar

My maven run creates different files in the target directory and compresses them into the jar file.
In case special characters like an Ü exist in the file names, the file names in the jar archive are not correctly encoded and showed as ├£. (The file contents are not affected)
As the files are correctly shown in the target directory, the issue must be caused by maven's jar:jar.
The interesting thing is that if I use the unzip command in Linux, the files are extracted with correct name, if I use Windows Explorer or 7zip in Windows, the names are not correct.
I had the exact same problem and upgrading my maven-war-plugin version solved the problem I think you should do the same with your maven jar plugin
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
</configuration>
</plugin>
</plugins>

Resources