Execution of Scala and Maven project - java

I have created a simple maven project of Scala in eclipse's scala ide using below details -
Artifact Id - scala-archetype-simple
Group Id - net.alchim31.maven
After created the project I have modified the pom.xml file.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>scalapoc</artifactId>
<version>0.0.1</version>
<name>${project.artifactId}</name>
<description>Test scala app</description>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.5</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<!-- <arg>-make:transitive</arg> -->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
<archive>
<manifest>
<mainClass>App.scala</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have also added manifest entry inside plugin tag.
<archive>
<manifest>
<mainClass>App.scala</mainClass>
</manifest>
</archive>
I have only one App.scala file and one test file available in project. Build was successful. But when trying to execute the jar using java -jar <jar_name> getting below error -
no main manifest attribute, in scalapoc-0.0.1.jar
When trying to execute the command java -cp scalapoc-0.0.1.jar com.test.scalapoc.App.scala getting below error -
Error: Could not find or load main class com.test.scalapoc.App.scala
Please suggest what to be needed to execute the jar.

try
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.scalapoc.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
.scala is for source only, source is not executable
archive tag is part of the configuration of a plugin
see https://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make
UPDATE 2018-06-05:
To create a standalone jar (without need to defined a classpath with scala-library and other dependencies on command line), you should create a jar that included other classes/jar.
see:
Apache Maven Shade Plugin – Executable JAR and the rest of the doc of the plugin for tuning & configuration
or the old one-jar tool Deliver Your Java Application in One-JAR™ !

Related

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 ).

Eclipse, JavaFX: Code runs in IDE, but not in runnable jar

I'm currently trying to port my project to javafx11. For testing, I'm trying to create a runnable jar file with the hello-world-example from the tutorial page. I included the vm-modules, to prevent the error
Error: JavaFX runtime components are missing, and are required to run this application.
In eclipse, everything works fine, but when I export the project as a runnable jar file, the error returns when I try to run the jar.
When I export the jar-file, I choose the same run configuration as the app and I chose to extract required libraries into generated jar.
EDIT: As it was recommended, I'm currently trying to create a runnable jar with maven. The bhild succeds, but I can't execute it.
The pom.xml looks like this
<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>Mpp_App</groupId>
<artifactId>Mpp_App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Dacemo</name>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<mainClass>Start</mainClass>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>Start</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>9.0.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.15</version>
</dependency>
</dependencies>
</project>
On trying to execute it, I get the error
Could not find or laod main class Start
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Is the Start.java defined wrongly? In the eclipse project structure, the Start.java lies in the default package.

How to fix "JAR will be empty - no content was marked for inclusion!" maven-jar-plugin?

When I run maven-jar-plugin, jar:jar i have this warning and empty jar-file
[WARNING] JAR will be empty - no content was marked for inclusion!
The build is successful but the produced jar-file is empty as the warning says.
How can I fix it? Im trying to find solution in ither questions, but all solutions not working
Here 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>com.actticus.instdbparse</groupId>
<artifactId>instDBParse</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.igor-suhorukov</groupId>
<artifactId>instagramscraper</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.brunocvcunha.instagram4j</groupId>
<artifactId>instagram4j</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.14</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>com.actticus.instdbparse.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<classpathScope>test</classpathScope>
<mainClass>Main</mainClass>
</configuration>
<executions>
<execution>
<id>run-selenium</id>
<phase>integration-test</phase>
<goals><goal>java</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Don't run mvn jar:jar, but mvn clean package. The jar:jar goal just packages - without the lifecycle, there is nothing to package.
Look up "Maven lifecycle" to get more background information.

Error: Could not find or load main class while executing jar file

I have created two java file my maven project one is POJO class and another one is java main class file. I want to make my project as an executable jar file which i want to run externally using java -jar command.
Please find my pom.xml file
<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>ElasticSearchUtility</groupId>
<artifactId>ElasticSearchUtility</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.es.utility.DocumentIndex</mainClass>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Copy project dependency -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
While am executing my jar file using java -jar command. Am getting the error
Error: Could not find or load main class com.es.utility.DocumentIndex
Please find my project structure also :
If there are no classes in the .jar it's probably because maven can't find them. The default path for java classes in Maven is src/main/java, try moving them there and run Maven again.
To add to #kshetline's comment, make sure com.es.utility.DocumentIndex is the correct path to your main class. When you unzip the generated jar, DocumentIndex.class should be in the com/es/utility folder.

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.

Categories