I'm developing a project using Vert.X framework using Gradle build tool. The problem I have is that breakpoints in IntelliJ simply doesn't work in any way I've tried.
Here is a run configuration for Vert.X which I use in Gradle:
run {
args = [
'run', mainVerticleName,
"-conf", confPath,
"--redeploy=$project.ext.watchForChange",
"--launcher-class=$mainClassName",
"--on-redeploy=$project.ext.doOnChange"
]
}
So the deployment is done using Gradle, runs without any issues, IntelliJ debugger is connected, but breakpoints are not working.
The ways I've tried to make it work:
1) Gradle run configuration. Here is a run configuration for Intellij IDEA:
Tried to use a Remote debugging tool, started application with the following VM options:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000
But this didn't work.
2) Application configuration:
In this case I just can't start the project as I get the following message on the startup:
Error: Could not find or load main class io.vertx.core.Launcher
Vert.X Core library is in the classpath and configuration seems to be correct, so can't get were is the problem.
The source code of the project is public and can be found on GitHub:
vertx-gradle-architecture-starter
Vert.X version - 3.4.0. Gradle version - 3.4.1. IntelliJ IDEA version - 2016.3.5. OS - MacOS Sierra 10.12.3.
Interesting fact is when I deploy Vert.X from tests - breakpoints work. Any ideas why breakpoints doesn't work in cases I've described above?
Here are solutions to both issues. Thanks to #CrazyCoder for help on this.
1) run command is run in separate VM. So, to make it work, I've added --java-opts argument to the script:
run {
args = [
'run', mainVerticleName,
"-conf", confPath,
"--redeploy=$project.ext.watchForChange",
"--launcher-class=$mainClassName",
"--on-redeploy=$project.ext.doOnChange",
// used for attaching remote debugger
"--java-opts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"
]
}
This allows to attach Remote debug configuration on port 8000.
2) By default, Intellij IDEA creates separate modules per source sets, so I had source sets for api_main and api_test modules. After turning off this feature - Application debug run started to work.
This can be turned off in Gradle Settings. Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle: uncheck create separate modules per source set.
This is an IntelliJ IDEA issue - reference.
I had the exact same issue and following worked for me. redeploy, launcher and on-redeploy options are not necessary in intelliJ. if we remove those the debug works after application is up.
run {
args = [
'run', mainVerticleName,
"-conf", confPath
]
}
Related
I want to set up Kafka source code with IntelliJ IDEA and I am able to successfully build the project but the Run configurations are incorrect.
I have loaded and successfully build the Gradle project in IDEA. I looked around and found a blog post that configures this but some of the run configurations he stated are just not possible. For eg, there is no class kafka.Kafka and the -cp flag is not core_main as stated in the module.
My setup is as following:
How do I setup the run configurations so that I can execute and debug Kafka's source code?
The kafka.Kafka is here. It is a scala class.
Install Scala plugin. Press double shift shortcut and go to Kafka.scala file. Click on the green triangle next to the main method. This will create a run configuration. You can then edit it to add any custom stuff.
I use IntelliJ IDEA CE in two environments (CentOS and Ubuntu).
The two share a project via GitHub.
In CentOS, the project is regarded as Java project? with a file build.gradle.
In the other, the project is fully managed as Gradle-Java project.
When I run a main method in CentOS, IDE just runs Java (after some compiles if are required)
On the other hand, when I run the same main method in Ubuntu, IDE activates Gradle and run that.
I think this difference happens from the module structures.. which I have not tried though....
(Am I wrong?)
One question I really want to know here is,
in CentOS, when I run a code, it shows how IDE ran the code in the Run window, like...
java [too many options] foo.target.Hoge
If I copy and paste this line, I can activate foo.target.Hoge anywhere, even where I do not set CLASSPATH because [too many options] contain that.
I always do it (I am afraid if I should not though)
However, in Ubuntu (Gradle-based environment)
the windows show only
time PM: Executing task 'Hoge.main()'...
I can not copy, paste and run it...
I want to know where I can found the actual command activated.
or.... using Gradle.. are there any simpler ways?? like
gradle run Java Hoge ...
When I run a main method in CentOS, IDE just runs Java (after some compiles if are required) On the other hand, when I run the same main method in Ubuntu, IDE activates Gradle and run that.
For Gradle-based projects this behaviour is controlled in Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Build and run using option. When Gradle is set IDE uses Gradle to run and build the project. When IntelliJ is set IDE uses it's own builder and Run/Debug Configurations to run.
Not an expert but Gradle is what the (Spring Boot) project is using. I have a #Component which runs setting up dummy data, but I only want it to run locally. So now I have put an #Profile("local123") over it too and this turns the component off. Great.
Next I wanted to put in Eclipse's Run Configurations... > JVM Arguments the -Dspring.profiles.active=local123 so that the component is turned on but only when I'm running my build locally. No joy. I have seen some posts that say you need to add this or similar to build.gradle
bootRun {
systemProperties System.properties
}
but anywhere I try to add it in the file, it causes my build to crash.
Where am I going wrong? and/or
Where do I get documentation on the structure of build.gradle to work out where this should go!
I am using IntelliJ 13.1 on Linux and try to build a basic REST application following the example http://spring.io/guides/gs/spring-boot/
When I execute the target bootRun from within IntelliJ the intergrated tomcat server is started and everything works fine.
10:09:35: Executing external task 'bootRun'...
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:findMainClass
:bootRun
But stopping the execution does not affect the gradle task:
10:11:40: External task execution finished 'bootRun'.
I still can access the tomcat instance on the configured port. Only closing IntelliJ will destroy the process.
I searched on this topic but could not find any reasonable answers. I wonder if it is a
gradle problem (using gradle wrapper version 1.11)
Linux problem (Ubuntu 12.04)
IntelliJ problem (IDEA 13.1.2)
Spring Boot problem (tried different versions from 1.0.0.RELEASE to 1.1.0.M2)
I am pretty sure it's an IntelliJ issue because:
When you run the bootRun from IntelliJ's Gradle integration and then press stop, the application continues to run as you mentioned.
However, if you use the command line and run gradle bootRun and then use Cntrl+C, the application stops just fine.
Note that my setup is Windows 7, IntelliJ 13.1.3, Gradle 1.12 and Spring Boot 1.1.0.M2.
If your need to is to be able to easily debug your application, all you need to do is run the Java (or Groovy) Class that has the main method, since that it is the easiest way to run Spring Boot applications! Kudos to Spring Boot for that!!!
Of course you can continue to use Gradle for tasks like running all the tests or creating the packaged application.
It's may not perfect solution. But for a quick fix, it will help you.
Goto terminal and apply this command to terminate the process
sudo kill -9 `sudo lsof -t -i:8080`
Note: Replace your port number instead of 8080
I know this answer was already answered, but as this is first topic when searching in Google, I thought in sharing an alternative I went with and didn't find in other topics I read.
My main problem with this terminate was that always I changed something in my classes, I needed to restart the server, and it would fail so I needed to stop/start the server sometimes.
To avoid this, I use spring-boot-devtools. Basically:
Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change.
The only catch is that on Intellij it only makes the project if click Build > Make Project, so either set it to build automatically or remember to click on make.
To use, simply add the dependency to your build.gradle / pom.xml:
compile('org.springframework.boot:spring-boot-devtools')
I found this to be really useful in development.
I am using IntelliJ in Mac, running Spring boot application was giving the same issue. Whenever I run the app in Terminal and use CTRL + Z, the terminal will strop running the app. But when I run the app again in the same terminal, will give error saying the port 8080 was in use.
I just tried the option to close the Terminal session ( right click -> select -> Close Session). This will close the terminal, and when I clicked the terminal option again and ran the app, it ran smoothly.
How do you debug java annotation processors using intellij?
Preferably using IDEA IntelliJ. I tried setting a breakpoint inside the processor and running but it did not break.
If you really need to debug an annotation processor, it might be better to run the annotation processor from the command line rather than within your IDE with debugging enabled and attach to that using your IDE's debugger.
If running javac directly, you can debug this by specifying the following extra parameters:
javac -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 ... (usual javac parameters go here)
If running Maven, use mvndebug instead of the standard mvn command - Maven runs the compiler in-process.
If running Ant, add the following to the ANT_OPTS environment variable before running:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
With all these executions, the javac, Ant or Maven process will wait for you to attach your debugger before it actually starts executing. IntelliJ instructions for this are here. For Eclipse, here.
This tutorial is written for an Android project. Main module name is "app" as usual. The project contains a submodule called "annotation" which is subdependency of "app". "app" module runs annotation processing with gradle declaration
apt project(':annotation') .
SIMPLE VERSION
(run compilation from terminal and attach from IDE)
[REQUIRED] Add a new project configuration "+" -> "Remote". Check "Single instance only". All other settings are generated automatically. Leave <whole project> as the classpath. Port should be left as the default 5005.
[REQUIRED] Make sure you stop all gradle instances by calling: ./gradlew --stop
[REQUIRED] Run the command : ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac
Run the APT project configuration in debug mode as fast as possible :)
[HINT] We start with an EMPTY gradle.properties file
[HINT] DO NOT USE gradle daemon ( --no-daemon / org.gradle.daemon=false option )
[HINT] Run gradle in debug mode ( org.gradle.debug=true option )
[HINT] Run app's module compilation not the processor's module compilation (app's compilation runs annotation processing!)
We DO NOT normally add any Java compiler settings in Android Studio (i.e. File -> other settings -> Default settings)
EXTENDED VERSION (use gradle.properties)
Add the following to your gradle.properties file:
org.gradle.daemon=false
org.gradle.debug=true
Run the compilation from terminal:
./gradlew :app:clean :app:compileDebugJavaWithJavac
ADVANCED VERSION (just press debug in IDE)
Add a bash script to your project main dir (e.g. compile.sh)
#!/bin/bash
./gradlew :app:clean :app:compileDebugJavaWithJavac &
Remember about the '&' sign for background processing.
Go to APT configuration settings we created in step 1 and add a Before launch configuration. Select Run external tool.
Add the path to the compile.sh script we created earlier.
Warning
Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). If you cannot stop gradle from the IDE then use this command in the terminal:
ps -A | grep gradle | awk '{ print $1; }' | xargs kill -9
Turning off debug option during project refresh sometimes helps Android Studio to come back to the right track.
Follow these steps, These worked for me on android studio for gradle project:-
1).In gradle.properties add following lines
org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
2).Edit Build COnfiguration and add Remote Configration
3).Run recently created run configuration APT.
4).Add break point in annotation processing code and build overall project
It is possible to run javac and debug it, as indicated higher. However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. So in the module where I wanted to apply my annotation processor, just create a class with main method.
public static void main(String[] args) {
com.sun.tools.javac.Main.main("-proc:only",
"-processor", "my.pkgs.MyAnnotationProcessor",
"my/pkgs/any/ClassIWantProcess.java");
}
For this to work you need to add $JAVA_HOME/lib/tools.jar to your SDK's JARs (by default it is not there). This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler.
Then just set proper working directory for your run configuration (so the relative path to the java file is correct), set your break-point into the processor and debug at your will!
Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. I believe it's possible to translate it to other IDEs too.
For a Maven project, this post, which explains the following steps in a little more detail, worked for me:
Add "Remote" run configuration and set "port" to 8000.
Issue the command mvnDebug clean install from the project's directory (on the command line).
Run the run configuration. In order to start a new session after the processes quit, repeat from (2).
Remember to run mvn install on the project's dependencies when they change (e.g. if the annotation processor is in a different artifact than the project you are debugging it from).
I found the following resource that can help you: http://code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse
The guy explains step-by-step how to debug annotation processors using Eclipse.
Annotation processing occurs during compilation, so normal debugging won't work. If you want to debug it in the context of you project, you can use IntelliJ remote debugging, while having Gradle or Maven in debug mode. Then you can put breakpoints in the Annotation Processor's files.
See Debugging an Annotation Processor in any project.
Disclaimer: I wrote the post.
Debugging an annotation processor with IntelliJ IDEA and Gradle
Set a custom VM option -Dcompiler.process.debug.port=5005: press Ctrl + Shift + A and select Edit Custom VM Options... in the list of actions to add a custom VM option then restart the IDE.
Create a remote debug configuration with default parameters: Run -> Edit Configurations... -> Add New Configuration (Alt + Insert) -> Remote.
Set breakpoints.
Build with Gradle from the terminal: $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build (it's okay if the execution of the command is frozen, don't terminate a process).
Debug the remote debug configuration within the IDE (see step 3): select a suitable remote debug configuration and press Shift + F9.
Hope it helps somebody :)