Java 9 Maven module not found in console - java

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

Related

Run Java Maven project with Linux command line

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;

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>

How to include jars of Eclipse project's classpath in pom.xml

I have an Eclipse Maven project with a Referenced Libraries folder. This folder contains the same jars that are included in project's actual classpath. To compile the project using Maven, when I use
mvn clean install
it cannot get the jars which are in classpath. So I tried using the following
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Whenever I try to compile, it gives me errors stating that it could not find the classes, those classes which I have already added on the classpath of the project.
I also tried
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>/Referenced Libraries</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
but it doesn't do anything. What am I doing wrong ? How do I make the pom.xml file to get jars from the project's classpath in order to compile the project? Any help will be appreciated please. Thank You.

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>

Add subversion revision to war manifest using maven2

I want to find a maven native (i.e. without calling external programs) to inject the svn revision in the war manifest.
Does anybody know a way to do that?
I found mention to how to add the subversion revision to manifests in jar files but not with war files.
I searched SO but could not find this issue specifically.
I want to find a maven native (i.e. without calling external programs) to inject the svn revision in the war manifest.
This is possible with the Build Number Maven Plugin using the svnjava provider:
If you need to execute the plugin on
machine without any svn in the path
you can configure the mojo to use the
svnjava provider.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>true</doUpdate>
<providerImplementations>
<svn>javasvn</svn>
</providerImplementations>
</configuration>
</plugin>
</plugins>
</build>
The Build Number Maven Plugin sets the build number in the ${buildNumber} property that you can then use in your POM.
I found mention to how to add the subversion revision to manifests in jar files but not with war files.
Then, to add the build number in the MANIFEST of a war, configure the plugin as mentioned in the Usage page:
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Try this. About halfway down, look for maven-war-plugin
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Categories