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?
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.
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
Related
I've created a Discord bot in Eclipse and want to export it (Jar file). The problem is: I can't run it. Each time I try to start it it gives me this error: java.lang.NoClassDefFoundError: net/dv8tion/jda/core/entities/Game
I'm using "JRE System Library [JavaSE-1.8]" for my program.
This here is my pom.xml file
https://pastebin.com/bcs51jm9
After some research it seems like Eclipse doesn't implement the Maven Librarys into the Jar-file.
How can I fix this?
Exporting a jar with Eclipse from the wizard is generally the way when you don't have a build tool that allows to do it in a straight way.
You use Maven. So you have this tool.
What you need is configuring your pom.xml to enable the assembly plugin execution with the jar-with-dependencies descriptorRef.
It allows to specify the main class and to create a fat jar that contains all required dependencies at runtime according to the Maven dependencies declaration defined in your pom.
By executing mvn assembly:single you could so generate an additional jar : a jar-with-dependencies.
I created a spring boot job which relies on properties on the server and I can get it to run like so, no modifying manifest.
/bin/java -Dspring.config.location=/var/tmp/com.jdbc.properties -jar my.jar
and it works. But the application relies upon another jar that is an internal jar that lives under /usr/local/share/jni/foo.jar which I want to add to this mix.
I have tried countless runs trying such things as:
java -cp /usr/local/share/jni/foo.jar -Dspring(picking up original line)
When I start to google this, it takes me on magical tours of running:
'org.springframework.boot.loader.JarLauncher'
or
'org.springframework.boot.loader.PropertiesLauncher'
then mucking with manifest etc.
Spent last 4 hours with no success. Is there a best practice to run a standalone jar that needs to consume remote properties file and an additional jar file? Would like to keep it simple if possible.
If you are using Spring Boot and want to have a Fat-jar that encapsulates all your dependencies, the best way is to add the required Jar as a dependency to your project.
Assuming you are using Maven to build your project, the "foo.jar" needs to be added as a Maven dependency to your project. Then, spring Boot maven plugin will pick up the jar and includes it in your Fat-jar.
Even if the "foo.jar" does not exist in any Maven repo, you still can add it manually to your local Maven repo using the Maven command mvn install:install-file (See Maven doc).
Did you try using foo.jar as a provided dependency within your maven/gradle dependencies and building the project as executable war file?
See spring boot's maven plugin description of building executable war files.
Overall. Run spring boot standalone jar on a Linux server. Additionally read the database properties from a static file on the server, and path in a jar file that adds functionality that only lives on the server. Cannot include in the boot lib.
command line run (will convert to shell) and ran.
/path/to/..openjdk-1.7.0.55.x86_64/bin/java -cp /usr/somewhere/jni/Foo.jar:/path/where/lib/MYBOOTJAR.jar org.springframework.boot.loader.JarLauncher --spring.config.location=/path/to/properties/on/server/com.xxx.yyy.zzz.jdbc.properties
Seems like using the JarLauncher (no modifications to manifest, except excluding the Foo.jar from local)
Hope this helps someone else.
I develop a java web app in intellij which is then deployed to tomcat (tomcat integrated with intellij). It is a maven project.
So, usually I do this:
I run package goal in maven. Then I run tomcat in intellij.
Or I may not package it with maven and run tomcat in intellij right away.
So what is the difference then?
If I package it with maven and then run tomcat in intellij does the source code get compiled and packaged again and then deployed? Or just war produced by maven goal is deployed to tomcat?
Well if I don't package it and just run tomcat in intellij then it obviously gets compiled by intellij goal.
I am a bit confused here.
You don't need to run mvn package before running your webapp within Intellij.
What you can do is configure your Tomcat server to run your war exploded.
In the run/debug configuration, you can specify before launch operation.
By default,
a make is done
build artifact in exploded mode is done
If you prefer to work with a-non exploded war, last step would be build artifact instead of build exploded artifact.
I'm trying to build web Service using maven and eclipse. Here is steps I followed.
Generated mvn folder struture using comman prompt
mvn archetype:generate - DarchetypeArtifactId=maven-archetype-webapp
Converted the mvn project into eclipse.
mvn eclipse:eclipse -Dwtpversion=2.0
Imported the project into my workspace.
Changed the project facets such as java version, servlet version, added cxf and jaxb feature and server runtime.
Copied the WSDL to Resources folder.
Since I do not want to add dependencies(as somebody else would be doing this job for me). I added spring and CXF lib into build path. and also to deployment assembly.
Generated the jaxb classes and operations from wsdl. Here all the java classes created instead of going to src/main/java to went to src/main/resources.
I run the app on tomcat. web.xml is invoked from that cxf framework got invoked and also spring bean creation got invoked. but while creating the bean for the webservice class it threw Class not found error.
When I opened up the war I could notice that while packaging instead of placeing the class files, the eclipse placed java file as it is in the war file.
Could some one help me in fixing this problem. Not sure how to configure eclipse to compile and place .class files in the war instead of .java filee.
Figured out the problem, while importing the file eclipse is adding command, exclude all the java files from src/main/resources in srource tab of java build path On removing it worked fine
I have a maven application under eclipse . The jsp pages and WEB-INF folder are located under the path NomeMiaApplicazione\src\main\webapp. I run these commands : mvn clean ,
mvn eclipse: eclipse and mvn compile under the root folder of the application, and in all three cases, the build is successful. However when I access the folder ( into the workspace )
. metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\NomeMiaApplicazione
, there are no jsp pages . what would be the solution for this problem ? thks !
It is necessary to clarify the role of the commands you used and their impact on Eclipse.
The mvn eclipse:eclipse command creates the Eclipse project files, in order to save you the hassle of configuring a new project and identifying all the sources for it. This command is also equivalent to creating a new Eclipse project from an existing Maven project using the m2e Eclipse plugin. See also this page regarding the eclipse:eclipse command. That being said, you only need to run such command once, and then import the resulting project into Eclipse.
Secondarily, mvn compile builds your source files into the target directory of your NomeMiaApplicazione root folder. This command does not involve Eclipse in any way. Also, web resources are still not packaged. To package them, you need to issue mvn package: you will then find the <artifact>-<version>.war file again under target, and the pre-packaged content under target\<artifact>-<version>. Beware that, in order to account for the webapp content, your Maven packaging must be of type war. Check the pom.xml for the <packaging> tag.
Finally, deployment is still another issue. If you actually need to move your .war file from the target directory to somewhere else (namely, an autodeploy folder of a servlet container), you can configure the Maven Deploy Plugin and issue mvn deploy. I'd rather suggest you to search SO for deploy war eclipse and/or deploy war maven, since there's plenty of related stuff. In the first case, you will find how to use Eclipse as a facility for deployment, while the second case leverages the command line to provide a more portable/flexible deployment procedure.
That's because mvn compile ends on compile phase of Maven's default lifecycle. Do mvn package and check then. And by the way, default Maven output directory is target so rather check it instead of kind of WTP temporary dirs.