Technology

Writing Java in VSCode and Understanding What Pressing F5 Does

Most developer blogs are in the structure of learn a thing, write a thing. This is where we find something we need to do, we learn how to do it, then we document what we did so that others can follow it. This feels very helpful with something is obscure or not documented well. However that is not the case here.

If we want to run, write and debug Java using Visual Studio Code, then we follow the well-written documentation here at Getting Started with Java in VS Code.

That’s it. Follow that. There’s nothing to add. Except… except…

When instructing us on how to run code we’re told…

To run and debug Java code, set a breakpoint, then either press F5 on your keyboard or use the Run > Start Debugging menu item.

https://code.visualstudio.com/docs/java/java-tutorial#_running-and-debugging-your-program

Yeah but what pressing F5 actually do?

Pressing F5 not only compiles the code for us, it also runs the code. Pressing F5 is similar to doing this…

javac ./src/App.Java -d ./bin
cd bin
java App

This not only compiles the code written in App.Java but also runs the code that exists in App.class with the added bonus that we can debug the code using flags -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:58965

These flags show how to load the Java Debug Wire Protocol (jdwp) library and listen for the socket connection on port 58965 (https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html).

Now we know a little more about what happens when we press F5 to compile and debug our Java code in VSCode.

Leave a Reply

Your email address will not be published. Required fields are marked *