How run spring boot jar with class path resource - spring

I have a spring boot application. It works perfectly if I run it from Eclipse. But if I run the same from command prompt, I am getting the following error
ERROR
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.core.ProducerFactory]: Factory method 'kafkaProducerFactory' threw exception; nested exception is java.lang.IllegalStateException: Resource 'class path resource [kafka/somejksfile.jks]' must be on a file system\r\n\tat
I extracted the .jar file and I was able to see the Kafka related .jks file inside
projectName\BOOT-INF\classes
For some reason it is not picking all the files which are inside src/main/resources. How to refer that location when running the .jar from command prompt. I tried the following with no luck
java -jar -Dspring.profiles.active=dev -Dspring.config.location=file:\\C:\my-files\poc\projectName\src\main\resources\ api-account-info.jar
Note: I cannot make any code changes

I resolved the issue by the following.
Downloaded Spring Boot CLI and extracted it. (https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.6.2/spring-boot-cli-2.6.2-bin.zip)
Set environment variable
Variable: SPRING_HOME
Value: C:\my-files\Installations\spring-boot-cli-2.6.2-bin\spring-2.6.2
Variable: Path
Value: %SPRING_HOME%\bin
From command prompt run the following commands
SET SPRING_PROFILES_ACTIVE=dev
mvn spring-boot:run
This will build and start the application.

Related

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.nsia.officems.OfficemsApplication]

I have a Spring-boot rest API Project when I want to run it as debug mode or run it directly from IntelliJ it gives me the above error and when I run it from the terminal and use mvn spring-boot:run and use this command it works for me and runs the project successfully, I need to debug my project and need it to run as debug mode,
can anyone help me??

Spring-boot app working in IDEA but crash when running jar - “Unable to resolve persistence unit"

Updated:
It's been days that I tried to accomodate some times in between then came back to this code-base, but still faced up the same issue.
I am frustrated that have a Spring Boot 1.5.12 application that worked in IDEA but failed when running executable jar in Unix box, and the stack trace follows this format that I needed to trim the stack trace for relevancy:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxxEntityManager' defined in class path resource [com.abc.persistence.ConfigClass]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
................. [Skipped the stack strace]
Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:602)
... 48 common frames omitted
As per suggestion by Getting "Unable to resolve persistence unit" error when building jar without IDE, I tried this workaround of 2 changes but it doesn't work at all.
1)#EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class) to my application class
2)spring.data.jpa.repositories.enabled=false to my application.yml file in src/main/resource/ directory
Even with those two changes being added, it still shows the same error.
I hope someone can help me figure out this frustrating inconsistency occured between the run time behavior in IDEA and executable jar in console.
Edit:
I used Gradle build script with this build task applications to build the app jar executable whose jar pull everything of src files in package names and resource property configuration yaml files besides gradle build:
applications {
...
}
So I've been tried to crack the root cause that runs my life nuts, which is based on this possible mystery:
Can someone know about jar (JVM) process of how it processes to create jar artifacts that in the end when I face the extremely weird anomaly where Intellj has always been able to set the persistence unit URL to default, whereas running on jar doesn't accomodate this auto-configuration already coded in the app and property codes?

I am getting this exception "org/apache/maven/shared/invoker/MavenInvocationException" when I try executing jar file from command prompt

I have a maven project selenium webdriver test cases, for which I am able to create a jar file but when I try executing in command prompt I get this Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/maven/shared/invoker/MavenInvocationException exception.
This looks like a repository corruption.
Try deleting org/apache/maven folder in your .m2 repository and try again

Spring profile unable to parse correct file

This is the code that I'm using to reading the application.properties file in a gradle project.
#PropertySource("${spring.profiles.active}/application.properties")
My file is located inside resources/local/application.properties
This is how I'm trying to run the application in IntelliJ
This is the error that I end up getting.
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.core.Application]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "${spring.profiles.active}/application.properties"
Can someone help me fix this and read the correct config.
You're trying to get spring.profiles.active but in Intellij you're setting spring.active.profiles

Springboot Strange Resource Loading Behavior

I'm working on a springboot 1.5.1 application that I'm trying to load a WSDL included in my resources directory in the wsdl directory. Depending on where my application executes I'm getting different results (command line, intellij, cloud foundry) and I can't seem to get all three to work at the same time.
I've tried several ways of locating the resource:
From prior to the migration to springboot we had this (worked in IntelliJ but not java -jar myboot.jar):
this.getClass().getResource("/wsdl/my.wsdl");
I switched it to the typically more correct version and got it to work in IntelliJ and java -jar but not Cloud Foundry:
this.getClass().getClassLoader().getResource("/wsdl/my.wsdl");
I switched it to use the Spring Resource Loader version and it worked in IntelliJ and CloudFoundry but not java -jar:
#Value("classpath:/wsdl/my.wsdl")
private Resource wsdlResource;
wsdlResource.getURL();
On the command line what I've noticed is that it seems to be thinking that BOOT-INF/classes is a JAR file (Note the ! after classes):
Caused by: javax.xml.ws.WebServiceException: Failed to access the WSDL at: jar:file:/C:/dev/redacted/build/libs/redacted.jar!/BOOT-INF/classes!/wsdl/my.wsdl. It failed with:
JAR entry BOOT-INF/classes!/wsdl/my.wsdl not found in C:\dev\redacted\build\libs\redacted.jar.
From looking at IntelliJ's URL, it's referring to the actual source folder which explains why it seems to always work.
What is causing this and how might I universally load these class path resources successfully with springboot?

Resources