|
| 1 | +# Testing Java No-Config Debug |
| 2 | + |
| 3 | +## Quick Test |
| 4 | + |
| 5 | +1. **Create a simple Java file** (`HelloWorld.java`): |
| 6 | + |
| 7 | +```java |
| 8 | +public class HelloWorld { |
| 9 | + public static void main(String[] args) { |
| 10 | + System.out.println("Starting application..."); |
| 11 | + |
| 12 | + String message = "Hello, World!"; |
| 13 | + System.out.println(message); // Set a breakpoint here |
| 14 | + |
| 15 | + for (int i = 0; i < 3; i++) { |
| 16 | + System.out.println("Count: " + i); |
| 17 | + } |
| 18 | + |
| 19 | + System.out.println("Application finished."); |
| 20 | + } |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +2. **Compile it**: |
| 25 | +```bash |
| 26 | +javac HelloWorld.java |
| 27 | +``` |
| 28 | + |
| 29 | +3. **Set a breakpoint** on the line with `System.out.println(message);` |
| 30 | + |
| 31 | +4. **Run with javadebug**: |
| 32 | +```bash |
| 33 | +javadebug HelloWorld |
| 34 | +``` |
| 35 | + |
| 36 | +5. **Result**: |
| 37 | + - The debugger should automatically attach |
| 38 | + - Execution should pause at your breakpoint |
| 39 | + - You can inspect variables, step through code, etc. |
| 40 | + |
| 41 | +## Advanced Test - With Arguments |
| 42 | + |
| 43 | +Create `EchoArgs.java`: |
| 44 | + |
| 45 | +```java |
| 46 | +public class EchoArgs { |
| 47 | + public static void main(String[] args) { |
| 48 | + System.out.println("Arguments received: " + args.length); |
| 49 | + |
| 50 | + for (int i = 0; i < args.length; i++) { |
| 51 | + System.out.println("Arg " + i + ": " + args[i]); // Breakpoint here |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Compile and run: |
| 58 | +```bash |
| 59 | +javac EchoArgs.java |
| 60 | +javadebug EchoArgs arg1 arg2 "arg with spaces" |
| 61 | +``` |
| 62 | + |
| 63 | +## Test with JAR |
| 64 | + |
| 65 | +Create a JAR with manifest: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Compile |
| 69 | +javac HelloWorld.java |
| 70 | + |
| 71 | +# Create manifest |
| 72 | +echo "Main-Class: HelloWorld" > manifest.txt |
| 73 | + |
| 74 | +# Create JAR |
| 75 | +jar cfm hello.jar manifest.txt HelloWorld.class |
| 76 | + |
| 77 | +# Debug the JAR |
| 78 | +javadebug -jar hello.jar |
| 79 | +``` |
| 80 | + |
| 81 | +## Test Terminal History |
| 82 | + |
| 83 | +One of the key benefits is easy parameter modification: |
| 84 | + |
| 85 | +```bash |
| 86 | +# First run |
| 87 | +javadebug EchoArgs test1 test2 |
| 88 | + |
| 89 | +# Press ↑ to recall, modify and run again |
| 90 | +javadebug EchoArgs different parameters |
| 91 | + |
| 92 | +# Press ↑ again, modify again |
| 93 | +javadebug EchoArgs yet another test |
| 94 | +``` |
| 95 | + |
| 96 | +This is much faster than editing launch.json each time! |
| 97 | + |
| 98 | +## Verify Environment Variables |
| 99 | + |
| 100 | +In your VS Code terminal, check that environment variables are set: |
| 101 | + |
| 102 | +**Unix/Linux/macOS**: |
| 103 | +```bash |
| 104 | +echo $JAVA_TOOL_OPTIONS |
| 105 | +echo $VSCODE_JDWP_ADAPTER_ENDPOINTS |
| 106 | +which javadebug |
| 107 | +``` |
| 108 | + |
| 109 | +**Windows (PowerShell)**: |
| 110 | +```powershell |
| 111 | +$env:JAVA_TOOL_OPTIONS |
| 112 | +$env:VSCODE_JDWP_ADAPTER_ENDPOINTS |
| 113 | +Get-Command javadebug |
| 114 | +``` |
| 115 | + |
| 116 | +**Windows (CMD)**: |
| 117 | +```cmd |
| 118 | +echo %JAVA_TOOL_OPTIONS% |
| 119 | +echo %VSCODE_JDWP_ADAPTER_ENDPOINTS% |
| 120 | +where javadebug |
| 121 | +``` |
| 122 | + |
| 123 | +## Expected Output |
| 124 | + |
| 125 | +When you run `javadebug`, you should see something like: |
| 126 | + |
| 127 | +``` |
| 128 | +Picked up JAVA_TOOL_OPTIONS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0,quiet=y |
| 129 | +[Java Debug] Captured JDWP port: 54321 |
| 130 | +[Java Debug] Wrote endpoint file: C:\...\endpoint-abc123.txt |
| 131 | +Starting application... |
| 132 | +``` |
| 133 | + |
| 134 | +Then VS Code should show: |
| 135 | +- Debug toolbar appears |
| 136 | +- Breakpoint icon turns solid red (was hollow) |
| 137 | +- Execution pauses at your breakpoint |
| 138 | + |
| 139 | +## Common Issues |
| 140 | + |
| 141 | +### Issue: "javadebug: command not found" |
| 142 | + |
| 143 | +**Solution**: |
| 144 | +- Close and reopen your terminal |
| 145 | +- The extension must be activated first |
| 146 | +- Check that the extension is installed and enabled |
| 147 | + |
| 148 | +### Issue: Debugger doesn't attach |
| 149 | + |
| 150 | +**Solution**: |
| 151 | +- Check the Debug Console for errors |
| 152 | +- Verify JAVA_TOOL_OPTIONS is set |
| 153 | +- Try setting a breakpoint before running |
| 154 | + |
| 155 | +### Issue: "Address already in use" |
| 156 | + |
| 157 | +**Solution**: |
| 158 | +- Stop any existing debug sessions |
| 159 | +- Wait a few seconds and try again |
| 160 | +- The port from a previous session might still be bound |
| 161 | + |
| 162 | +## Comparison with Traditional Debugging |
| 163 | + |
| 164 | +### Traditional Way (with launch.json): |
| 165 | + |
| 166 | +1. Create `.vscode/launch.json` |
| 167 | +2. Configure: |
| 168 | +```json |
| 169 | +{ |
| 170 | + "type": "java", |
| 171 | + "name": "Debug HelloWorld", |
| 172 | + "request": "launch", |
| 173 | + "mainClass": "HelloWorld", |
| 174 | + "args": ["arg1", "arg2"] |
| 175 | +} |
| 176 | +``` |
| 177 | +3. Press F5 |
| 178 | +4. To change args: Edit launch.json, save, press F5 |
| 179 | + |
| 180 | +### No-Config Way: |
| 181 | + |
| 182 | +1. Set breakpoint |
| 183 | +2. Run: `javadebug HelloWorld arg1 arg2` |
| 184 | +3. To change args: Press ↑, edit command, press Enter |
| 185 | + |
| 186 | +**Much faster! 🚀** |
0 commit comments