Run Java Maven project with Linux command line - java

I have a Java project that prints "Hello world !". It runs well under Eclipse/Windows and on a Linux server with the command:
java MyClass.java; javac MyClass
Now that I have converted the project to a Maven project it's still running fine in Eclipse, but I can't find how to run it with a Linux command. I tried many answers that I found on forums but none work for me.
Here is an example of what I tested:
mvn package install;
cd target;
java -cp myApp-0.0.1-SNAPSHOT.jar mypackage.Mylass;
This results to an error:
Error: Could not find or load main class mypackage.Mylass
So, how can I run the Maven code on Linux without generating a jar file or at least make it work with a command line?

i finally managed to make it work thanks to all the answers.
here is what i did
i moved my Main class to the outside of packages and deleted/regenerated a new POM file with
<manifest>
<mainClass>Myclass</mainClass>
</manifest>
but i got some dependency errors so i generated a jar file with the dependency by adding maven plugin jar-with-dependencies
so here is what the build part of my POM file looks like now
....
</dependencyManagement>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>MyClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>MyClass</mainClass>
</manifest>
</archive>
</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>
</plugins>
</build>
and here is how i execute it .
rm -rf target/ #delete old generated files
mvn install package # generate new jar files
java -jar target/App-0.0.1-SNAPSHOT-jar-with-dependencies.jar # execution
i hope this can save someone else's time
thanks for your help

You've specify a main class in the manifest of myApp-0.0.1-SNAPSHOT.jar
Add the next snippet of code to your pom file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>mypackage.Mylass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
This's the complete pom.file
http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.mycompany.app
my-app
jar
1.0-SNAPSHOT
my-app
http://maven.apache.org
junit
junit
3.8.1
test
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.mycompany.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Can you confirm the class name you can trying to run ?
As per your comment, originally ran the class using below command.
java MyClass.java; javac MyClass - here the class name is MyClass
After maven install, it should be invoked using
mvn package install;
cd target;
java -cp myApp-0.0.1-SNAPSHOT.jar mypackage.MyClass;

Related

Create Executable Jar with Maven (maven-assembly-plugin) Not Working vs. Eclipse Export Function

I am trying to create a runnable jar using maven (maven-assembly-plugin) which includes all required libraries in a generated jar file, maven successfully creates .jar file however when executing it seems some libraries are not being correctly loaded (specifically it is trying to load com.ibm.mq.jms classes it throws a DetailedJMSException: JMSCC0091), however when exported in eclipse as a runnable jar it successfully creates a runnable jar and when executed no exceptions are thrown.
Just wondering how I can use maven to do the same as the eclipse export runnable jar function without the exception?
Note: I have also tried a similar approach using maven-shade-plugin with the same exception thrown.
Here is the relevant build information in my pom.xml:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<executions>
<execution>
<id>make-executable-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.tapcons.execute.Execute</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
seeing other questions related to this you may should remove ur plugin from the pluginmanagement.
see here:
maven assembly plugin is not working with pluginManagement

Java 9 Maven module not found in console

I am building a Jigsaw module using Maven.Here is a simple class inside of it
public class Welcome {
public static void main(String[] args) {
System.out.println("Java 9 Modular Welcome");
Module module = Welcome.class.getModule();
System.out.printf("Module name: %s%n",module.getName());
}
}
module-info.java
module org.abondar.experimental.intro {
exports org.abondar.experimental.intro;
}
Maven compiler plugin set up
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<jdkToolchain>
<version>[1.8,9)</version>
</jdkToolchain>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
And Jar plugin
<build>
<sourceDirectory>src/main/org.abondar.experimental.intro</sourceDirectory>
<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>org.abondar.experimental.intro.Welcome</mainClass>
</manifest>
<manifestEntries>
<Class-Path>./lib</Class-Path>
<Module-Path>./mods</Module-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
So in IDE this code works as expected. But in console getModule returns null. I believe I have misconfigured one of Maven plugins because in console module is not in module path. So what exactly is wr
Alright, after sometime and careful reading I understood my mistake: I didn't run jar correctly.
So here how should it be:
java --module-path target/Intro-1.0.jar -m org.abondar.experimental.intro/org.abondar.experimental.intro.Welcome
Instead of just running
java -jar target/Intro-1.0.jar

No main manifest attribute error when running jar

I have create a maven project. It is running fine. Now i want to run my code through jar. After maven build i got a jar file in .m2 folder. When I try to run this jar using
java -jar "jar path"
getting no main manifest attribute, in "jar path".
My POM.xml is
<build>
<plugins>
<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>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>main.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Please suggest how to get over rid of the problem.
Now i want to run my code through jar
exec-maven-plugin is for executing a program during a maven build.
You don't want it.
And as a side note, you don't have specified any goal for it.
So, it will do nothing.
You want to package your jar in a way that it is executable.
So use rather the maven-jar-plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
To create a JAR with dependencies specified in the pom, instead of, use the maven-assembly-plugin with the jar-with-dependencies descriptorRef:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>main.Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>

Maven won't create runnable jars

My pom.xml looks like this
...
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
...
but although I specified the main class, on clean package install maven only creates jars that aren't runnable.
How can I fix that? The manifest then looks like this:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Matthias
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_121
Instead of maven compiler plugin, you should configure the maven jar plugin to create an executable jar :
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
To declare the source/target java version you could use :
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
instead of declaring the compiler plugin.
This is less verbose and it produces exactly the same result.
Besides, if you want to include dependency jars of your application inside your executable jar, you should favor the maven assembly plugin over the maven jar plugin :
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>application.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>

scala maven plugin not packaging scala files into jar

I'm new to maven and currently try to assemble a scala project with it. Project structure:
dir
|
|--src/main/java
|
|--src/main/scala
|
|--pom.xml
I was kind of surprised that classes compiled from *.java end up in jar, but one compiled from *.scala do not. I added these plugins to pom.xml
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
I though maven-jar-plugin is responsible for assembling jar files. But for some reason, it does not add scala-compiled classes.
QUESTION: Who adds .class file into a final jar after executing mvn install? How to add .class-files compiled with scala compiler?
mvn package will build your jar, however maven-compiler-plugin will only compile your java source files not your scala source files. Scala-maven-plugin can be used to compile both java and scala sources.
I wrote a blog post on this a while ago, that may help http://blog.rizvn.com/2016/04/scala-and-maven.html
You will need to tell maven about src/main/scala, since you are putting your scala code under src/main/java. This is done through the build section like so:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
...
</build>
Can you try something like this, using "scala-maven-plugin" instead. Then execute maven goal : mvn clean package
<build>
<sourceDirectory>${basedir}/src</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/test/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
<configuration>
<sourceDir>${basedir}/src</sourceDir>
<outputDir>${basedir}/target/classes</outputDir>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Categories