'Error: Could not find or load main class - When running a JAR at the prompt
I'm looking all afternoon how I can run a JAR of a Spring Boot application I'm developing.
Generated the JAR through the mvn clean package command. The JAR is generated inside the target folder, so I run the CMD in this folder and type java -jar apptest.jar and then I get
Error: Could not find or load main class br.com.myapp.Application
When I run the application directly on the eclipse, it runs perfectly.
Can you help me ?
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath />
</parent>
<groupId>br.com.braxxy.adm</groupId>
<artifactId>brxmind</artifactId>
<version>0.0.1</version>
<name>brxmind</name>
<description>Redmine like Applicaiton for Project Management</description>
<packaging>${project.packaging}</packaging>
<properties>
<java.version>1.8</java.version>
<start-class>br.com.braxxy.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>dev</activatedProperties>
<project.packaging>jar</project.packaging>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<executions>
<execution>
<id>show-profiles</id>
<phase>compile</phase>
<goals>
<goal>active-profiles</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
<project.packaging>war</project.packaging>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</profile>
</profiles>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Application.java
package br.com.myapp;
import java.io.File;
import java.util.Locale;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(new Locale("pt", "BR"));
return slr;
}
@Bean
CommandLineRunner init(SystemRoleRepository systemRoleRepository, ProjectRoleRepository projectRoleRepository) {
return args -> {
for (ESystemRole role : ESystemRole.values()) {
SystemRole systemRole = systemRoleRepository.findByRole(role.toString());
if (systemRole == null) {
SystemRole newSystemRole = new SystemRole();
newSystemRole.setRole(role.toString());
systemRoleRepository.save(newSystemRole);
}
}
for (EProjectRole role : EProjectRole.values()) {
ProjectRole projectRole = projectRoleRepository.findByRole(role.toString());
if (projectRole == null) {
ProjectRole newProjectRole = new ProjectRole();
newProjectRole.setRole(role.toString());
projectRoleRepository.save(newProjectRole);
}
}
};
}
}
MANIFEST.MF
Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 0.0.1
Build-Jdk-Spec: 1.8
Created-By: Maven Jar Plugin 3.2.0
Main-Class: br.com.myapp.Application
Solution 1:[1]
To use the Spring Boot Maven Plugin, include the appropriate XML in the plugins section of your pom.xml
, as shown in the following example:
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>getting-started</artifactId>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Build:
mvn clean package
Run:
java -jar target/brxmind.jar
Output:
...
2021-07-29 07:53:43.557 INFO 18739 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-29 07:53:43.576 INFO 18739 --- [ main] br.com.braxxy.Application : Started Application in 2.112 seconds (JVM running for 2.476)
Solution 2:[2]
Sometimes check your pom .xml. If it is showing errors on it in the project explorer file list but when you open the file there is no error. What you can do is to remove all the dependencies and start adding them one by one to find the problematic ones (You can check if the red X sign shows on the pom.xml in the project explorer list). If you locate the problematic dependencies delete the in the .m2/repository directory in your user directory. if it is difficult delete the whole .m2 folder and update all your projects
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dmitry Rakovets |
Solution 2 | mumbasa |