Reputation: 51
EDIT
I have only been working with spring for several weeks now and I can create some basic applications. I'm working on a rest api based app with hibernate connected to mysql using maven build system. I am using IntelliJ Idea for development and making artifacts. I am trying to make single .jar file containing embedded tomcat, which I can deploy just by running that file.
ISSUE
In IDE everything runs fine and works properly. When I make executable .jar and try to run it with java -jar, nothing works.
CODE
https://github.com/YoungPetr/fitapp
But basically it is just slightly modified https://github.com/netgloo/spring-boot-samples/tree/master/spring-boot-mysql-springdatajpa-hibernate
PROGRESS
The first problem: It says that I am missing embedded servlet container factory bean, which I fix with adding:
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory();
return factory;
}
Then the second issue: My crud repository interface (autowired as a bean) is not being found and I have to use @EnableJpaRepositories("com.youngpetr.fitapp.server.repositories")
SUMMARY
Please note that this whole time the app was runnable in IDE but only .jar had issues.
After fixing the second issue I basically ended up in an infinite loop of missing configurations and when I fix one, another one appears. Missing entity manager bean, missing session factory bean...
Am I missing something in building the project. How is it possible everything is configured fine in IDE and beans are found, jar can't find them.
I have used several tutorials and even tried to run code from them and same issue appears.
my pom.xml looks similar to
<!-- GLOBAL PROPERTIES AND SETTINGS -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.youngpetr.fitapp.server.Main</start-class>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- BUILD SETTINGS -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- DEPENDENCIES -->
<dependencies>
<!-- SPRING BOOT -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MYSQL DRIVER -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.2</version>
</dependency>
</dependencies>
SUGGESTION
When i try another basic tutorial which doesn't use springboot, but just standard spring, with main method that executes and shuts down, everything is fine even with built jar.
I guess there might be problem in the way I am creating .jar and probably in spring boot configuration since in IDE I am getting no errors.
Would switching to .war help me?
Thanks for your help
Upvotes: 3
Views: 1839
Reputation: 51
Ok so after A WHILE of searching among plenty of different issues which were not applicable, because it seemes my code is all right, I looked into the jar creation process.
I have been creating jar to run by intellij idea function to create artifacts, standard procedure, from module with dependencies, linked main class, added configuration files and all the stuff.
I don't know what is going on there, but when I tried to run everything from the very start manually from the command line (basically just a few maven and java commands), I have managed to create a completely different jar and that one runs properly.
I still have no clue why IDE artifact doesn't work and would appreciate any comments so that people who run into this issue can learn something here.
Big thanks to all guys who gave me a slight hints of where I should and should not look for issues - Ravindra Devadiga, Andy Wilkinson, and even M. Deinum
Upvotes: 1
Reputation: 692
I downloaded your code base and created database with name fitapp as you mentioned. One issue I found after that is couldn't create connection to database server.
I found out that version of mysql-connector-java which you have mentioned in pom.xml was causing this issue.
Now, I have updated the pom like as shown below(removed the version tag) and it is working fine for me
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Make sure you have update the maven dependencies once you change the pom.xml
Upvotes: 0