Spring boot runtime error details - spring

I'm a beginner to spring and spring boot.I'm using command line runner to execute my application.I was facing issue that my application services stops immediately without any error when I run the application. After reviewing all files i came to know that one of autowired variable was not having #component notation in its class.After fixing it started working fine.
My question here is, is there a run time debugger in spring boot to trace these types of errors ?

inside src/main/resources/application.properties or src/main/resources/application.yml add
debug: true
run with debug using spring-boot property
java -jar /path/to/file.jar --debug
run with debug using jvm args
java -Ddebug -jar /path/to/file.jar
also you can ise IDE such Itellij IDEA (in Run Configurations) for your main class
ps: I believe, other IDEs, such as eclipse / STS or NetBeans also should have similar property
set env variable DEBUG=true and run app
5.1. unix bash: DEBUG=true java -jar /path/to/file.jar
5.2. windows cmd:
set DEBUG=true
java -jar path\to\file.jar

Related

Pass Dynamic Port from Gradle boot:run of an application

I am having an application, which is running on some port(ex-8080) now when I start this application using gradlew I want to pass dynamic port to start the application?
./gradlew :testApplication:bootRun
is there anyway to pass the dynamic port here??
Add the following to build.gradle so that we can pass parameters to gradlew along to the underlying java command:
bootRun {
if (project.hasProperty('args')) {
args project.args.split(',')
}
}
Pass the arguments you would normally send to a java command (in this case, overriding the server.port) as -Pargs to gradlew:
/gradlew :testApplication:bootRun -Pargs="--server.port=8081"
What is here:
When you run java with arguments --server.port=8081, Spring Boot will override default property (e.g. Spring Boot will ignore your port in properties file, it will use value from command line
-Pargs is the way to ask bootRun to command line arguments. See details here.
See also the same question for maven.
I couldn't pass the port directly.
But if you want a workaround, do the following:
Build the application with gradle build.
Navigate in your project and open the directory build/libs
Now you have to see the jar of your project and then run this command java -jar yourJarProject.jar --server.port=8081.

Unable to start Spring Boot executable jar using IBM JRE 1.8

We have a Spring Boot application which is built as an executable jar and runs fine using both the Oracle and OpenJDK JREs (using 1.8 versions).
Attempting to run it using the IBM 1.8 JRE however results in the following error at the command line.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
This occurs if we execute the jar (using ./application.jar) or using java -jar application.jar
This led us to change the packaging to not make the jar executable and this allows us to start the application using java -jar application.jar. So it appears the IBM JRE doesn't like the launch script.
The problem is we don't want to have two packaging methods for different deployment environments, if possible.
Does anyone have any experience of why the IBM JRE doesn't like the script on the front of the jar file and whether there are any command line options to disable whatever checking its doing?
From your post it is unclear if you have problem with
1) running jar from Linux like chmod a+x application.jar and executing
Or
2) running via /opt/IBM/java/jre/bin/java -jar application.jar
For option 1) it is not a good idea as you do not explicitly choose jvm binary and rely on OS to choose one for you.
Read about binfmt_misc mechanism:
https://en.m.wikipedia.org/wiki/Binfmt_misc
For option 2)-it might be class loading problem, please add
/opt/IBM/java/jre/bin/java -verbose:class -jar application.jar
and consult documentation here: https://www.ibm.com/developerworks/library/j-dclp1/index.html

Can the IntelliJ IDEA run tab have color?

I am running a spring boot application inside IntelliJ IDEA and noticed a difference if I run it via the run tab (run menu) and via manual command from the terminal tab.
If I run it through a maven run target (play button), I do not see any colors inside the 'run' tab. If I run it via 'mvn spring-boot:run from the 'terminal' tab I see the pretty color highlights. The maven run configuration also runs the same command, spring-boot:run.
Application started from the play button/run configuration (run tab):
Application started from the terminal tab via mvn spring-boot:run (terminal tab):
Inside build.gradle, add the following block to get colorized log output when running your Spring Boot app inside IntelliJ IDEA via gradle bootRun.
bootRun {
jvmArgs = ["-Dspring.output.ansi.enabled=ALWAYS"]
}
It's supported for the Spring Boot Run/Debug configuration type. It explicitly passes
-Dspring.output.ansi.enabled=always
JVM option enabling the color output.
As far as I know, ANSI colors support is not available when you run it in IntelliJ IDEA using Maven or Gradle configurations in the built-in console. Feature request is welcome.
In IDEA 2017.1 EAP I'm getting colours even if I'm just running the application with the standard run command
Edit:
Might be that Community edition doesn't support Spring Boot at all (https://www.jetbrains.com/idea/features/editions_comparison_matrix.html)
You can obtain the very same output also in the Community Edition with the previously mentioned option:
-Dspring.output.ansi.enabled=always
Simply go to "Run" -> "Edit Configurations..." and add the option in the "VM options:" field for your main class.

How do I activate a Spring Boot profile when running from IntelliJ?

I have 5 environments:
- local (my development machine)
- dev
- qc
- uat
- live
- staging
I want different application properties to be used for each environment, so I have the following properties files each which have a different URL for the datasource:
- application.properties (containing common properties)
- application-local.properties
- application-dev.properties
- application-qc.properties
- application-uat.properties
- application-live.properties
I am using IntelliJ and running my app using bootRun in the Gradle plugin on my local machine. I will be using deploying the same application war file on all other environments which run Tomcat.
I have tried adding:
--spring.profiles.active=local
to the run configuration under script parameters.
I have tried adding
-Dspring.profiles.active=local
to the run configuration under VM options.
Neither work. I keep seeing the INFO message on startup say: No active profile set, falling back to default profiles: default
If I run my app from the windows command line using
gradle bootRun
but I first set the environment variable
set SPRING_PROFILES_ACTIVE=local
Then everything works.
So my question is, how do I activate my local spring boot profile when running bootRun from IntelliJ ?
I added -Dspring.profiles.active=test to VM Options and then re-ran that configuration. It worked perfectly.
This can be set by
Choosing Run | Edit Configurations...
Go to the Configuration tab
Expand the Environment section to reveal VM options
If you actually make use of spring boot run configurations (currently only supported in the Ultimate Edition) it's easy to pre-configure the profiles in "Active Profiles" setting.
Spring Boot seems had changed the way of reading the VM options as it evolves. Here's some way to try when you launch an application in Intellij and want to active some profile:
1. Change VM options
Open "Edit configuration" in "Run", and in "VM options", add: -Dspring.profiles.active=local
It actually works with one project of mine with Spring Boot v2.0.3.RELEASE and Spring v5.0.7.RELEASE, but not with another project with Spring Boot v2.1.1.RELEASE and Spring v5.1.3.RELEASE.
Also, when running with Maven or JAR, people mentioned this:
mvn spring-boot:run -Drun.profiles=dev
or
java -jar -Dspring.profiles.active=dev XXX.jar
(See here: how to use Spring Boot profiles)
2. Passing JVM args
It is mentioned somewhere, that Spring changes the way of launching the process of applications if you specify some JVM options; it forks another process and will not pass the arg it received so this does not work. The only way to pass args to it, is:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."
Again, this is for Maven.
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html
3. Setting (application) env var
What works for me for the second project, was setting the environment variable, as mentioned in some answer above: "Edit configuration" - "Environment variable", and:
SPRING_PROFILES_ACTIVE=local
Tested with IntelliJ Community edition 2021.x
You can create Multiple configurations, one each for a specific profile, In my case below, I have created a dev config with dev profile environment variable.
Goto Run > Edit Configuration
Choose the configuration you want to edit, in the left under Application.
On the right side > Under Environment Variable, update spring.profiles.active=<your profile name> example
spring.profiles.active=dev
(observer:- the variable should be without -D flag)
Save the changes and Run the Spring boot app with the same configuration.
Note:- You can also create a new configuration or copy existing in step 2 above, using the option available in the same panel.
Try add this command in your build.gradle
So for running configure that shape:
For Spring Boot 2.1.0 and later you can use
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
I ended up adding the following to my build.gradle:
bootRun {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "local"
}
test {
environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "test"
}
So now when running bootRun from IntelliJ, it defaults to the "local" profile.
On our other environments, we will simply set the 'SPRING_PROFILES_ACTIVE' environment variable in Tomcat.
I got this from a comment found here: https://github.com/spring-projects/spring-boot/pull/592
A probable cause could be that you do not pass the command line parameters into the applications main method. I made the same mistake some weeks ago.
public static final void main(String... args) {
SpringApplication.run(Application.class, args);
}
I use the Intellij Community Edition.
Go to the "Run/Debug Configurations" > Runner tab > Environment variables > click button "...". Add:
SPRING_PROFILES_ACTIVE = local
spring.profiles.active
In my case I used below configuration at VM options in IntelliJ , it was not picking the local configurations but after a restart of IntelliJ it picked configuration details from IntelliJ and service started running.
-Dspring.profiles.active=local
So for resuming...
If you have the IntelliJ Ultimate the correct answer is the one provided by Daniel Bubenheim
But if you don't, create in Run->Edit Configurations and in Configuration tab add the next Environment variable:
SPRING_PROFILES_ACTIVE=profilename
And to execute the jar do:
java -jar -Dspring.profiles.active=profilename XXX.jar
Try this. Edit your build.gradle file as followed.
ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }
You can try the above way to activate a profile
Here are 2 ways
Using gradle project property
In build.gradle, add
bootRun{
//https://github.com/spring-projects/spring-boot/pull/592#issuecomment-880263914
if (project.hasProperty('profiles')) {
environment SPRING_PROFILES_ACTIVE: profiles
} else {
def profiles = 'dev'
environment SPRING_PROFILES_ACTIVE: profiles
}
}
In intellij gradle configuration, change the value "test" in "-Pprofiles" as appropriate to environment you want to run
Using environment property
Follow answer by #Hubert https://stackoverflow.com/a/39749545/3333878
And configure the run configuration as
Create files properties like these
application.properties
application-dev.properties
application-prod.properties
then run
VM option is hidden by default.
Here is the right way to do it
Run->Edit Configurations->Select the application on the left menu->Add VM Options
and then add
-Dspring.profiles.active=<profile_name>
Replace the <profile_name> with the profile, say local
Click Apply & OK.
Set -Dspring.profiles.active=local under program arguments.

How can I debug scala source code in IDEA with SBT Jetty-run?

I found related answer in Debugging Scala code with simple-build-tool (sbt) and IntelliJ . But I still don't get it. Does it mean I need to first config the following in sbt.bat
set SCRIPT_DIR=%~dp0
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Xmx512M -jar "%SCRIPT_DIR%sbt-launch-0.7.5.RC0.jar" %*
And then launch the "Remote" in Run/Debug configurations in IDEA all with default?
After those two steps, the program can stop in the breakpoint when I refresh my web application page?
See my answer in the related question:
Debugging Scala code with simple-build-tool (sbt) and IntelliJ

Resources