12

Maven build succeeded but when I trying to run it fails with:

Error: Could not find or load main class app.jar

I have in resources/META-INF/MANIFEST.MF with

Manifest-Version: 1.0
Main-Class: go.Application

All seems in place. What's wrong?

pom.xml

<build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>

            </plugin>

        </plugins>
    </build>

UPDATE1

Same story when building jar artifact with IntelliJ.

UPDATE2

OK, I managed to run it but now I have :

Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

UPDATE3

Got it working by adding to Application.java:

@Bean
        public EmbeddedServletContainerFactory servletContainer() {
            TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
            return factory;
        }
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
  • 1
    How do you start the application? Seems to me like the parameter `-jar` is missing when java is called. – Steven Pessall Jan 21 '15 at 10:08
  • when i add `-jar` it shows different error `Unable to access jarfile` – J.Olufsen Jan 21 '15 at 10:10
  • That sounds like your path is wrong. Are you in the correct directory? – Steven Pessall Jan 21 '15 at 10:16
  • Is this spring-boot? With spring-boot you don't need that maven jar config, nor do you need the bean you've created in your application.java. – Software Engineer Jan 21 '15 at 16:39
  • I have the same question as Engineer Dollery. It seems like you are trying to run this as a spring boot app. If so then you need the spring-boot-maven-plugin with sets up all the required references in your jar. So Spring Boot? – EvilJinious1 Jan 22 '15 at 04:32
  • Since I need to trigger methods I need through controller - I need it run as web app (open 8080 port which I can access). The path of jar is right. – J.Olufsen Jan 22 '15 at 08:48

1 Answers1

4

Ok, So i was beating my head over this... I had the following:

/**
 * Main class.
 */
@SpringBootApplication
public class Application {

  /**
   * Main entry point for the application.
   *
   * @param args The args to pass in
   */
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

and all my dependencies were correct..

After an exhausive search, i found the following:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-first-application-executable-jar

Since i dont have the spring boot parent as my parent, I had to include the executions section in my plugin configuration like so:

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <mainClass>your.Application.fqdn.here</mainClass>
          <layout>ZIP</layout>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin> 

See the following for additional info:

http://docs.spring.io/spring-boot/docs/1.4.0.BUILD-SNAPSHOT/maven-plugin/usage.html

Nicholas Terry
  • 1,812
  • 24
  • 40