简体   繁体   中英

Spring boot application cannot run using cmd

I try to run my compiled jar file using java -jar jarfile.jar but it returning following error.

Exception in thread "main" java.lang.ClassNotFoundException: MainApplication
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:593)

Why this is happening. When i run in the spring tool suit it run perfectly. This happen only when i try to run my application using CMD windows

SOLVED This answer is correct. This is the error i have done when configuration of my pom.xml in the api module of my project.

<build>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.0.RELEASE</version>
            <configuration>
                <mainClass>com.mobios.MainApplication</mainClass>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Following line create error

<mainClass>MainApplication</mainClass>

This define the main class of the application. But i have only mentioned the class name only. It must include the group id also. I think lot of people doing this kind of simple mistakes like me. As a spring boot beginner i think it is common. The above line must be like following.

<mainClass>com.mobios.MainApplication</mainClass>

Now working fine when building the jar and run it. But without group id i can run the project in eclipse or any other development tool you are using.

Did you add this type of spring boot application class?

import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;


    @SpringBootApplication
    public class FApplication {

        public static void main(String[] args) {
            SpringApplication.run(FApplication.class, args);
        }
    }

May be your application can not find java manifest on your jar file.

Open command prompt and goto your where pom.xml presents
run mvn clean install
once you get the message build successful

goto target folder by cd target
then run the command java -jar <file-name>.jar

if you are still getting the error message while running then some spring config is wrong.

create new spring boot application from https://start.spring.io/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM