Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Getting the remote debugger to work with a java process running in a vagrant vm

Vagrant vm network config

In the Vagrantfile, do :

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network :public_network

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  config.ssh.forward_agent = true

And:

  # The java process in the VM will listen on port 4040, which is the same port that the remote debugger will connect to
  # They don't have to be the same, but...why the heck not?
  config.vm.network :forwarded_port, guest: 4040, host: 4040

Setup ssh port forwarding from the 'host' machine to the VM:

  1. From the (host) 'real' machine, do:
  #Here, -L 4044 is the port that the IDE debugger will connect to (in the 'host' - real - machine)
  #and the :4044 on the right of the IP address is the port where the java process being debugged will listen on
  
  ssh -p2222 -f -N -L 4044:127.0.0.1:4044 vagrant@localhost (runs in the background)
  
  ssh -p2222 -N -L 4044:127.0.0.1:4044 vagrant@localhost (runs in the foreground)
  1. Start the java process to be debugged (in the VM):
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=40444 -jar ./target/test.jar

Note the 'suspend=y' here : this means that the (command line java application) will 'wait' for the debugger to connect; If you are launching a webservice (say, a jetty/dropwizard), then, this could be 'suspend=n'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment