diff --git a/source/_posts/2026-03-06.md b/source/_posts/2026-03-06.md new file mode 100644 index 0000000..6fc6980 --- /dev/null +++ b/source/_posts/2026-03-06.md @@ -0,0 +1,22 @@ +--- +title: Opening a web browser from Java apps in WSL +date: 2026-03-06 +--- + +Linux CLI tools typically rely on xdg-open or fallback to $BROWSER environment variable to open the default browser, usually for OAuth authentication to authorise the CLI tool without using a permanent credential. Java apps behave differently by trying common browser executables in $PATH if the default browser could not be located. To launch a web browser from a Java app in WSL, simply add a symlink in "/usr/bin/" that points to the browser executable in Windows, this way you don't have to install a [web browser](https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps#install-google-chrome-for-linux) in WSL. + +``` +# Examples, just choose one. +sudo ln -s "/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe" "/usr/bin/msedge" +sudo ln -s "/mnt/c/Program Files/Google/Chrome/Application/chrome.exe" "/usr/bin/chrome" +sudo ln -s "/mnt/c/Program Files/Mozilla Firefox/firefox.exe" /usr/bin/firefox" +``` + +Programs that rely on OAuth authentication typically listens on a random port to receive credential during the last step of authorisation. Since WSL enables IPv6 by default, the program may listen on IPv6 only. I noticed this behaviour when I check for listening ports `netstat -aon | findstr ":port-number"` on Windows and the output was `[::1]:port-number` without any `127.0.0.1`. This can cause the program to unable to receive credential and fail authorisation if the Windows host has IPv6 disabled. A workaround for this issue is to also disable IPv6 on WSL to force the program to listen on IPv4. Append this line to "$home\\.wslconfig" in Windows and restart WSL `wsl --shutdown`, + +``` +[wsl2] +kernelCommandLine=ipv6.disable=1 +``` + +[Credit](https://stackoverflow.com/questions/64159822/how-to-solve-java-lang-exception-no-web-browser-found-on-raspberry-pi#comment113470482_64160341)