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.
Related
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>
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
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
I have been trying for several days now to create an executable jar file for my muli-module maven project. However, when I try to run this jar file I get "Could not find or load main class src.main.java.com.domain.Mainclass" (I have changed the name domain and MainClass for my company's privacy sake)
I have searched for days and none of the obvious tips seem to work.
My project has a main maven project that downloads all of the dependencies (packaging:POM)
and several module projects (Packaging:Jar).
Everything seems to work fine, and all of the files are compiled into class files, but somehow the main class is not being added to the pom.
My Pom File Plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.domain.project.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
The Commands I Use: mvn clean package, then I cd into the target folder and do: java -jar jarfilename.jar
Any tips or help would be most welcomed!
Edit:
My current configuration creates a 2 jar files for every module:
projectname-jar-with-dependencies.jar
projectname.jar
When I navigate to the target folder of the module with my main class, I can successfully run the jar file. However, when I try to run the jar file of my parent maven project (the one with packageing:pom) I still get the same error. Does anyone know why the main jar file cannot find the main class?
Thanks!
You should not have src.main.java as part of the package name of your main class. It's just part of the default maven project folder structure convention. Your configuration should probably read:
<archive>
<manifest>
<mainClass>com.domain.project.MainClass</mainClass>
</manifest>
</archive>
I'm going to necro this post because it is important to have 3 different things set properly:
MainClass.java needs to be in <project_root>/src/main/java/com/domain/project/
Maven assembly plugin needs <mainClass>com.domain.project.MainClass</mainClass>
Your package should be set to com.domain.project
When those three match, Maven should package an executable JAR file.
it seems like you have one of the plugins missing. add the bellow to pom.xml file
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>yourclassnameKt</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
The plugin will add MANIFEST.mf file, which will tell the Java runtime which class to execute.
There is another issue that has the same problem ->
Kotlin + Maven assembling: no main manifest attribute
Reference: https://michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/
I have a bit of a different but similar scenario so I thought I would share. I have a primary maven application packaged into a war through the build (pom.xml packaging configuration). I wanted to add a jar file that is created from one package within our source code, and added into the assembled output along with the webapp war. This allows us to deliver the web application along with a cli tool separately. I didn't want to reconfigure the pom.xml to be multi-module, but just to add a jar as a separate executable within our existing structure. I was banging my head against this for a while.
First, I ended up deleting my entire .m2 directory with maven repositories locally. It appears there may have been an issue for me here, because my ultimate code that worked seems to be the same as what wasn't working originally. I suspect the reasoning for this was I was trying different versions of libraries and was creating conflicts, but who knows. First suggestion I have if you are having issues is delete your .m2 folder and try from scratch.
Also reference #Adam Howell's answer because that is the fundamentals. I created a simplified example project to figure out why nothing was happening and in there I realized i forgot to prefix the folder structure as src/main/java... doh! Of course in my existing project, this was not the case.
And here is my plugin code i inserted in my pom.xml that worked:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.myCompany.app.cli.CLITool</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
<finalName>our-cli</finalName>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
I am pretty sure you can change the phase you want this done in, I figured earlier on in compile made since to ensure it was available for packaging later. I'm not very experienced with Maven though, so note that this may not be semantically in-line with Maven conventions.
And it worked! Not sure why this took me hours to work out although it did. This I just updated my bin.xml to include the jar file in my assembled deliverable, and Voilah! I have a jar executable separate from my webapp that I can use as a command-line interface tool.
I recently just started toying around with Maven in java. Time comes to test my project, it works fine in the NetBeans window, running the main class found in App.java (com.MyCompany.App), but when I try to run it from a command line I get an error:
java -jar fileName.jar
"No Main Manifest Attribute" in fileName.jar
I have tried adding a manifest.mf file specifying what main is, I've also been into project properties and added it as the main file...
What's going on?
You need the maven-jar-plugin (see Maven's example). This plugin will create the required entries in the manifest file when the project is built.
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
You need the version, otherwise, the project won't build. The fully.qualified.MainClass starts at the package hierarchy.
Hope there is a problem in your manifest file. Some basic checks might solve your problem.
it should under /META-INF/MANIFEST.MF
Content should have Main-Class:com.MyCompany.App
If you are using any IDE, there should be an option to export project as runnable jar, you can make use of that to let the IDE take care of correct manifest.
From command line jar cfm filename.jar Manifest.txt com/MyCompany/*.class which generates the Manifest file with following contents
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: com.MyCompany.App
And then you can run jar command java -jar fileName.jar.
These type of problems are trivial but kills lot of time, just ensure your contents and location of the file is correct.
You could just use this for command line:
java -cp jarFileName.jar full.package.className
You wouldn't have to go into specifics of the Manifest file in this case.
Setting an Entry Point with the JAR Tool:
The 'e' flag (for 'entrypoint') creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:
jar cfe app.jar MyApp MyApp.class
You can directly invoke this application by running the following command:
java -jar app.jar
If the entrypoint class name is in a package it may use a '.' (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:
jar cfe Main.jar foo.Main foo/Main.class
If you look at the properties dialog for the project (from project tab, right click on your project and select properties) you'll see that there is a "run" item in the "Categories" window. Click on it and you'll see a dialog where you can specify the Main Class for the jar. That information will end up in your manifest.
I have been having this problem with Netbeans 8.0 and the built-in Maven project for the "Java Application" project prototype. Also I have Maven 3 and I found some of the suggestions on the web don't match the maven code used with Netbeans as well.
Anyway here's a simple recipe for having JAR file to run the main-class and embed dependent libraries. I made this work by comparing other project POM files for projects I found that worked with sub-project JAR-s so if someone with better Maven knowledge spots a gottcha, please speak. Also, I left in some normal stuff to provide context. Example follows:
<properties>
<packageName>trials.example</packageName>
<mainClass>${packageName}.CmdApp</mainClass>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4jVersion>1.7.7</slf4jVersion>
<log4jVersion>1.2.17</log4jVersion>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
This section tells Maven about the project.
packageName ... Is the Java package for the main-class
mainClass ..... The fully qualified name for class with main() method.
You will see these used in the maven-jar-plugin.
The other thing the built-in example prototype didn't do was to package my sub-projects into the JAR so I can run from the command line. This is done with the maven-dependency-plugin below. The interesting bit is in the where we don't need to package the system stuff, and we want the dependant classes wrapped into our JAR.
These are used as follows:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeScope>system</excludeScope>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
<packageName>${packageName}</packageName>
<addClasspath>true</addClasspath>
<classpathPrefix>classes/</classpathPrefix>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${pom.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Hope that saves you the few hours of checking and testing to make it happen. Cheers, Will.