Maven: Executable JAR cant find resource - java

I create an executable jar with maven using the plugin
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>pack.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I have all of the dependencies in the src/min/resources folder. After i build the jar, i unpack it with archive manager and i can see all of my dependencies at the root level.
Here is how i am trying to access a resource
ClassLoader classLoader = getClass().getClassLoader();
dataIn = new MarkableFileInputStreamFactory(
new File(classLoader.getResource("trainingData").getFile()));
This runs just fine in Eclipse. But when i try to run the executable jar file, i get
java -jar target/my-0.0.1-SNAPSHOT-jar-with-dependencies.jar -o 1
java.io.FileNotFoundException: File 'file:/path/to/myProject/target/my-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/trainingData' cannot be found
why is JVM looking for File 'file:/path/to/myProject/target/my-0.0.1-SNAPSHOT-jar-with-dependencies.jar!/trainingData' ?
what can i do to solve this?

Related

When run executable jar file i have and SQLException: No suitable driver found for jdbc:mysql://localhost:3306/DENTAL

I am working on a project, my project should be connected with Database, When I run it on NetBeans it works perfectly, But when I am try to run the executable jar file i have an error like this,
"No suitable driver found for jdbc:mysql://localhost:3306/DENTAL"
please note that I added the mysql-connecter.jar to libraries.
Thank you
Please use the following to generate jar file:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>edu.learn.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-runnable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
then run mvn clean package

make a .jar file of a java project(with some classes) in command prompt or Eclipse?

I am trying to create a jar file of a java project. I am using eclipse IDE and maven to make the built. I am trying to execute it through command prompt
You need to use maven-assembly-plugin in your pom.xml if you are looking for an executable jar. Example:
<plugins>
<!-- other plugins -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>PATH_TO_YOUR_MAIN_CLASS</mainClass>
</manifest>
</archive>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Then build with maven: mvn clean install
Then you can run from command prompt: java -jar YOUR_JAR_NAME.jar

no main manifest in maven jar

I've build this project with mvn clean install. Now I have a .jar file but I'm not able to use it with:
λ java -jar flyway-commandline-xxx.jar
no main manifest attribute, in flyway-commandline-xxx.jar
What am I missing?
An executable jar needs a "manifest file" - which is located at META-INF/MANIFEST.MF inside the jar.
You can use the Maven Assembly plugin to produce this manifest file in line with this:
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>dk.tbsalling.ais.cli.AisCli</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>
...
The MainClass (referenced in the manifest file) is the class containing the main(...)-method called when you execute the jar.
You can have a look at https://github.com/tbsalling/aiscli/blob/master/pom.xml for a full, working example.
Use below 2 maven plugins in pom.xml:
maven-assembly-plugin with a goal to package all dependencies.
maven-jar-plugin along with this to add manifest and make jar with the dependencies included by assembly plugin.
Since, in assembly plugin below, appendAssemblyId is false, so final jar name will be with appname.jar only.
You can test assembly using command: "mvn clean compile assembly:single". (or using Jenkins JPaC file then add the command "clean compile assembly:single" under mavenGoals).
See below:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<finalName>appname</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.myorg.appname.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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.myorg.appname.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Maven - Can't Execute JAR

After building a sample mvn project, I added my org.restlet dependencies & Java code.
Then, I successfully built my JAR via mvn install. Finally, I ran into an error when trying to run the JAR.
vagrant$ java -jar target/my-app-1.0-SNAPSHOT.jar
Failed to load Main-Class manifest attribute from
target/my-app-1.0-SNAPSHOT.jar
You need to set the main class in the manifest using the maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.someclass.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Taken from here.
EDIT
If you want to package the resulting jar with dependencies you can use this
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.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>
Taken from here.
If you dont have a manifest in your jar invoking java -jar will not work.
Use this command if you dont have a manifest:
java -cp foo.jar full.package.name.ClassName

Maven jar-with-dependencies don't overwrite files

I have this plugin in my pom.file:
<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>
<mainClass>com.doesntwork.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
This worked just fine until now. Currently two of my dependencies use the same filename of some file in META-INF to store some data. Unfortunately the plugin doesn't merge the files, it simply overwrites the first one with the second, which makes my application crash.
Can I force maven to merge the files that happen to have the same name (but are from different dependencies) ?
The maven-shade-plugin and its transformers I think would be right up your alley.

Categories

Resources