[Tutorial] Remote debugging with bukkit

Discussion in 'Resources' started by MikeMatrix, Aug 9, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    MikeMatrix

    Hello everyone.

    In this little tutorial I'm going to show you how to remote debug Plugin/CraftBukkit code with eclipse (of course this works out of the box with java, so no worries. Netbeans has the same feature).

    Let's start of with what you need.
    • Bukkit Installation (preferably local)
    • Eclipse (for this tutorial, I'm not going to show you how to do this in other IDE's)
    • Minecraft (duh')
    • Code to debug (In my case I just threw together a little plugin that broadcasts a message when you break a block)
    Let's start of with Bukkit.
    We need to start it with java in a mode, that enables debugging and the transfer of debug information over network. So how do we do this?
    Simple. Adding parameters. In this case these:
    Code:
    -Xdebug -Xrunjdwp:transport=dt_socket,address=1000,server=y,suspend=n
    Ok let's quickly go over this.
    -Xdebug enables the debug mode of the JVM.
    -Xrunjdwp starts the Java Debug Wire Protocol. In this case as a socket server listening on port 1000. (address => binding address, server (y/n) => enable/disable the JVM as the server side, suspend (y/n) => if y then it will suspend the JVM until someone connects a debugger to it)
    In my case the whole command being executed will look like this:
    Code:
    java -Xmx1G -Xms1G -Xdebug -Xrunjdwp:transport=dt_socket,address=1000,server=y,suspend=n -jar craftbukkit.jar 
    Now the only thing missing is our plugin. This needs to be dropped into the plugins folder, just as you are used to it.
    Ok. This is settled. Now we move on and configure Eclipse, so that it will connect to the JVM and knows where he can look for the code being executed.
    Let's go to "Debug Configurations" and add a new Remote Java Application there and then change the Connection type to Standard (Socket Attach), the Host to the IP running our development server, and the port to the port specified in the launch command. Mine looks like this:
    [​IMG]
    Now we need to tell him which Source code is running there (this list does not need to be up to date). Hit the Source tab and add all the Projects you want to debug. Mine will look like this:
    [​IMG]
    Now the fun part. Run the server with the plugin. You should see something like this in the first line:
    [​IMG]
    Now hit debug in eclipse. This will attach to the JVM.
    After it has attached, I recommend switching to Debug view. There should already be the process shown.
    [​IMG]
    Now you can add Breakpoints. They will pause the Thread running that code and allow you to look into variables and step through the code. Though remember. This will probably kick you in Minecraft, since you are pausing the Thread handling your connection most of the time. => Connection Timeout.
    [​IMG]
    Now let's execute that. When we break something we see something like this:
    [​IMG]
    Now we notice, that we actually wanted to show something completely different, when we break a block. For example, what type of block it was. Normally that means, that we need to change the code, recompile it and redeploy it, then reload the server and test it again. But since we are remote debugging we can easily hotswap such a code. Let's make a change:
    [​IMG]
    Now that's the code we wanted. Let's hit save (this will only work if you have automatic Building enabled for the Project, should be enabled by default though) and break a block ingame.
    [​IMG]
    Swapping code on the fly is so hot. (Oh god what a pun)
    There are many other features, but I think those two are probably the most useful ones in case of Plugin development. I use them a ton and it saves so much time doing so.
    I hope you liked this tutorial and didn't mind the bad formatting or the bad spelling/grammar (aside from the bad jokes).
    Hope this helps you guys develop more awesome things.
    Cheerio
    MikeMatrix
     
  2. Offline

    Hidendra

    +1 debuggers seem daunting at first but are very powerful once you get used to using them, especially since you won't need to restart the server for most changes!

    I use Intellij myself and it definitely saves a lot of time.

    edit: a few images for those of us on Intellij :)

    Run > Edit configurations

    Add a 'Remote' config [​IMG]

    Set the port, apply, save

    [​IMG]

    Run > Debug 'Name' !

    [​IMG]

    Once you 'make' / compile it will prompt you to reload the changes. I'd advise you tick the box to do it automatically.
     
  3. Offline

    chaseoes Retired Staff

    MikeMatrix I attempted to follow your instructions to do it in Eclipse but it fails.
    Code:
    Failed to connect to remote VM. Connection refused.
    Connection refused: connect
    I used the exact same startup line as you and everything, and made sure it was set to port 1000 and localhost.
     
  4. Offline

    MikeMatrix

    Could be that you have that port in use. I can't really tell what is happening there, but somehow it does not accept your connection. You should also look into the server log (right at the beginning) there might be some information on what went wrong.
     
  5. Offline

    chaseoes Retired Staff

    No information:
    [​IMG]

    I've tried using a different port, nothing.
     
  6. Offline

    theguynextdoor

    Took 3 mins to set up and works beautifully. Nice tutorial. I shall make good use of this in future
     
  7. Offline

    MikeMatrix

    Good to hear. That was what I was aiming for. Not a lot of people seem to know about this.

    Can you send me a screenshot of your configuration in eclipse?
     
  8. Offline

    chaseoes Retired Staff

    [​IMG]
     
  9. Offline

    MikeMatrix

    Well that is odd.
    Is the bukkit server on the same machine, running before you start debugging?
    If yes, then I have no idea, what would cause this.
     
  10. Offline

    Hidendra

    if it's your local computer try using 127.0.0.1 as the host, perhaps your hosts file is garbled.
     
  11. Offline

    thedeadlybutter

    Great post Mike!
     
  12. Offline

    Unlucky4ever

    Very nice post, I can use this to debug much faster :D
     
  13. Offline

    chaseoes Retired Staff

    Same result, I can connect to the server fine using "localhost".
     
  14. Offline

    BlockCat

    Thanks! Didn't know this!
     
  15. Offline

    Jnorr44

    Have you figured this out yet? I would really like to know, because I am getting the same problem, and this is what I have been looking for forever now.
     
    EstJoel likes this.
  16. Offline

    The_Coder

    MikeMatrix
    I have a question will this work with other computers on a network.
    Example: I have a bukkit server running on another computer, but I do my coding on a different one.
    Will this work or not
     
  17. Offline

    Giant

    The_Coder assuming you change the host to the local IP of that computer, it should work! :)

    Your local IP would be the one your router hands to your computer, not the one you obtain from your ISP!
     
  18. Offline

    The_Coder

    Yes I figured that I just wanted to know.
    thanks for helping

    Giant
    where do I put in the ip of the eclipse computer in this line
    java -Xmx1G -Xms1G -Xdebug -Xrunjdwp:transport=dt_socket,address=1000,server=y,suspend=n -jar craftbukkit.jar

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  19. Offline

    Giant

    that is the computer you want to listen to, you would put the IP for that computer in the "Host" field in eclipse! :)
     
  20. Offline

    chaseoes Retired Staff

    Nope. I really wish I could use this too.
     
  21. Offline

    Jnorr44

    It would be great to figure out. Have you seen the "Programming with Notch" videos on youtube? He uses debugging and it's really cool, because he edits, then he plays, and he never has to stop/start minecraft again.
     
  22. Offline

    NinjaW0lf

    +1
    Really Fucking cool.
    Extremely useful.
    Nuff said.
     
  23. Offline

    Lolmewn Retired Staff

    In Netbeans:
    [​IMG]
    [​IMG]
     
    Europia79 and Feaelin like this.
  24. Offline

    cocoson

    seems cool in theory but i cant seem to get it to work have followed your tut and googled it for about a hour and tried a lot of what they suggested but nothing i do seems to make it work

    nvm figured it out
    chaseoes
    if your still needing help the reason its saying you cannot connect is cause its already connected just open the debug mode and it should show you that your connected

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  25. Offline

    coaster3000

    I Freaking Love You!!!
    Thankyou so much!!! I knew you can do realtime debugging. But I never knew how to debug a plugin since you must actually run it in another jar then the plugin...

    This has helped me so much!!
     
  26. Offline

    cnpi

    I have a problem here... now you said

    How would I add this to the plugins folder. From what I see you never add a JAR file or anything, nor a project folder - which I have tried, but since it's not a JAR, CraftBukkit can't load the plugin.

    Could you explain in more detail how I should go on with this? If I have to "attach" the JAR executable after export, how would I do that?

    I don't have any issues with running the remote debug and attach to CraftBukkit, but I can't debug it if CraftBukkit haven't loaded the plugin I wan't to debug.

    Hope you can help :)
     
  27. Offline

    MikeMatrix

    This is explaining how to debug a plugin, meaning the plugin needs to be running on the server. What I refer to in that statement as "plugin" is the compiled jar file (the plugin) that needs to be dropped into the server's plugin folder.
     
  28. Offline

    Adamki11s

    MikeMatrix

    Thank you so much for this tutorial :).

    I have always wanted to implement something like this to speed up testing but have been too lazy, your tutorial was simple and easy to follow and will save me a lot of time in the future.
     
  29. Offline

    ftbastler

    Tested it, this is sooo great! :)
     
  30. Offline

    vYN

    chaseoes If you still have problems with this. I guess you hit debug once. and nothing happens right?
    Then it is connected. And if you hit it again. it will give you that error.

    just go to Window->Open Perspective and select debug.

    Anyway great tutorial MikeMatrix :)
     
Thread Status:
Not open for further replies.

Share This Page