no main manifest attribute in JAR file - java

I can not execute a jar file from terminal due to the 'no main manifest attribute in JAR file' error. I have added a reference to the class with the main method within the 'pom.xml', however it is not detected when I try to execute the file.
This is my POM file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.countrymapper.CountryMapperApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Related

org.apache.flink.client.program.ProgramInvocationException: The program's entry point class 'com.example.KafkaFlink' was not found in the jar file

I was getting an error ProgramInvocationException while trying upload a jar file to flink local server
Here is the pom.xml of code
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.KafkaFlink</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

No main manifest attribute in .jar

I search in Internet many information, but I don't know what's porblem is that...
Please, check this:
Manifest:
Manifest-Version: 1.0
Main-Class: BotPolytechnic
POM:
<groupId>FPolytechnic</groupId>
<artifactId>FPolytechnic</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<archive>
<manifest>
<mainClass>BotPolytechnic</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
use instead the maven-jar-plugin to make the JAR executable:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

Making absolute reference to a classpath in Maven

I have a customized jar that the application uses. Instead of installing jar in maven I want to make reference to it by defining absolute location for that jar in the manifest file. I am using the code shown below to update information in manifest file. However, I am not sure how can I reference to the jar location that uses absolute path like : D:/Shared_library/Test.jar . This might not be a good practice but I want to see if there is anything like that:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
You can add it using <manifestEntries> tag.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>file:///D:/Shared_library/Test.jar</Class-Path>
</manifestEntries>
<manifest>
<mainClass>org.ad.MainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

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>

Categories