I have made an spring boot app and it works fine with maven but when I run it's jar file it gives an error like
java.lang.IllegalAccessException: class org.springframework.boot.loader.MainMethodRunner cannot access a member of class com.cafe2.user.UserApplication with modifiers "public static"
at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591)
at java.base/java.lang.reflect.Method.invoke(Method.java:558)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.base/java.lang.Thread.run(Thread.java:829)
i have not such an error when i am running app with maven
it was really stupid the error was because of public in boot class i just add public and it works –
public class UserApplication {
Related
Created a jar file for a spring boot multimodule application and ran the jar file using java -jar command. While starting the application, it gives ResourceFinderException. When I analyzed it, the issue is happening because in my ResourceConfig file, i have used the package for my api end points. If I use register(service.class), the application starts fine. Any suggestion how can I provide the package instead of using register? The reason I want to use package is because I have lots of services inside multiple packages and the code looks very ugly if i use register for all the services. The ResourceConfig file looks like below.
public class AppResourceConfig extends ResourceConfig {
public AppResourceConfig {}{
super();
property("jersery.config.beanValidation.enableOutputValidationErrorEntity.server");
**packages("com.api");**
register(GsonProvider.class);
register(RequestContextFilter.class);
register(NotFoundExceptionMapper.class);
register(DefaultExceptionMapper.class);
}
}
Here the issue is with highlighted line: packages("com.api")
If I comment out this code application will be up. Otherwise it is giving org.glassfish.jersey.server.internal.scanning.ResourceFinderException: java.io.FileNotFoundException: api-01.03.00.04-snapshot.jar (No such file or direcotry)
Note: api-01.03.00.04-snapshot.jar is the jar file for one of the module in a project
In our Springboot MVC project we need to get context path. The following code works fine if we use embedded tomcat. Otherwise if we deploy war file to jboss server, context path returns null.
I tried to add context-root property to jboss-web.xml but changed nothing. I also tried to add "server.servlet.contextPath" property to application.properties file but it doesn't work.
Spring boot version : 2.2.0.RELEASE
Code:
#Configuration
public class EurekaClientConfigBeanListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
String pathValue = System.getProperty("server.servlet.contextPath");
}
I have two configuration files that have to be read using spring boot application, however, only the first configuration property file is resolved (external_application.properties) , later one (queries_config.properties) is not getting detected any issue in passing in the command line.
java -Dexternal.app.properties=file:external_application.properties -Dqueries.config.properties=file:queries_config.properties -jar testsnapshot.jar
If you are using spring boot so you can use
java
-Dspring.config.location=file:external_application.properties,file:queries_config.properties
source
I did like below which resolved over all problem..
java -Dspring.config.location=classpath:file:///C:\Users\configfiles\ -jar testsnapshot.jar
secondly to lookup for external config
#Component
#PropertySource(value = "${spring.config.location}queries_config-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class QueriesConfig {
}
#Component
#PropertySource(value = "${spring.config.location}external_application-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class ExternalConfig {
}
How can I load test properties from a file like 'application-test.properties'?
The file is stored in the src/test/resources folder. I put the file also in all possible folders as well. When running the test as part of the Maven test run, all works fine.
When running the new (single) test from the (IntelliJ) IDE, each time I get same the error message:
Caused by: java.io.FileNotFoundException: class path resource
[application-test.properties] cannot be opened because it does not
exist
This is the test class:
#RunWith(SpringRunner.class)
#EnableAutoConfiguration
#ComponentScan(basePackages = {"nl.deholtmans.tjm1706.todolist"})
#PropertySource( "application-test.properties")
public class TodoListServiceTest {
#Autowired
TodoListService todoListService;
#Test
public void testBasic() { ... }
It looks that I have to run the test first time from Maven. Why is that?
Spring Boot will automatically load the correct properties file if the profile is activated. In a test you can use the #ActiveProfiles annotation for that.
Next you would need to make sure that you actually use the proper Spring Boot infrastructure to run your test, using #SpringBootTest. That being said your test header should look something like the following
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles("test")
public class TodoListServiceTest { ... }
And ofcourse make sure that your IDE builds the application before running the tests.
I have a project setup where a common module (JPA.jar) containing Spring JPA configuration.
#Configuration
#EnableJpaRepositories({"com.db.jpa.repository"})
#EnableTransactionManagement
public class Jpa {
// ...
}
I intend to invoke the config from a webservice (spring boot) and have a config importing the JPA configuration from JPA.jar.
#Configuration
#Import(com.db.config.Jpa.class)
public class JpaApp {
}
This fails with following error:
Caused by: java.io.FileNotFoundException: class path resource [com/db/config/Jpa.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:51)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:88)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:75)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81)
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:731)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getRelated(ConfigurationClassParser.java:1007)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getAnnotationAttributes(ConfigurationClassParser.java:988)
at org.springframework.context.annotation.ConfigurationClassParser.collectImports(ConfigurationClassParser.java:536)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:509)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245)
at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:635)
... 40 more
Unable to find any documentation that says this is illegal for Spring's #Import. However I see this is done for resources with #ImportResource, using a classpath prefix.
I can include a set of configs for each webservice component using the common JPA models and repos, but just wondering if aggregating #Configuration(s) specifically using #Import from dependency jars is possible.
Is it possible?
If illegal, is there any rationale to it.
Thanks in advance.
You can use #Import for configuration classes from other jars. I think that you are getting this error because your jar is not defined as dependency in your pom.xml (if you use maven of course), that's why Spring can't find it.