Maven clean install not including sqlite dependency for executable jar file - java

After using
mvn clean install
and then
java -jar executable.jar
I get this error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>****</groupId>
<artifactId>****</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>****</name>
<description>****</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.23.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tinyjee.jgraphx/jgraphx -->
<dependency>
<groupId>org.tinyjee.jgraphx</groupId>
<artifactId>jgraphx</artifactId>
<version>3.4.1.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>******</sourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>*****</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>******</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Running the program inside intelliJ works without problems. I included it there from the project structure.
I replaced names and directories with ****. This is a school project and I don't want my prof accusing me of providing solutions to other groups in case they find this stackoverflow entry.

Probably you are getting this only when you are running your jar because the dependencies are not available/packaged inside of it.
Try generating a "fat jar" (also known as uber-jar), it will package all your dependencies inside the jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
Documentation related to the maven-shade-plugin can be found in here
Since you are using a runnable jar file, you can follow this section of the documentation related to Executable Jars

Some background.
Maven install never installs dependencies;
it only installs the project that is build via the POM.
The installation of dependencies are a task that you must also perform
if you don't use either a "fat jar" (which I can't recommend) or use
the new spring boot executable jar implementation.
The classic "fat jar" is an amazingly terrible
(but often the only option)
solution for a problem like this.
Consider using Spring-Boot;
they have developed a new,
sane,
version of an executable jar file that includes the dependencies within the executable jar and adds them to the classpath at startup.
This functionality is similar to the functionality of a "war" file when it is added to
a JEE container.
Caveat: I don't work for Pivotal,
I just like much of their work (the Spring Framework).
Edit:
Here is a link to the
Executable Jar Section in the Spring Boot Reference.
It contains some details.

Related

How to create a non-executable JAR file that includes all Maven dependencies

I have a Java project that doesn't have a main file but it has a lot of Maven dependencies.
How I can create a JAR-file that contains my source-code and required maven dependencies?
My pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>NetworkCommunicationAPI</groupId>
<artifactId>NetworkCommunicationAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
</dependencies>
</project>
You can use the maven-assembly-plugin just as you could use it for creating an executable JAR with dependencies.
The only thing you need to change is that you don't need to specify a main class:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
After that, you can create the JAR using mvn compile assembly:single.
This compiles your sources and creates a JAR containing the compiled sources and all dependencies in the compile-scope.
You can use the Maven shade plugin or Maven assembly plugin to create such a JAR.
In most cases, such JARs should be avoided. You create a dependency hell for everyone who uses your JAR because Maven cannot manage the included dependencies any more and therefore conflicts with other dependencies (not from your JAR) may become unmanagable.
On the other, such fat JARs are of little use because Maven or Gradle will find your transitive dependencies anyway, so there is no need to include them into the JAR.

How can I execute a simple jar?

I'm trying to make a simple batch file with maven commands to execute the installation and execute the jar main class.
But the compile JAR does not have the dependencies and I get error
Exception in thread "main" java.lang.NoClassDefFoundError:
akka/actor/ActorSystem
This is my simple script
call mvn clean dependency:copy-dependencies
call mvn package
call cd target
call java -jar distributed-1.0.0.jar
pause
POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>distributed</groupId>
<artifactId>distributed</artifactId>
<version>1.0.0</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor -->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.5.16</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<outputDirectory>src/main/java/resources/lib
</outputDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source/>
<target/>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.ipca.distributed.Implementations</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
What do I need here??
I think the easiest solution to create a runnable jar is to use Maven Assembly Plugin, or Maven Shade Plugin, or One Jar Plugin or even Spring Boot Plugin ( it's not weird if it works ). You can find some more details on how to use each one here.
These plugins are doing exactly you are trying to do with the 2 combined plugins that you have ( maven-dependency-plugin and maven-jar-plugin ).

Creating Windows EXE from Java(FX) through Eclipse [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 4 years ago.
I can't create Windows software through Eclipse. Using "Run" does run the app as it should, but I would like to start it without Eclipse, preferably as an independent Windows app, using a mere double-click.
Although this suggests several approaches, they all require additional knowledge that I do not posses.
I have tried using Launch4J but it also requires additional knowledge. I am astounded that it is so difficult to create a simple Windows-Java-app.
EDIT
I am using this pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<name>baz</name>
<description></description>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>application.Main</mainClass>
<updateExistingJar>true</updateExistingJar>
<skipCopyingDependencies>true</skipCopyingDependencies>
<useLibFolderContentForManifestClasspath>true</useLibFolderContentForManifestClasspath>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
But Eclipse gives this error message:
Failed to execute goal com.zenjava:javafx-maven-plugin:8.8.3:build-jar
the goal being
clean compile package -e
Maven is installed, trying to run it with minimal appendancies.
Launch4j I know it can make an exe Wrapper over the Java, and it is cross platform.
Check here:
http://launch4j.sourceforge.net/
First of all, you can't create "Windows-Java-App". Java applications run on JVM (Java Virtual Machine) which means you wont be getting an exe file. What you need is a JAR file. For info on how to create a JAR file in Eclipse, see here:
https://stackoverflow.com/a/21110972/4440179
For JavaFX follow this guide: https://wiki.eclipse.org/Efxclipse/Tutorials/Tutorial1
I recommend making a Maven project to help you build an executable JavaFX Jar.
Firstly you'll need to make sure that Eclipse has the Maven plugin Help -> About Eclipse IDE and make sure that Eclipse.org - m2e is installed.
Then make a Maven project New -> Other.. -> Maven Project, fill in the basic stuff to create the project then put your source files under src/main/java of this new project.
There should be a pom.xml file in the root of this new project; This file helps manage your project dependencies and build process.
To make easily build an executeable add the below plugins to your pom.xml file to modify the build process.
Then you can just right click your pom.xml then Run As -> Maven Build ... then put clean compile package -e as your goals, refresh your MyProject/target directory and find your Jar under MyProject/target/javafx/app/jarname.jar
Also, if you want, you can look into using the launch4j maven plugin to have maven build the native executable for you, else your Jar will require the user have Java installed but it will still be a double-clickable JavaFX Jar.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0.0</version>
<name>baz</name>
<description></description>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<mainClass>path.to.my.Main</mainClass>
<updateExistingJar>true</updateExistingJar>
<skipCopyingDependencies>true</skipCopyingDependencies>
<useLibFolderContentForManifestClasspath>true</useLibFolderContentForManifestClasspath>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Maven dependencies doesn't get exported into the jar

I have a project which I want to export as jar (for some reasons it's impossible to export it as a runnable jar). I have 3 maven dependencies, gson, io and junit, but when I execute the maven built jar in console it says this:
Check my build path:
I export it this way (Eclipse):
Run as -> Maven build...
(mvn) package
And here is my pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Carlos</groupId>
<artifactId>Buscaminas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Buscaminas</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>res.application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
Also the maven build result:
My project didn't seem to have the correct project structure so I crated a new (maven) project and migrated my packages to the src/main/java folder and then used this:
<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>
<archive>
<manifest>
<mainClass>res.application.Main</mainClass> <!-- Or wherever is your main method-->
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
And executed mvn package
That created a "jar-with-dependencies" jar also with all the recources (images, interface fxml files...).
From what I understand what you need might be this one
mvn install dependency:copy-dependencies
The built jar does not contain the dependencies, you must either provide them in classpath when executing the jar or have build process copy them into your jar, creating a so-called uber-jar. Good way to achieve the latter is using maven-shade-plugin.

Maven custom directory structure - no sources to compile despite specifying sourceDirectory

I am using a custom directory structure and have specified the directory in sourcedirectory tag. But still I get the message No sources to compile. Although the build is successful.
My directory structure:
So instead of src/main/java, I am using java. (And I have a reason to do that, so right now it's not possible to switch to src/main/java)
Here's my pom.xml:
<artifactId>application</artifactId>
<name>application</name>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
</properties>
<build>
<sourceDirectory>java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/old/**/*.java</exclude>
</excludes>
<includes>
<include>java/com/**/*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/old/**/*.java</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.start.Start</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
When I run command mvn clean package, I get following output:
The source is not compiled and resultant jar is empty. All the sources I have referred for using custom directory structure with maven say that using sourceDirectory should solve the problem. But in my case it doesn't solve
EDIT
I am using custom directory structure as using standar directory structure did not work for me. Hers' my question related to that:
Getting error in linking a source folder in eclipse maven
EDIT2:
I am linking source in java directory. That is, on the file system, application->java does not exist, but in eclipse through link source option, I have added the Java source folder from a different directory. Therefore it appears in eclipse. Also I have run maven commands with mvn command line as well as through eclipse.
If I understand your issue correctly, You have an application folder and the actual source (java) folder is from somewhere else in the file system.
And you linked the external folder as java source through eclipse for compilation.
By linking in Eclipse, maven will not automatically know where the source files are. Maven follows standard directory structure for looking up java and test files.
You can use this Build Helper plugin to customize the way maven looks up sources.
An example talked here
This is your problem: <include>java/com/**/*.java</include>
You should not include the sourcedirectory, just the paths relative to it.
Follow these steps of this working example and compare it step by step with your project to figure out what's wrong:
create a folder.
create inside the folder a pom.xml with the following content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yourdomain.yourproject</groupId>
<artifactId>yourapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>yourapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.javadoc.skip>true</maven.javadoc.skip>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.yourdomain.yourproject.content.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
</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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<dependencies>
<dependency>
<groupId>org.jvnet.wagon-svn</groupId>
<artifactId>wagon-svn</artifactId>
<version>1.12</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
create this folder structure inside your root folder:
src/main/java/com/yourdomain/yourproject/content/
create a Main.java in content folder with the following content:
package com.yourdomain.yourproject.content;
public class Main
{
public static void main(String[] args)
{
System.out.println("HELLO");
}
}
go back to your root folder and execute mvn clean install
a target folder will be created with the jar in there.
you can execute it with java -jar target/yourapp-1.0-SNAPSHOT.jar

Categories