Create executable jar file for Cucumber Selenium project - java

Problem: I want to create an executable jar of my BDD Cucumber Selenium framework, which I can run using command like "java -jar bddframework-0.0.1-SNAPSHOT.jar".
What I tried:
I tried to directly create a jar file by "mvn clean package". I did get an jar file but when I run it using java -jar ***, I get below message:
no main manifest attribute, in bddframework-0.0.1-SNAPSHOT.jar
Then I tried adding a main method as below by adding a new Main.java.
public static void main(String[] args) throws Throwable {
String[] arguments = {"--plugin", "html:build/reports/cucumber", "--glue", "com.demo.amazonbdddocker.teststeps", "src/test/resources/feature"};
cucumber.api.cli.Main.main(arguments);
}
Still I get the same error: no main manifest attribute

The root cause of that issue is you are just creating a jar file NOT EXECUTABLE JAR. In order to create the executable jar, you need to do the below configuration in the POM.xml and after that you will be able to create the executable jar file.
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
and then you can run the below maven goals
mvn clean compile assembly:single

Related

Running Java program/Jar from command line

I have a java program that I coded in intellij using maven. I exported it as a jar and tried running from the command line but it keeps on telling me
Error: Could not find or load main class com.company.Main
Caused by: java.lang.ClassNotFoundException: com.company.Main
I then attempted to run the program itself from the command line and that also tells me it cannot find the main class
command line:
for the jar: java -jar selenium_project.jar
for running the program :java com.company.Main
For the jar I am running it from the folder it is in. For the program I am running it from the root of the program.
Pom.xml
<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.company.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins></build>
Manifest:
Manifest-Version: 1.0
Main-Class: com.company.Main
It might be an issue with maven, I dont know. I created a simple hello world program and ran that from the command line and it worked. I ran it like this (c://etc)../src/> java com.company.HelloWorld. But again when i try for this one it cannot find the main class.
Thank you
These are my classes
EDIT:
I changed to use the maven-assembly-plugin and I rebuilt the jar and still does not work
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.company.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins></build>
class structure to show where main class is
code in the Main class
public class Main {
public static void main(String[] args) throws IOException, SQLException {
System.out.println("hello");
Executor ex = new Executor();
}
}
Your error is probably that your Main class is in the wrong package.
Furthermore, please not that you need the Maven shade plugin or Maven assembly plugin to build executable jars if you have dependencies.
I dont know what the issue was. I tried creating a new program and i copied over the classes and remade the pom.xml file and my new program works except for one dependency (chrome options) but I dont think that is what was causing an issue. So now I just need to figure out why Chrome options wont work but the actual jar from the new program does run.
From: https://www.xspdf.com/resolution/54971948.html
To extract the files from a jar file, use x , as in: C:\Java> jar xf myFile.jar. To extract only certain files from a jar file, supply their Install an archive program. JAR files work just like ZIP files. You can use any archive program to extract them. 2. Find the JAR file you want to extract. Use File Explorer (press Win + E to open File Explorer) or Finder on Mac to 3. Right-click the JAR file. Doing so . How to extract manifest file from jar. Extracting the Contents of a JAR File.

Can't load main class from java commandline for jar

I have a jar with two main class. I'm trying to run the main class using the command
java -cp TestNGExamples-0.0.1-SNAPSHOT.jar:lib/* com.test.integration.TestMain
but I get the following error
Error: Could not find or load main class com.test.integration.TestMain
I don't want to edit the manifest file since I already have a Main class given there. lib folder is there in the jar and I've checked it.
Can anyone tell me what I'm doing wrong here?
Edit:
This was my referral : Run a JAR file from the command line and specify classpath
If I am not wrong you are using maven. If so you can specify your main class in pom.xml.
You can do something like this.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main_Class_Package_Name</mainClass>
</manifest>
</archive>
</configuration
</plugin>
</plugins>
</build>

Running maven project via apache-maven server

Dear all I have a maven project and I need to run it using apache-maven I'm using the command mvn install to compile and add it to .m2 repository but how can I run this maven project via apache-maven server without using IDE's
Thanks in advance
In order to run a simple Java project, you have to identify the Main class. If you are using Maven for the build, the packaging will have to specify which class contains the main method.
You can do the above in Maven POM by adding a manifest in the configuration for the maven-jar-plugin which is responsible for the packaging. In other words, you simply add the following to your POM.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
org.baeldung.executable.ExecutableMavenJar
<!--Full classified name of the class that contains the main method -->
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Once the JAR is created, you can execute it by invoking the following:
java -jar jarName.jar
You can find more details on the topic at http://www.baeldung.com/executable-jar-with-maven
You can follow these steps to find the solution of the main question:
https://www.mkyong.com/maven/how-to-deploy-maven-based-war-file-to-tomcat/

Build java project using jars into one jar (command line)

In Intellij, I'm able to "build an artifact" which allows me to create a jar file, even though my project uses other jar files (like drivers). Traditionally, I would compile a project with jar dependencies by putting them in my classpath like so:
javac -cp .:dependency.jar Main.java
and then be able to run Main by doing:
java -cp .:dependency.jar Main
My question is, how do I create a jar file file with this new Main? I can do:
jar cvf Main.jar Main.java
which will create a jar file, but it doesn't run and says:
no main manifest attribute, in Main.jar
I'd like to know how to package my other jar dependencies into on executable jar.
Option 1: Using Maven plug-in if you are using maven:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Path.to.your.main.class</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
Option 2: Using spring boot to create fat jar. Which includes all your dependent jar files. Make sure you include spring boot dependency.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
Option 3: Without any help of maven and Spring:
Create a manifest.txt file with the following content:
Main-Class: path.to.your.main.class
To create jar use command : jar -cvfm Application.jar manifest.txt /path/to/*.class

Error: Could not find or load main class in Java Maven project

I am working in Java Maven project. There is a .bat file in the root of the project which invokes a Java class with some arguments something like this:
java my.package.MyClass abc hi 1
Now, my project jar is built in the target directory of that project when I do mvn clean install. When I run that .bat file it gives me the below error
Error: Could not find or load main class my.package.MyClass
Project's pom.xml only contains jars as dependency.
Do I need to do something in pom.xml to make it work?
please provide your pom.xml so we can look for it,
anyway,
do you use maven-jar-plugin?
something like this
<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>your.main.class.package.ClassName</mainClass> // your main class
</manifest>
</archive>
</configuration>
</plugin>
and try to run your *.jar with command java -jar yourjar.jar

Categories

Resources