|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * JDWP Port Listener and Communication Wrapper |
| 4 | + * |
| 5 | + * This script wraps Java process execution and captures the JDWP port |
| 6 | + * from the JVM output, then writes it to the endpoint file for VS Code |
| 7 | + * to pick up and attach the debugger. |
| 8 | + * |
| 9 | + * JDWP Output Format: |
| 10 | + * "Listening for transport dt_socket at address: 12345" |
| 11 | + */ |
| 12 | + |
| 13 | +const { spawn } = require('child_process'); |
| 14 | +const fs = require('fs'); |
| 15 | +const path = require('path'); |
| 16 | + |
| 17 | +// Get environment variables |
| 18 | +const endpointFile = process.env.JDWP_ADAPTER_ENDPOINTS || process.env.VSCODE_JDWP_ADAPTER_ENDPOINTS; |
| 19 | +const javaToolOptions = process.env.JAVA_TOOL_OPTIONS || ''; |
| 20 | + |
| 21 | +// Check if debugging is enabled |
| 22 | +const isDebugEnabled = javaToolOptions.includes('jdwp') && endpointFile; |
| 23 | + |
| 24 | +// Helper function to find java command |
| 25 | +function getJavaCommand() { |
| 26 | + // Priority 1: Try JAVA_HOME environment variable first (user's explicit choice) |
| 27 | + const javaHome = process.env.JAVA_HOME; |
| 28 | + if (javaHome) { |
| 29 | + const javaPath = path.join(javaHome, 'bin', 'java'); |
| 30 | + const javaPathExe = process.platform === 'win32' ? `${javaPath}.exe` : javaPath; |
| 31 | + |
| 32 | + // Check if the file exists |
| 33 | + if (fs.existsSync(javaPathExe)) { |
| 34 | + return javaPath; |
| 35 | + } |
| 36 | + if (fs.existsSync(javaPath)) { |
| 37 | + return javaPath; |
| 38 | + } |
| 39 | + |
| 40 | + console.warn(`[Java Debug] JAVA_HOME is set to '${javaHome}', but java command not found there. Falling back to VS Code's Java.`); |
| 41 | + } |
| 42 | + |
| 43 | + // Priority 2: Use VSCODE_JAVA_EXEC if provided by VS Code (from Java Language Server) |
| 44 | + const vscodeJavaExec = process.env.VSCODE_JAVA_EXEC; |
| 45 | + if (vscodeJavaExec && fs.existsSync(vscodeJavaExec)) { |
| 46 | + return vscodeJavaExec; |
| 47 | + } |
| 48 | + |
| 49 | + // Priority 3: Fall back to 'java' in PATH |
| 50 | + return 'java'; |
| 51 | +} |
| 52 | + |
| 53 | +const javaCmd = getJavaCommand(); |
| 54 | + |
| 55 | +if (!isDebugEnabled) { |
| 56 | + // No debugging, just run java normally |
| 57 | + const child = spawn(javaCmd, process.argv.slice(2), { |
| 58 | + stdio: 'inherit', |
| 59 | + shell: false |
| 60 | + }); |
| 61 | + child.on('exit', (code) => process.exit(code || 0)); |
| 62 | + child.on('error', (err) => { |
| 63 | + console.error(`[Java Debug] Failed to start java: ${err.message}`); |
| 64 | + console.error(`[Java Debug] Make sure Java is installed and either JAVA_HOME is set correctly or 'java' is in your PATH.`); |
| 65 | + process.exit(1); |
| 66 | + }); |
| 67 | +} else { |
| 68 | + // Debugging enabled, capture JDWP port |
| 69 | + const child = spawn(javaCmd, process.argv.slice(2), { |
| 70 | + stdio: ['inherit', 'pipe', 'pipe'], |
| 71 | + shell: false |
| 72 | + }); |
| 73 | + |
| 74 | + let portCaptured = false; |
| 75 | + const jdwpPortRegex = /Listening for transport dt_socket at address:\s*(\d+)/; |
| 76 | + |
| 77 | + // Shared function to capture JDWP port from output |
| 78 | + const capturePort = (output) => { |
| 79 | + if (portCaptured) return; |
| 80 | + |
| 81 | + const match = output.match(jdwpPortRegex); |
| 82 | + if (match && match[1]) { |
| 83 | + const port = parseInt(match[1], 10); |
| 84 | + |
| 85 | + // Validate port range |
| 86 | + if (port < 1 || port > 65535) { |
| 87 | + console.error(`[Java Debug] Invalid port number: ${port}`); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + console.log(`[Java Debug] Captured JDWP port: ${port}`); |
| 92 | + |
| 93 | + // Write port to endpoint file |
| 94 | + const endpointData = JSON.stringify({ |
| 95 | + client: { |
| 96 | + host: 'localhost', |
| 97 | + port: port |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + try { |
| 102 | + fs.writeFileSync(endpointFile, endpointData, 'utf8'); |
| 103 | + console.log(`[Java Debug] Wrote endpoint file: ${endpointFile}`); |
| 104 | + portCaptured = true; |
| 105 | + } catch (err) { |
| 106 | + console.error(`[Java Debug] Failed to write endpoint file: ${err}`); |
| 107 | + } |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + // Monitor stdout for JDWP port |
| 112 | + child.stdout.on('data', (data) => { |
| 113 | + const output = data.toString(); |
| 114 | + process.stdout.write(data); |
| 115 | + capturePort(output); |
| 116 | + }); |
| 117 | + |
| 118 | + // Monitor stderr for JDWP port (it might appear on stderr) |
| 119 | + child.stderr.on('data', (data) => { |
| 120 | + const output = data.toString(); |
| 121 | + process.stderr.write(data); |
| 122 | + capturePort(output); |
| 123 | + }); |
| 124 | + |
| 125 | + child.on('exit', (code) => process.exit(code || 0)); |
| 126 | + child.on('error', (err) => { |
| 127 | + console.error(`[Java Debug] Failed to start java: ${err}`); |
| 128 | + process.exit(1); |
| 129 | + }); |
| 130 | +} |
0 commit comments