When I compile the application it works perfect, also test are passed but then if I execute java -jat in the compiled jar i had the following error when trying to load a jpg resource:
XXX@XXXX MINGW64 /d/Proyectos/PetApp/Back (master)
$ mvn clean install
###########################################################
__________ __ __ _____ ____________________
\______ \ _____/ |__/ |_ / _ \\______ \______ \
| ___// __ \ __\ __\/ /_\ \| ___/| ___/
| | \ ___/| | | | / | \ | | |
|____| \___ >__| |__| \____|__ /____| |____|
\/ \/
###########################################################
2019-10-16 15:29:59,977 INFO [main] org.springframework.boot.StartupInfoLogger: Starting BackEndTestApplicationTests on gggarrido10 with PID 4436 (started by gggarrido in D:\Proyectos\PetApp\Back)
2019-10-16 15:29:59,980 DEBUG [main] org.springframework.boot.StartupInfoLogger: Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-10-16 15:29:59,981 INFO [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
2019-10-16 15:30:04,187 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBConfig: H2 DDBB selected
2019-10-16 15:30:08,767 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Initializacion of the DDBB
2019-10-16 15:30:09,002 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: ADMIN stored on DDBB
2019-10-16 15:30:09,006 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: USER stored on DDBB
2019-10-16 15:30:09,017 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: VISITOR stored on DDBB
2019-10-16 15:30:09,237 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default AdminUser Created: AdminRoot/AdminPass246
2019-10-16 15:30:09,237 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default Owner Created: Manolo Amelgas
2019-10-16 15:30:09,253 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default Pet Created: Lucky
2019-10-16 15:30:14,019 INFO [main] org.springframework.boot.StartupInfoLogger: Started BackEndTestApplicationTests in 14.867 seconds (JVM running for 16.865)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 16.279 s - in com.mashosoft.backEndTest.BackEndTestApplicationTests
[INFO] Running com.mashosoft.backEndTest.security.service.Impl.AuthServiceImplTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.mashosoft.backEndTest.security.service.Impl.AuthServiceImplTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ pettapp ---
[INFO] Building jar: D:\Proyectos\PetApp\Back\target\pettapp-V.RELEASE.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) @ pettapp ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ pettapp ---
[INFO] Installing D:\Proyectos\PetApp\Back\target\pettapp-V.RELEASE.jar to D:\Tools\.m2\repository\com\mashosoft\pettapp\V.RELEASE\pettapp-V.RELEASE.jar
[INFO] Installing D:\Proyectos\PetApp\Back\pom.xml to D:\Tools\.m2\repository\com\mashosoft\pettapp\V.RELEASE\pettapp-V.RELEASE.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 34.801 s
[INFO] Finished at: 2019-10-16T15:30:18+02:00
[INFO] Final Memory: 49M/331M
[INFO] ------------------------------------------------------------------------
XXX@XXXX MINGW64 /d/Proyectos/PetApp/Back (master)
$ cd target/
XXX@XXX MINGW64 /d/Proyectos/PetApp/Back/target (master)
$ java -jar pettapp-V.RELEASE.jar
###########################################################
__________ __ __ _____ ____________________
\______ \ _____/ |__/ |_ / _ \\______ \______ \
| ___// __ \ __\ __\/ /_\ \| ___/| ___/
| | \ ___/| | | | / | \ | | |
|____| \___ >__| |__| \____|__ /____| |____|
\/ \/
###########################################################
2019-10-16 15:30:33,466 INFO [main] org.springframework.boot.StartupInfoLogger: Starting BackEndTestApplication vV.RELEASE on gggarrido10 with PID 11360 (D:\Proyectos\PetApp\Back\target\pettapp-V.RELEASE.jar started by gggarrido in D:\Proyectos\PetApp\Back\target)
2019-10-16 15:30:33,472 DEBUG [main] org.springframework.boot.StartupInfoLogger: Running with Spring Boot v2.1.6.RELEASE, Spring v5.1.8.RELEASE
2019-10-16 15:30:33,475 INFO [main] org.springframework.boot.SpringApplication: No active profile set, falling back to default profiles: default
2019-10-16 15:30:37,905 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBConfig: H2 DDBB selected
2019-10-16 15:30:41,331 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Initializacion of the DDBB
2019-10-16 15:30:41,792 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: ADMIN stored on DDBB
2019-10-16 15:30:41,795 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: USER stored on DDBB
2019-10-16 15:30:41,800 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Role: VISITOR stored on DDBB
2019-10-16 15:30:41,918 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default AdminUser Created: AdminRoot/AdminPass246
2019-10-16 15:30:41,919 INFO [main] com.mashosoft.backEndTest.config.DDBB.DDBBInitializer: Default Owner Created: Manolo Amelgas
2019-10-16 15:30:41,963 ERROR [main] org.springframework.boot.SpringApplication: Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DDBBInitializer': Invocation of init method failed; nested exception is com.mashosoft.backEndTest.exception.initialization.InitializationException
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:139)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at com.mashosoft.backEndTest.BackEndTestApplication.main(BackEndTestApplication.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: com.mashosoft.backEndTest.exception.initialization.InitializationException: null
at com.mashosoft.backEndTest.config.DDBB.DDBBInitializer.getTestPetImageAsMultipart(DDBBInitializer.java:123)
at com.mashosoft.backEndTest.config.DDBB.DDBBInitializer.init(DDBBInitializer.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
... 26 common frames omitted
This is the class:
package com.mashosoft.backEndTest.config.DDBB;
import com.mashosoft.backEndTest.component.ImageUploader;
import com.mashosoft.backEndTest.exception.errorcode.InitializationErrorCodes;
import com.mashosoft.backEndTest.exception.initialization.InitializationException;
import com.mashosoft.backEndTest.persistence.entity.Owner;
import com.mashosoft.backEndTest.persistence.entity.Pet;
import com.mashosoft.backEndTest.persistence.repository.OwnerRepository;
import com.mashosoft.backEndTest.persistence.repository.PetRepository;
import com.mashosoft.backEndTest.security.model.enums.Roles;
import com.mashosoft.backEndTest.security.persistence.entity.Role;
import com.mashosoft.backEndTest.security.persistence.entity.User;
import com.mashosoft.backEndTest.security.persistence.repository.RoleRepository;
import com.mashosoft.backEndTest.security.persistence.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
@Configuration
public class DDBBInitializer {
private static final Logger log = LoggerFactory.getLogger( DDBBInitializer.class );
@Autowired
private RoleRepository roleRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private OwnerRepository ownerRepository;
@Autowired
private PetRepository petRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Value("${pettapp.config.default.username}")
private String defaultUsername;
@Value("${pettapp.config.default.email}")
private String defaultEmail;
@Value("${pettapp.config.default.password}")
private String defaultPassword;
@PostConstruct
private void init() {
log.info( "Initializacion of the DDBB" );
for (Roles role : Roles.values()) {
Role roleEntity = new Role( role );
roleRepository.save( roleEntity );
log.info( "Role: " + roleEntity.getRoleName() + " stored on DDBB" );
}
User defaultAdminUser = new User();
defaultAdminUser.setUserId( new BigDecimal( 1 ) );
defaultAdminUser.addOneRole( Roles.ADMIN );
defaultAdminUser.setUsername( defaultUsername );
defaultAdminUser.setPassword( passwordEncoder.encode( defaultPassword ) );
defaultAdminUser.setActive( true );
defaultAdminUser.setEmail( defaultEmail );
log.info( "Default AdminUser Created: " + defaultAdminUser.getUsername() + "/" + defaultPassword );
Owner adminOwnerProfile = new Owner();
adminOwnerProfile.setAddress( "Calle de jacinto Nº6 Bajo B" );
adminOwnerProfile.setName( "Manolo" );
adminOwnerProfile.setSurname( "Amelgas" );
adminOwnerProfile.setDefaulter( false );
adminOwnerProfile.setTelephoneNumber( "678987656 " );
adminOwnerProfile.setUser( defaultAdminUser );
defaultAdminUser.setOwner( adminOwnerProfile );
log.info( "Default Owner Created: " + adminOwnerProfile.getName() + " " + adminOwnerProfile.getSurname() );
Pet testPet = new Pet();
testPet.setAlive( true );
testPet.setBitrh( new Date() );
testPet.setBreed( "Carlino" );
testPet.setFur( "White" );
testPet.setName( "Lucky" );
String imageUrl = ImageUploader.uploadFile( getTestPetImageAsMultipart() );
testPet.setPictureUrl( imageUrl );
testPet.setOwner( adminOwnerProfile );
adminOwnerProfile.addPet( testPet );
log.info( "Default Pet Created: " + testPet.getName() );
userRepository.save( defaultAdminUser );
}
private MultipartFile getTestPetImageAsMultipart() {
try {
File file;
try {
file = new ClassPathResource( "/TestResources/DogImageForTest.jpg" ).getFile();
} catch (IOException e){
throw new InitializationException( InitializationErrorCodes.IMAGE_TEST_NOT_FOUND );
}
return new MockMultipartFile( "DogImageForTest.jpg", "DogImageForTest.jpg", "image/jpeg",
new FileInputStream( file ) );
} catch (Exception ignored) {
throw new InitializationException( InitializationErrorCodes.IMAGE_TEST_ERROR );
}
}
}
Why everything works fine but not when executing java -jar?? I dont know what to try because also in intellij when i press play or debug it works perfect.
The line that I know it is failing is the following:
file = new ClassPathResource( "/TestResources/DogImageForTest.jpg" ).getFile();
I have check inside the jar file and the image is inside:
Thanks in advance
I found the solution. Within a jar I cant use .getFile(); because it expect to have a file withint the system. Intead I used:
fileStream = new ClassPathResource( "/TestResources/DogImageForTest.jpg" ).getInputStream();
return new MockMultipartFile( "DogImageForTest.jpg", "DogImageForTest.jpg", "image/jpeg",fileStream );
With a stream it works perfect