Can't run Spring Boot JAR file with specific profiles - java

Like the image I've attached, I can run my JAR file with command 'java -jar', but it doesn't work when I add a spring.profiles.active config, and I can't get any infomation in my console.

Please try it as below:
java -jar service-app-1.7.44.jar -Dspring.profiles.active=demo

Related

Springboot application not using the correct .properties file

I have a simple Springboot app that can be ran with the command: ./mvnw spring-boot:run. This works fine if I put the URI to my database inside of the application.properties file but the problem is that this file is used by Heroku, and is not meant for my local use.
So I came across a Stackoverflow answer that said I could simply make another .properties file but name it application-dev.properties and then when I run my app, the correct .properties file will automatically be chosen when I set the active profile to dev.
So I tried this by doing the following:
Make the application.properties file use the environment variable from Heroku since this is the .properties file I do NOT want to use locally.
I created a .properties file called application-dev.properties that has this line in it:
spring.data.mongodb.uri=mongodb+srv://MY_NAME:MY_PASSWORD#springbootcluster.v1maw.mongodb.net/Employees?retryWrites=true&w=majority
I run the app like this: ./mvnw spring-boot:run -Dspring.profiles.active=dev
The app fails with a ton of different errors because it is trying to use the application.properties file and not the application-dev.properties file
Part of the error message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';
ERROR MESSAGES
-Dspring.profiles.active is setting the spring.profiles.active system property in the JVM that's running Maven, not the JVM that's running your application. To fix the problem, use the spring-boot.run.jvmArguments system property to configure the arguments of the JVM that is used to run your application:
./mwnw -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
Alternatively, there's a property specifically for setting the active profiles which is slightly more concise:
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
You can learn more in the relevant section of the reference documentation for Spring Boot's Maven plugin.

How to call a main class in a jar package from eclipse?

Currently, there is bat file that calls the main class in a jar file. Now I want to run it using eclipse. How do I configure the eclipse to run it?
I have tried the Run > External tools > External tools configuration. But I don't know what to type in...
#echo off
set MODULE2_HOME=%~dp0..
set JAVA_HOME=C:/Program Files/Java/jdk1.6.0_71
set CLASSPATH="%MODULE2_HOME%/classes;%MODULE2_HOME%/lib/*;%MODULE2_HOME%/lib/oracle/*;%MODULE2_HOME%/lib/aspose/*;%MODULE_HOME%/aspose/*"
set SETUP_PROPERTIES="%MODULE2_HOME%\conf\setup.properties"
set PATH=%JAVA_HOME%\bin;%PATH%
java -cp %CLASSPATH% -Dsetup.properties=%SETUP_PROPERTIES% com.module.fast.main.Module2Main %*
How do I configure eclipse to run exactly like this command?
Since it is a batch file which will run in Windows. You can go to the command prompt and run the batch file using .bat. You do not need Eclipse. You can run the java class in eclipse. Still if you want to run, you can check the following screenshot.
While configuring, make sure to give the complete path of batch file. If the bat file needs extra commandline params, you can give the batch file location with space and the command line parameters separated with spaces.
It does not work to run the jar file which contains main class, you have to give the working directory location.

Heroku error code H10 when Spring Boot app is deployed

I can't open my app in Heroku - I get error H10. When I run my app in Eclipse or use executable jar file - everything work well. Here is my code : GITHub Below is my log file:
DropBox
You app is running on port 8080, but on Heroku it needs to bind to the environment variable $PORT. You can fix Spring to do this by adding the following to your application.properties:
server.port=${PORT:8080}
This will use $PORT if it's set, and default to 8080 if it's not.
I solved this error using Procfile, the Procfile is always a simple text file that is named Procfile without a file extension, For example, Procfile.txt is not valid, The Procfile must live in your app’s root directory. It does not function if placed anywhere else.
What to write in Procfile?
web: java -jar build/libs/your-project-name-version.jar
the version you can find inside build.gradle/pom.xml
Example:- web: java -jar build/libs/khatabook-1.0.jar
Where to create Procfile?
I have given my project structure image link for
[1]: https://i.stack.imgur.com/tsB35.png
My gradle file link
https://github.com/himanshujainpro/khatabook/blob/master/build.gradle
My application.properties
https://github.com/himanshujainpro/khatabook/blob/master/src/main/resources/application.properties
Project link for further reference
https://github.com/himanshujainpro/khatabook

Spring: Configure PropertyPlaceHolder when Property File outside Jar File

I have a SPRING 2.5.6 based project and my properties file is outside executable Jar file like
./
|---MyApplication.Jar
|---MyApplication.properties
I don't know how to configure PropertyPlaceholderConfigurer so that it can pick the file outise of JAR file or any other way by which spring can know the location of properties file. In other words How to put Jar root path in class-path because spring can automatically pick the properties file from class-path.
I have read the following Questions but it did not exactly tell how to configure PropertyPlaceholderConfigurer.
Read properties file outside JAR file
Add properties file to build path of runnable jar
java -jar -cp . ./main.jar
I know this way of adding root path into class-path but my client do not want to run jar by command line or batch file. So is there a way to configure Spring somehow?
I think of possible solution is to make JAVA base configuration alongside xml base configuration.
java -jar -cp . ./main.jar
Change run command.
Solution- To get the directory where jar is located, spel can be used.
The following should get you going.
<context:property-placeholder location="file:///#{T(java.lang.Object. getClass().getProtectionDomain().getCodeSource().getLocation‌​())}/ application.properties"/>
You can use Maven or Gradle to manage your project, he can help you automatically add dependencies, you do not need to manually import Jar package.

How to refer to a properties file in a batch file

I'm trying to run my Java program using a batch file, I'm able to run it properly. However, when I run the batch file after inserting the code to read a properties file from the Java program, I'm getting the following error.
Can't find bundle for base name app1, locale en_US
Actually i have a conf folder under which I have this properties folder, then I came to know that I need to keep this conf folder in the class path. But I have actually added it as class folder using Eclipse. However, I'm getting the same error. Please let me know what exactly I need to do for running the Java program using batch file. Using Eclipse I'm able to run the properly.
Thanks,
Balaji.
From the comments your batch script:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin com.myapp.app1.demoprogram
pause
Notice how the /conf/ directory is not listed on the classpath. The easiest way to get it there is to just add it to the -classpath argument being passed to the JVM. Something closer to:
#echo off
java -Djava.ext.dirs=lib -classpath ./bin;./conf com.myapp.app1.demoprogram
pause
This is assuming that /conf/ is in the same directory as bin. You might have to do some tweaking to get the setup to work for you application, but the root problem is that while you added the /conf/ folder to the project classpath in eclipse, you need to do the same thing for the batch script so the JVM can find it

Categories