Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions workspaces/ballerina/ballerina-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to the **Ballerina** extension will be documented in this fi

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project adheres to [Semantic Versioning](https://semver.org/).

## [5.7.1] - 2026-01-21

### Fixed

- **Environment** — Fix comprehensive fallback JDK detection logic that checks JAVA_HOME environment variable.


## [5.7.0](https://github.com/wso2/vscode-extensions/compare/ballerina-integrator-1.5.4...ballerina-integrator-1.6.0) - 2026-01-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion workspaces/ballerina/ballerina-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ballerina",
"displayName": "Ballerina",
"description": "Ballerina Language support, debugging, graphical visualization, AI-based data-mapping and many more.",
"version": "5.7.0",
"version": "5.7.1",
"publisher": "wso2",
"icon": "resources/images/ballerina.png",
"homepage": "https://wso2.com/ballerina/vscode/docs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { debug, log } from '../logger';
import { ServerOptions, ExecutableOptions } from 'vscode-languageclient/node';
import { isWindows } from '..';
import { BallerinaExtension } from '../../core';
import { isSupportedSLVersion, createVersionNumber } from '../config';
import { isSupportedSLVersion, createVersionNumber, isWSL } from '../config';
import * as fs from 'fs';
import * as path from 'path';
import { orderBy } from 'lodash';
Expand Down Expand Up @@ -122,6 +122,81 @@ export function findHighestVersionJdk(directory: string): string | null {

if (jdkInfos.length === 0) {
debug(`No JDK directories found matching pattern in: ${directory}`);
// If no JDK directories found, check for system set jdk version by using JAVA_HOME environment variable
// Try to find JAVA_HOME using environment variables on Windows, WSL, Ubuntu, or Mac
let javaHome = process.env.JAVA_HOME;

// For WSL, try to detect Linux JAVA_HOME if not found or is a windows path
if ((!javaHome || javaHome.includes('\\')) && isWSL()) {
try {
// Try to run 'bash -c "echo $JAVA_HOME"' to get the Linux side JAVA_HOME
const wslJavaHome = require('child_process').execSync('bash -c "echo $JAVA_HOME"', { encoding: 'utf8' }).trim();
if (wslJavaHome) {
debug(`Using WSL system set JDK from Linux environment: ${wslJavaHome}`);
return wslJavaHome;
}
} catch (e) {
debug(`Could not get JAVA_HOME from WSL Linux environment: ${e}`);
}
}

if (javaHome) {
debug(`Using system set JDK: ${javaHome}`);
return javaHome;
}

// Try some common fallback locations for Ubuntu / Mac
const platform = process.platform;
let commonJavaDirs: string[] = [];
debug(`Detecting platform-specific common Java directories for platform: ${platform}`);

if (platform === 'darwin') { // macOS
debug('Platform is macOS. Checking default Java and SDKMAN directories.');
commonJavaDirs = [
'/Library/Java/JavaVirtualMachines',
process.env.HOME ? `${process.env.HOME}/.sdkman/candidates/java/current` : ''
];
debug(`Common Java directories for macOS: ${JSON.stringify(commonJavaDirs)}`);
} else if (platform === 'linux' || isWSL()) { // Linux, also WSL
debug('Platform is Linux or WSL. Checking standard Java and SDKMAN directories.');
commonJavaDirs = [
'/usr/lib/jvm',
'/usr/java',
process.env.HOME ? `${process.env.HOME}/.sdkman/candidates/java/current` : ''
];
debug(`Common Java directories for Linux/WSL: ${JSON.stringify(commonJavaDirs)}`);
} else if (platform === 'win32') { // Windows
debug('Platform is Windows. Checking ProgramFiles Java directories.');
if (process.env['ProgramFiles']) {
debug(`Adding Java directory from ProgramFiles: ${process.env['ProgramFiles']}\\Java`);
commonJavaDirs.push(`${process.env['ProgramFiles']}\\Java`);
}
if (process.env['ProgramFiles(x86)']) {
debug(`Adding Java directory from ProgramFiles(x86): ${process.env['ProgramFiles(x86)']}\\Java`);
commonJavaDirs.push(`${process.env['ProgramFiles(x86)']}\\Java`);
}
debug(`Common Java directories for Windows: ${JSON.stringify(commonJavaDirs)}`);
} else {
debug(`Unknown or unsupported platform for Java directory detection: ${platform}`);
}

for (const dir of commonJavaDirs) {
if (dir && fs.existsSync(dir)) {
// Check for JDK subdirectories
const subDirs = fs.readdirSync(dir);
for (const sub of subDirs) {
// JDK dir must contain bin/java[.exe]
const javaBin = platform === 'win32'
? path.join(dir, sub, 'bin', 'java.exe')
: path.join(dir, sub, 'bin', 'java');
if (fs.existsSync(javaBin)) {
debug(`Found JDK in fallback directory: ${path.join(dir, sub)}`);
return path.join(dir, sub);
}
}
}
}
debug(`No system set JDK found, returning null`);
return null;
}

Expand Down Expand Up @@ -243,7 +318,7 @@ function getServerOptionsUsingJava(extension: BallerinaExtension): ServerOptions
"bal-shell-service*",
"org.eclipse.lsp4j*",
"diagram-util*",
"openapi-ls-extension*",
"openapi-ls-extension*",
"sqlite-jdbc*"
];

Expand Down Expand Up @@ -316,11 +391,6 @@ function getServerOptionsUsingJava(extension: BallerinaExtension): ServerOptions
throw new Error(`JDK not found in ${dependenciesDir}`);
}

const jdkVersionMatch = jdkDir.match(/jdk-(.+)-jre/);
if (jdkVersionMatch) {
log(`JDK Version: ${jdkVersionMatch[1]}`);
}
debug(`JDK Version: ${jdkVersionMatch[1]}`);
const javaExecutable = isWindows() ? 'java.exe' : 'java';
const cmd = join(jdkDir, 'bin', javaExecutable);
const args = ['-cp', classpath, `-Dballerina.home=${ballerinaHome}`, 'org.ballerinalang.langserver.launchers.stdio.Main'];
Expand Down
Loading