> ## Documentation Index
> Fetch the complete documentation index at: https://blog.mapptech.com.co/llms.txt
> Use this file to discover all available pages before exploring further.

# WSL + VS Code + Tomcat 9 Setup Guide

> This guide walks you through the one-time global setup on your machine, then the per-project steps to get **PREDIUM** up and running inside WSL with VS Code and Tomcat 9.0.85.

## Part A: One-Time Global WSL Configuration

### 1. Enable & initialize WSL 2

```powershell theme={null}
# In PowerShell (Admin):
wsl --install
wsl --set-default-version 2
```

Reboot if prompted. Launch your Linux distro (e.g. Ubuntu) and complete its first-run setup.

### 2. Hide Windows ADS files globally

```ini theme={null}
# In WSL, edit /etc/wsl.conf:
[automount]
options = "metadata,streams_interface=none"
```

```powershell theme={null}
# Back in PowerShell (Admin):
wsl --shutdown
```

Restart your distro—Windows alternate-data-streams (`*:Zone.Identifier`) will no longer appear.

### 3. Configure your shell environment

```bash theme={null}
# Append to ~/.bashrc (or ~/.profile):
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export CATALINA_HOME=/opt/tomcat9

# Reload:
source ~/.bashrc
```

***

## Part B: Install Tooling & Tomcat in WSL

### 4. Install core tools

```bash theme={null}
sudo apt update
sudo apt install -y openjdk-8-jdk ant unzip curl
```

### 5. Install Tomcat 9.0.85

```bash theme={null}
# Download & extract
cd /tmp
wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.85/bin/apache-tomcat-9.0.85.tar.gz
sudo mkdir -p /opt/tomcat9
sudo tar xzf apache-tomcat-9.0.85.tar.gz \
  -C /opt/tomcat9 --strip-components=1

# Create tomcat user & set permissions
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat9 tomcat
sudo chown -R tomcat:tomcat /opt/tomcat9
sudo chmod -R o+rX /opt/tomcat9
sudo chmod +x /opt/tomcat9/bin/*.sh
```

### 6. Verify Tomcat

```bash theme={null}
sudo -u tomcat $CATALINA_HOME/bin/startup.sh
```

Open your Windows browser to **[http://localhost:8080](http://localhost:8080)** —you should see the Tomcat welcome page.

***

## Part C: VS Code & Project Workflow

### 7. Install & configure VS Code Remote-WSL

1. Install **Visual Studio Code** on Windows.
2. In VS Code → Extensions (Ctrl+Shift+X) → install **Remote – WSL**.
3. Click the green `><` icon (bottom-left) → **Remote-WSL: New Window**.
4. In that WSL-connected window, install:
   * **Extension Pack for Java** (`vscjava.vscode-java-pack`)
   * **Tomcat for Java** (`adashen.vscode-tomcat`)

### 8. Copy the PREDIUM project into WSL

* From Windows Explorer: copy your `PREDIUM` folder into\
  `\\wsl$\Ubuntu\home\santiago\projects\PREDIUM`
* Or in WSL:
  ```bash theme={null}
  mkdir -p ~/projects
  cp -r /mnt/c/Path/To/PREDIUM ~/projects/
  ```

### 9. Open the project in VS Code

```bash theme={null}
cd ~/projects/PREDIUM
code .
```

:::note
On first run, you’ll see **“Installing VS Code Server for x64…”** in your WSL shell. **Please wait** for it to complete before interacting with the editor—otherwise you may end up editing files on Windows by mistake.
:::

### 10. Register Tomcat in VS Code

* In the WSL-connected VS Code window → **Tomcat Servers** view (elephant icon or Cmd/Ctrl+Shift+P → `Tomcat: Show Servers View`).
* Click **➕ Add Tomcat Server** → select `/opt/tomcat9`.
* You should now see **Local Tomcat 9.0.85** listed.

### 11. Build & package the WAR

```bash theme={null}
cd ~/projects/PREDIUM
ant clean dist
```

On success: `dist/PREDIUM.war` is generated.

### 12. Deploy & run in VS Code

* In VS Code’s Explorer, right-click `dist/PREDIUM.war` → **Run on Tomcat Server** → **Local Tomcat 9.0.85**.
* VS Code will deploy and start your application.

### 13. Verify & debug

* Browse to **[http://localhost:8080/PREDIUM/](http://localhost:8080/PREDIUM/)** to see your live app.
* To debug: in **Tomcat Servers** view → right-click the server → **Debug** → select the same WAR; set breakpoints in VS Code.

Here’s that same “Connect to PostgreSQL on Windows from WSL” doc—but boiled down to **copy-and-paste** commands only. No manual file-edits, no chasing IPs.

***

### 14. Connect PostgreSQL on Windows from WSL (v16, copy & paste)

1. **Configure PostgreSQL on Windows & open the firewall**
   ```powershell theme={null}
   $winIp = (Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null } | Select-Object -Expand IPv4Address).IPAddress
   $pgRoot = 'C:\Program Files\PostgreSQL\16'
   & "$pgRoot\bin\psql.exe" -U postgres -d postgres -c "ALTER SYSTEM SET listen_addresses='*';"
   $hba = "$pgRoot\data\pg_hba.conf"
   Add-Content $hba ("host all all " + $winIp + "/32 md5")
   Restart-Service postgresql-x64-16
   New-NetFirewallRule -DisplayName 'Postgres (WSL)' -Direction Inbound -Protocol TCP -LocalPort 5432 -Action Allow -Profile Private
   ```

2. **Fetch the Windows-host IP inside WSL**
   ```bash theme={null}
   export DB_HOST=$(
     powershell.exe -NoProfile -Command \
       "(Get-NetIPConfiguration | Where-Object { \$_.IPv4DefaultGateway -ne \$null }).IPv4Address.IPAddress"
   )
   DB_HOST=${DB_HOST//$'\r'/}
   echo "DB_HOST → $DB_HOST"
   ```

3. **Install the client & verify the connection**
   ```bash theme={null}
   sudo apt update
   sudo apt install -y postgresql-client
   psql -h "$DB_HOST" -U postgres -d predium_vup
   ```

4. **Configure your Spring/Hibernate application**\
   Replace `<YOUR_WINDOWS_IP>` with the value you obtained in step 2:
   ```xml theme={null}
   <property name="url" value="jdbc:postgresql://<YOUR_WINDOWS_IP>:5432/predium_vup"/>
   ```

***

##### Extra Tips

* **Change HTTP port**: edit `/opt/tomcat9/conf/server.xml`.
* **View logs**: VS Code → Output panel → select **Tomcat Server**.
* **Restart**: right-click the server → **Restart**.
