Skip to content

Instantly share code, notes, and snippets.

@rsenden
Last active August 4, 2024 01:53
Show Gist options
  • Save rsenden/7bc96a3e678b60e61c750517ca8de883 to your computer and use it in GitHub Desktop.
Save rsenden/7bc96a3e678b60e61c750517ca8de883 to your computer and use it in GitHub Desktop.
Run Eclipse in WSL2
<?xml version="1.0" encoding="UTF-8"?>
<XLaunch WindowMode="MultiWindow" ClientMode="NoClient" LocalClient="False" Display="0" LocalProgram="xcalc" RemoteProgram="xterm" RemotePassword="" PrivateKey="" RemoteHost="" RemoteUser="" XDMCPHost="" XDMCPBroadcast="False" XDMCPIndirect="False" Clipboard="True" ClipboardPrimary="True" ExtraParams="" Wgl="True" DisableAC="True" XDMCPTerminate="False"/>

This gist provides a summary on how to accomplish the following tasks:

  • Access WSL2 using putty
  • Run Eclipse on WSL2 Ubuntu, displaying the Eclipse UI on VcXsrv running on Windows
    • Connecting to VcXsrv directly from WSL2
    • Using SSH X11 forwarding, with network connection initiated from Windows to WSL2 Ubuntu, which is particularly useful on company laptops that refuse all incoming network connections on Windows.
  1. Prerequisites:

    • WSL2 up and running
    • Putty, Plink & PuttyGen installed on Windows
  2. WSL2 Ubuntu: Install & configure OpenSSH

    • sudo apt install openssh-server

    • Edit /etc/ssh/sshd_config:

        X11Forwarding yes
        X11DisplayOffset 10
        X11UseLocalhost no
      
    • sudo ssh-keygen -A

    • sudo service ssh start

  3. Auto-start SSH daemon:

    • WSL2 Ubuntu: mkdir -p ~/bin ; echo "sudo service ssh start" >> ~/bin/startup.sh
    • Windows: Create scheduled task that runs on user logon, running C:\Windows\System32\bash.exe with arguments /home/<user>/bin/startup.sh
  4. Windows: Configure SSH public key authentication using PuttyGen:

    • See https://www.puttygen.com/#How_to_use_PuTTYgen for general instructions
    • Run PuttyGen
    • Generate new key pair, or convert existing key pair
    • Save private key to ppk file; this will used later when connecting to WSL2 Ubuntu using Putty or PLink
    • Add the SSH public key to ~/.ssh/authorized_keys on WSL2 Ubuntu
  5. Windows: Create putty profile to connect to WSL2 Ubuntu:

    • Session
      • Host Name: localhost
      • Port: 22 (or whatever OpenSSH port you configured on WSL2 Ubuntu)
    • Connection -> Data:
      • Auto-login username: <your user name>
    • Connection -> SSH -> Auth:
      • Private key file for authentication: <key file generated by PuttyGen in step #2>
    • Connection -> SSH -> X11:
      • Enable X11 forwarding (if you want to start X11 programs)
      • X display location: 127.0.0.1:0.0 (this should match whatever DISPLAY value is shown in VcXsrv logs)
  6. Windows: Install & configure VcXsrv:

    • Download & install https://sourceforge.net/projects/vcxsrv/
    • Copy config.xlaunch as shown below to the Startup folder (Win+R: shell:startup)
    • Launch config.xlaunch to start the X Server manually for now
  7. WSL2 Ubuntu: Install Eclipse & prerequisites:

    • sudo apt-get install openjdk-11-jdk
    • sudo apt-get install libgtk-3-0
    • Download and unpack Eclipse for Linux to ~/eclipse
  8. Windows: Eclipse shortcut using SSH X11 tunnel:

    • Manually try starting Eclipse from Windows by running the following command:
      plink.exe -batch -X -i <path to .ppk from step #2> <Ubuntu username>@localhost ~/eclipse/eclipse
    • Copy silent.vbs and plink-silent.cmd (see attached files) to the directory where PLink.exe is installed
    • Create a new shortcut that runs the following command:
      C:\Windows\System32\cmd.exe /c "C:\<path to plink>\plink-silent.cmd -batch -X -i <path to .ppk from step #2> <Ubuntu username>@localhost ~/eclipse/eclipse"
    • Double-click the shortcut; if all is well you should see the Eclipse UI after a couple of seconds
  9. Windows: Eclipse shortcut using direct XServer connection:

    • Add the following lines to ~/.profile on WSL2 Ubuntu (create file if it doesn't yet exist):
      export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
      export LIBGL_ALWAYS_INDIRECT=1
      
      Note that having these lines in .bashrc won't work as .bashrc is only loaded for interactive shells
    • Create a new shortcut that runs the following command:
      C:\Windows\System32\wsl.exe -u <WSL2 user> -- bash -lc 'nohup ~/eclipse/eclipse' ^& sleep 1
CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False
@smaring
Copy link

smaring commented Jun 4, 2022

Many thanks for this. You've made me a happy man and changed how I will spend the majority of my day for the foreseeable future!

About 18 months ago I decided to switch from Eclipse, which I had been using successfully for what seems like 20 years, over to VSC.
Today, I made an honest assessment and finally admitted to myself that I hate it. It was also causing vmmem to grab all of my available RAM (~25GB) and making my machine unusable. I was happier with Eclipse. Since I'd become a fan of WSL2 Ubuntu I decided to search for how I could run Eclipse using WSL2 Ubuntu as my workspace. That led me here. I was getting a little frustrated because I didn't think the "Multiple windows" thing in XLaunch was working. So, I was starting with "One large window". I spent a bit of time trying to figure out how I could maximize the size of Eclipse in the X11 server window. Imagine my surprise when I started Eclipse without having launched "One large window" and the app came up looking like a regular Windows desktop app! Hahahaha. Yeah, I figured out that "Multiple windows" starts a background process and sits in the system tray.

A couple of thoughts here and perhaps to help others ...

"Copy config.xlaunch as shown below" I thought you forgot to add it because I wasn't seeing it "below". If I had noticed it was at the top or that WindowMode="MultiWindow" opened a process in the system tray I could have saved myself the last couple hours of fumbling around. LOL. Oh well.

I ended up saving the config.xlaunch to the XcXsrv install directory and modifying the XLaunch desktop shortcut to have a Target of "C:\Program Files\VcXsrv\xlaunch.exe" -run config.xlaunch

Step #7:

Eclipse will run much better with these ...

sudo apt-get install libwebkit2gtk-4.0-37
sudo apt-get install libwebkit2gtk-4.0-37-gtk2

Assuming a typical Putty install, use C:\PROGRA~1\PuTTY\plink-silent.cmd as a path instead of C:\Program Files\PuTTY\plink-silent.cmd

Step #8:

When running a Groovy script in bash, following this config, I started getting:

java.awt.AWTError: Can't connect to X11 window server using 'NN.NN.NN.NN:0' as the value of the DISPLAY variable

where "NN.NN.NN.NN:0" is the value of DISPLAY as determined by the awk command

I cured it simply by opening up the permissions in the Windows Firewall for the vcxsrv.exe executable.

@Clevinacio
Copy link

Hello! Im trying to use this tutorial, but when I try to connect mannualy it gives me this message:

FATAL ERROR: Cannot answer interactive prompts in batch mode

Then, I've tried to remove the -batch flag and connect. It asks me the ubuntu user password, shows a message of Access granted and says to press Return, but after that it closes and opens nothing. Can somebody help me? Heres the complete message

C:\Program Files\PuTTY> plink.exe -X -i C:\Users\clevi\OneDrive\Documents\wsl2Key.ppk clevinacio@localhost ~/eclipse/eclipse
Sclevinacio@localhost's password:
erver refused our key
Aclevinacio@localhost's password:
ccess denied
Access granted. Press Return to begin session.
b
PS C:\Program Files\PuTTY>

@gkmurphy
Copy link

gkmurphy commented Sep 9, 2023

@Clevinacio - adding a solution if anyone else runs into this (I did too). It seems to be a problem with the keys recognizing each other. I'm creating this for a personal project and I trust everything I'm connecting to so I just removed the "-batch" parameter so it would prompt me to OK the connection. After trusting it, that error no longer pops up and you should have access.

@rsenden
Copy link
Author

rsenden commented Sep 10, 2023

I'm no longer using this approach myself, but rather using native WSL2 / WSLg features. In short:

  • Install Ubuntu 22.04
  • Open an Ubuntu session
  • Download and run the Eclipse installer. Don't remember whether it's relevant for running the installer, but:
    • In my .bashrc I run dbus-update-activation-environment --all > /dev/null 2>&1
    • I have the following environment variables defined:
      • GDK_BACKEND=x11
      • LIBGL_ALWAYS_SOFTWARE=1
  • Edit ~/.local/share/applications/epp.package.java.desktop (I guess name may be different based on which Eclipse edition you install) to add the same two environment variables as above to the Exec line, i.e., something like:
    • Exec=GDK_BACKEND=x11 LIBGL_ALWAYS_SOFTWARE=1 /home/myuser/eclipse/java-2023-06/eclipse/eclipse
    • These environment variables add standard x11 controls to the Eclipse window, like min/max buttons, and LIBGL_ALWAYS_SOFTWARE=1 seems to provide better stability, and I think one of these also help to have context menu's shown within screen boundaries
  • Move the file above to /usr/share/applications/ to have Eclipse show up in your Windows start menu

Note that when Eclipse is maximized, context menu's may still be partially hidden by the Windows task bar, so I've configured the taskbar to automatically hide in desktop mode.
* Insert the same two environment variables as listed above in the Exec

@isaac-gros
Copy link

Just wanted to share my experience as I thought it would help others.

I managed to install and run Eclipse using WSL without any other software (Putty, Plink & PuttyGen included).

First of all, I followed instructions on this Windows tutorial up to the "Update the packages in your distribution" section. Once you're done with checking prerequisites and updating packages of your distro, you can proceed to install the Eclipse Installer directly from your WSL Bash. Here are the steps to reproduce :

  1. Go to the Eclipse Installer page.

  2. Click on Linux x86_64 in the "Try the Eclipse Installer" section.

  3. Navigate to "Other download options", hover "Direct link to file", right click, select "Copy link".

  4. In your WSL terminal, run wget <copied-link>. You don't need to use the -O flag as we want to keep the original filename.

  5. (Recommended security steps up to step 8) Run the previous command again with some extras changes :

    • Change the download.php in the copied link by sums.php.
    • Add the text &type=md5 at the very end of the copied URL.
    • Add the -O eclipse-installer.md5 after the wget command.
    • Your command should look like this : wget -O eclipse-installer.md5 https://[...]sums.php?file=[...].tar.gz&r=1&type=md5
  6. Make sure you have files <eclipse-installer>.tar.gz and eclipse-installer.md5, then run md5sum -c eclipse-installer.md5.

  7. If the output says somehting like below, go to the next step. If not, try to download the tar.gz file again.
    eclipse-java-2023-12-R-linux-gtk-x86_64.tar.gz: OK

  8. Extract the downloaded archive with tar xvfz <eclipse-installer>.tar.gz.

  9. Run cd eclipse/eclispe-installer/, then ./eclipse-inst. A window should open, inviting you to install the software of your choice. DO NOT CLOSE/REDUCE the window during installation as it may crash if you do so. Keep the installation window open with focus on. When done, you can cd to the chosen installation path and run ./eclipse.

Happy coding !

@asmolcic
Copy link

Thanks @isaac-gros Worked for me, but I had to also do: sudo apt install libswt-gtk-4-java

@rsenden
Copy link
Author

rsenden commented Jan 19, 2024

Thanks all for the various suggestions. I'm using a similar approach as @isaac-gros described, however with the standard WSLg-based approach, I encountered an annoying issue; Eclipse would disappear after sleep/resume or on network change (for example switching between LAN & WIFI). Sometimes the existing Eclipse window would re-appear when starting another instance, but other times the window wouldn't re-appear and I had to kill the existing Eclipse process to be able to start a new instance on the same workspace.

I'm now using https://github.com/nbdd0121/wsld in combination with VcXsrv, which fixes this problem for me (I couldn't figure out how to get wsld to work with the native WSLg X11 server; see nbdd0121/wsld#34)

@codespearhead
Copy link

@asmolcic 's suggestion did the trick for me.

sudo snap install --classic eclipse &&
sudo apt-get update &&
sudo apt-get install libswt-gtk-4-java &&
eclipse

@TheAnton205
Copy link

TheAnton205 commented Jul 1, 2024

There's a another way that I found works for me via WSL, following https://github.com/a-langer/eclipse-oomph-console from the thread https://stackoverflow.com/questions/35282460/install-eclipse-via-terminal:

  1. Follow the steps on https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps up until step "Update the packages in your distribution" via @isaac-gros

  2. Download the tar from console oomph installer :

wget -O installer.tar.gz  https://search.maven.org/remotecontent?filepath=com/github/a-langer/org.eclipse.oomph.console.product/1.0.1/org.eclipse.oomph.console.product-1.0.1-linux.gtk.x86_64.tar.gz
  1. Extract it:
tar -xvzf installer.tar.gz
  1. Go into new directory:
cd eclipse-installer
  1. Run the installer (it might hang at 74%, just wait):
./eclipse-inst
  1. Follow the steps on the installer that pops up:
  • Eclipse IDE for Java Developers
  • Installation folder --> Change to home, not root! It will make things easier imo
  • Click install
  1. Cd into where you set your installation to be
  2. Cd into /ide/eclipse (you may not have an ide folder)
  3. You can now run eclipse from terminal:
./eclipse

@dantran
Copy link

dantran commented Aug 4, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment