|
1 | 1 | # Java No-Config Debug Wrapper Script for PowerShell |
2 | 2 | # This script intercepts java commands and automatically enables JDWP debugging |
3 | 3 |
|
4 | | -# Export the endpoint file path for JDWP port communication |
5 | | -$env:JDWP_ADAPTER_ENDPOINTS = $env:VSCODE_JDWP_ADAPTER_ENDPOINTS |
6 | | - |
7 | | -# Set JDWP options only for this debugjava invocation |
8 | | -# This overrides the global JAVA_TOOL_OPTIONS to avoid affecting other Java processes |
9 | | -$env:JAVA_TOOL_OPTIONS = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0" |
10 | | - |
11 | 4 | # Get the directory of this script |
12 | 5 | $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
13 | 6 |
|
14 | | -# Use Node.js wrapper to capture JDWP port |
15 | | -& node (Join-Path $scriptDir "jdwp-wrapper.js") $args |
| 7 | +# Save current environment variables to restore later |
| 8 | +$oldJavaToolOptions = $env:JAVA_TOOL_OPTIONS |
| 9 | +$oldJdwpAdapterEndpoints = $env:JDWP_ADAPTER_ENDPOINTS |
| 10 | + |
| 11 | +try { |
| 12 | + # Set environment variables only for this debugjava invocation |
| 13 | + $env:JDWP_ADAPTER_ENDPOINTS = $env:VSCODE_JDWP_ADAPTER_ENDPOINTS |
| 14 | + $env:JAVA_TOOL_OPTIONS = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0" |
| 15 | + |
| 16 | + # Use Node.js wrapper to capture JDWP port |
| 17 | + & node (Join-Path $scriptDir "jdwp-wrapper.js") $args |
| 18 | +} |
| 19 | +finally { |
| 20 | + # Restore original environment variables to avoid affecting subsequent commands |
| 21 | + if ($null -eq $oldJavaToolOptions) { |
| 22 | + Remove-Item Env:\JAVA_TOOL_OPTIONS -ErrorAction SilentlyContinue |
| 23 | + } else { |
| 24 | + $env:JAVA_TOOL_OPTIONS = $oldJavaToolOptions |
| 25 | + } |
| 26 | + |
| 27 | + if ($null -eq $oldJdwpAdapterEndpoints) { |
| 28 | + Remove-Item Env:\JDWP_ADAPTER_ENDPOINTS -ErrorAction SilentlyContinue |
| 29 | + } else { |
| 30 | + $env:JDWP_ADAPTER_ENDPOINTS = $oldJdwpAdapterEndpoints |
| 31 | + } |
| 32 | +} |
0 commit comments