Reputation: 1319
I created a Maven project for Spring Boot. I have a lot of Spring dependencies and one main class:
package com.vastserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyArtifactApplication {
public static void main(String[] args) {
// SpringApplication.run(MyArtifactApplication.class, args);
System.out.println("hello!");
}
}
The folder structure of src
directory is:
.
└── main
├── java
│ └── com
│ └── vastserver
│ └── MyArtifactApplication.java
└── resources
└── application.properties
In my pom.xml I use maven-assembly-plugin
in order to build my project in a standalone .jar file. Even though I triple checked that the directory structure and main class file appear correctly in the pom.xml I keep getting the error: Error: Could not find or load main class com.vastserver.MyArtifactApplication
when I run mvn package
and then java -cp target/vast-ad-server-artifactId-1.0-SNAPSHOT-jar-with-dependencies.jar com.vastserver.MyArtifactApplication
or mvn exec:exec
. The main class does work if I run it from Intellij so I know the code is not the problem but rather Maven build settings. I lost at where my problem could be.
My pom.xml looks as follows:
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<mainClass>com.vastserver.MyArtifactApplication</mainClass>
<descriptorRef>jar-with-dependencies</descriptorRef>
<targetSnapshot>target/vast-ad-server-artifactId-1.0-SNAPSHOT</targetSnapshot>
<targetWithDependencies>${targetSnapshot}-${descriptorRef}.jar</targetWithDependencies>
</properties>
<groupId>com.vastserver</groupId>
<artifactId>vast-ad-server-artifactId</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<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>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>${descriptorRef}</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<argument>${targetWithDependencies}</argument>
<argument>${mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Upvotes: 9
Views: 93706
Reputation: 119
It may be caused by JRE path. Change JRE path and clean project, it will work.
Upvotes: 0
Reputation: 11
Your source file is missing from classpath. For me it happened when I restarted STS.
Upvotes: 1
Reputation: 29
try updating project in maven. Sometimes while adding new dependency it wants maven to be updated there Dev tools won't work
---Right click project ---go to maven ---Update project
Upvotes: 0
Reputation: 41
Rightclickonproject->Maven->UpdateProject->Tick the checkbox Force update of Snapshots/Releases-OK
give time to update the project. Then simply run the application Rightclickonproject->Runas->Spring Boot App
Upvotes: 0
Reputation: 1216
Adding to all the above answers
right click on project-> Maven ->update project-> force update
This will download all missing dependencies.
Still if it doesn't work ?? right click on pom.xml
and run as configuration. Check you are using correct JRE. Then set goal as install and run it.
Important check java version in pom.xml if it doesn't match with your installed JRE version then also you might get this error. So make sure its same. Still doesn't work?? Delete the project from workspace and import it back.
Upvotes: 0
Reputation: 42491
I think you should check the directory structure of the artifact that was built by maven. Usually, spring boot artifacts are prepared by a special spring boot plugin and not by a maven assembly plugin.
Although it shares the "jar" suffix, it's not really a jar, it has special classloader to load classes from BOOT-INF/lib
folder.
I've already provided a detailed answer on what happens exactly when the spring boot application starts here but bottom line if you use assembly plugin you'll have to prepare both manifest file and a fairly complicated structure of folders. Frankly, I think you should use spring boot plugin as a first resort to build spring boot applications.
Upvotes: 3
Reputation: 1319
I realized that the spring-boot-maven-plugin
actually does the building, so other plugins are not needed. If the plugins section in maven is edited to:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
Then running mvn package
and java -jar target/vast-ad-server-artifactId-1.0-SNAPSHOT.jar
works.
Upvotes: 8