How can I make sure IntelliJ's debugger attaches to the right process?

I’m trying to debug the unit tests in my main project in IntelliJ by right-clicking the file and choosing debug. My main project depends on an artifact from a second project, and that artifact is generated using a JavaExec task. When I execute the debugger, it fails to connect.

I think what’s happening is that the debugger sees the task in the subproject start, attempts to attach to it, and then isn’t available to attach to the tests I actually want to debug. Everything works if I use -x to skip running the JavaExec task, but I have to do this manually every time I want to run a test. Is there a way to ensure that a utility task executed as a part of the build doesn’t confuse the debugger?

The task in the subproject looks like this:

task createFakeData(type: JavaExec) {
  main = 'fakedata.WriteFakeData'
  classpath = sourceSets.main.runtimeClasspath
}
  task jarFakeData(dependsOn: createFakeData, type: Jar) {
  from 'build'
  classifier = 'fakedata'
  include 'fakedata/*'
}
configurations {
  fakeData
}
artifacts {
  fakeData jarFakeData
}

I depend on it from the main project like this:

dependencies {
  compile project(path: ':Model', configuration: 'fakeData')
}

And the error I get when running the debugger looks like this:

FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)
ERROR: transport error 202: connect failed: Connection refused
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugInit.c:750]
ERROR: transport error 202: connect failed: Connection refused
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugInit.c:750]
FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)

Earlier in the log (when running createFakeData) I see the messages “Connected to the target VM, address: ‘127.0.0.1:63495’, transport: ‘socket’” and “Disconnected from the target VM, address: ‘127.0.0.1:63495’, transport: ‘socket’”, which suggests that the debugger briefly connected to the wrong process.

1 Like

> I’m trying to debug the unit tests in my main project in IntelliJ by right-clicking the file and choosing debug. 

When running unit tests via the IDE, the build isn’t involved. I don’t understand what you’re actually doing here.