Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting Spring Boot application as JAR file in eclipse

I'm using the Spring STS in Eclipse to create a simple web-based spring boot project. I can run it fine in Eclipse, but when I try to export it as a JAR file I get:

rg.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

My public static void mainis located in Application.java, with the @SpringBootApplication annotation.

I've double checked all the Maven dependencies a hundred times.

What am I doing wrong?

like image 1000
Viggo Lundén Avatar asked May 03 '16 23:05

Viggo Lundén


People also ask

How do I Export a JAR file from Eclipse?

To export your project, right-click it and select Export. Select Java > Runnable JAR file as the export destination and click Next. On the next page, specify the name and path of the JAR file to create and select the Launch configuration that includes the project name and the name of the test class.

How do I create a .JAR file?

Open the Jar File wizard In the Package Explorer select the items that you want to export. If you want to export all the classes and resources in the project just select the project. Click on the File menu and select Export. In the filter text box of the first page of the export wizard type in JAR.

How do I create a JAR file in Spring Tool Suite?

If you are using STS/Eclipse IDE, then Right Click on your project » Run As » Maven build… » Goals: clean package » Run. Step 4: Step 3 will create an executable JAR file of your Spring Boot application and put it within the target folder. Step 5: Run the executable JAR, using the following Java command.


2 Answers

Most likely, you're using the built-in Eclipse exporter to generate your jar, which only includes the target files actually produced in that project. In order to have a "fat" (standalone executable) jar, you need to use the Spring Boot Maven or Gradle plugin to "repackage" the jar.

First, make sure that you have the repackage goal included in your build setup, then use the Maven package target. The simplest way to do this is to run mvn package from the command line (you may need to install the Maven CLI package for your OS); you can also right-click the POM in Eclipse and "Run As" to execute specific Maven operations from within Eclipse.

like image 115
chrylis -cautiouslyoptimistic- Avatar answered Oct 12 '22 18:10

chrylis -cautiouslyoptimistic-


It is a single line command, on window 7/10 machine, with command prompt to your project folder (Inside your project workspace). I do not do with Eclipse IDE POM maven goals, but you can do with maven goal there also. ON window machine I prefer cmd.exe for exporting and running.

mvnw clean package

on unix kernel based

./mvnw clean package

You have to go inside workspace and than to the project root folder. You will see a maven wrapper mvnw, with that you don't need to have maven installed and .mvn folder at the same level provides necessary jar for that. For a project

D:\workspace\Zuteller Workspace\zusteller>mvnw clean package

it will create zusteller-0.0.1-SNAPSHOT.jar in the target folder at the same level.

D:\workspace\Zuteller Workspace\zusteller>java -jar target\zusteller-0.0.1-SNAPSHOT.jar

You can run self-contained application(embedded Tomcat) and access at localhost:8080/your project

like image 37
vimal krishna Avatar answered Oct 12 '22 20:10

vimal krishna