| Repository | Author | License | Version | Date |
|---|---|---|---|---|
| https://github.com/JoZapf/Java-Directory-Tree-2-html | Jo Zapf | MIT | 1.4 | 2025-02-04 |
Produce an executable JAR (e.g., Java-Directory-Tree.jar) for the app that generates an interactive HTML file directory-tree.html from a chosen directory.
The JAR should run by double-click (GUI folder picker) and via CLI with an optional directory argument (which skips the GUI).
- JDK 21 installed (JDK 17 works if you compile with
--release 17for broader runtime compatibility). java,javac, andjaravailable on your PATH (or use full paths, e.g.,"%ProgramFiles%\Java\jdk-21\bin\javac.exe").- Source files in the
srcdirectory (default package):RootDirectoryListing.java(main class withpublic static void main)HtmlSnippets.java(returns CSS & JS for the HTML output)FileTypeIcons.java(returns emoji icons per extension)
Why JDK 21? We target a current LTS. If you want the JAR to run on Java 17+ runtimes, compile with
--release 17(see below).
- No explicit package declarations (default package).
- Main class:
RootDirectoryListing. The manifest must reference this exact name:Main-Class: RootDirectoryListingIf you add a package, change the manifest to the fully qualified name, e.g.,
Main-Class: com.example.RootDirectoryListing.
You can build the JAR either from IntelliJ IDEA (Artifacts) or from the command line. Both produce identical runtime behavior.
- Open/Import the project. Mark
srcas Sources Root (Right-click → Mark Directory as → Sources Root). - Project SDK / Language Level
- Project SDK: JDK 21 (or JDK 17 if you explicitly target
--release 17) - Language level: 21
- Project SDK: JDK 21 (or JDK 17 if you explicitly target
- Create Artifact
- File → Project Structure → Artifacts
- Click
+→ JAR → From modules with dependencies… - Choose your module → set Main Class to
RootDirectoryListing. - Choose an output directory (e.g.,
out/artifacts/JavaDirectoryTree_jar/).
- Build
- Build → Build Artifacts… → JavaDirectoryTree:jar → Build
- Result: a JAR in your chosen output directory. You can rename it to
Java-Directory-Tree.jar.
Notes:
- This project has no external libraries, so “with dependencies” simply bundles your classes.
- If “Main class not found” appears, set the correct Main Class in the artifact settings and rebuild.
The following commands demonstrate exactly how we produced the JAR.
cd /d C:\path\to\src
rmdir /s /q out 2>nul & mkdir out
:: Compile all sources to 'out' using UTF-8
:: If you want the JAR to run on Java 21+ only:
javac -encoding UTF-8 -d out RootDirectoryListing.java HtmlSnippets.java FileTypeIcons.java
:: If you prefer Java 17+ runtime compatibility instead (recommended for portability):
:: javac --release 17 -encoding UTF-8 -d out RootDirectoryListing.java HtmlSnippets.java FileTypeIcons.java
:: Create a manifest file with the Main-Class entry.
> manifest.mf echo Main-Class: RootDirectoryListing
>> manifest.mf echo.
:: Package classes from 'out' into the JAR, applying the manifest.
jar cfm Java-Directory-Tree.jar manifest.mf -C out .
:: Verify contents (optional)
jar tf Java-Directory-Tree.jarPowerShell handles redirection differently; use
Out-Filefor the manifest or run the cmd.exe block above.
Set-Location "C:\path\to\src"
Remove-Item -Recurse -Force out -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path out | Out-Null
# Java 21+ runtime target:
javac -encoding UTF-8 -d out RootDirectoryListing.java HtmlSnippets.java FileTypeIcons.java
# For Java 17+ runtime compatibility:
# javac --release 17 -encoding UTF-8 -d out RootDirectoryListing.java HtmlSnippets.java FileTypeIcons.java
# Create manifest with a trailing blank line
"Main-Class: RootDirectoryListing`n" | Out-File -Encoding ascii manifest.mf
jar cfm Java-Directory-Tree.jar manifest.mf -C out .
# Verify
jar tf Java-Directory-Tree.jarcd /path/to/src
rm -rf out && mkdir -p out
# Java 21+ runtime target:
javac -encoding UTF-8 -d out RootDirectoryListing.java HtmlSnippets.java FileTypeIcons.java
# For Java 17+ runtime compatibility:
# javac --release 17 -encoding UTF-8 -d out RootDirectoryListing.java HtmlSnippets.java FileTypeIcons.java
# Create manifest (note the required trailing blank line)
printf "Main-Class: RootDirectoryListing\n\n" > manifest.mf
jar cfm Java-Directory-Tree.jar manifest.mf -C out .
# Verify
jar tf Java-Directory-Tree.jar-
javac -encoding UTF-8 -d out …
Compiles.javasources using UTF-8 (avoids platform-dependent encoding issues) and writes.classfiles into theoutdirectory (clean separation of sources and artifacts). -
javac --release 17 …
Produces Java 17-compatible bytecode and links against Java 17 APIs, ensuring the JAR runs on Java 17+. Without this flag and compiling on JDK 21, the output typically requires Java 21+ to run. -
manifest.mfwith a blank line at the end
The JAR manifest requires a trailing newline to be valid. Missing it can cause “no main manifest attribute” errors. -
jar cfm Java-Directory-Tree.jar manifest.mf -C out .
Creates (c) a JAR, uses the provided manifest (m), and reads files from theoutdirectory (-C out .setsoutas the working directory for adding files). -
jar tf …
Lists the JAR’s contents for a quick sanity check.
- Double-click the JAR: a folder picker appears; on completion, a dialog shows the path to the generated
directory-tree.htmlin the chosen directory. - Command line (example):
- Windows (cmd.exe):
java -Dfile.encoding=UTF-8 -jar Java-Directory-Tree.jar "C:\Users\YourName\Downloads" - macOS/Linux:
java -Dfile.encoding=UTF-8 -jar Java-Directory-Tree.jar "/Users/yourname/Downloads"
- Windows (cmd.exe):
PowerShell parsing tip: either quote each
-Dargument ('-Dfile.encoding=UTF-8') or use--%stop-parsing mode.
-
no main manifest attribute, in …
The manifest is missing or incorrect. Ensuremanifest.mfcontainsMain-Class: RootDirectoryListingand ends with a blank line. Rebuild the JAR. -
Error: Could not find or load main class RootDirectoryListing
The manifest’s Main-Class is wrong or the class was compiled into a package. If you add a package, use the fully qualified name in the manifest. -
Unsupported class file major version …
You compiled with a newer JDK than the runtime supports (e.g., built on 21, running on 17). Recompile with--release 17or use a newer runtime. -
Emoji not showing / odd glyphs in HTML
This depends on the OS/browser font. The generated HTML is UTF-8; rendering relies on available emoji fonts. -
Access denied errors during traversal
Expected on system folders or protected paths. Run with appropriate permissions or test on user folders first.
Windows (cmd.exe) — build_jar.cmd
@echo off
setlocal
set "SRC=%~dp0src"
set "OUT=%~dp0out"
set "JAR=%~dp0Java-Directory-Tree.jar"
rmdir /s /q "%OUT%" 2>nul
mkdir "%OUT%"
rem Compile (choose one)
rem javac --release 17 -encoding UTF-8 -d "%OUT%" "%SRC%\RootDirectoryListing.java" "%SRC%\HtmlSnippets.java" "%SRC%\FileTypeIcons.java"
javac -encoding UTF-8 -d "%OUT%" "%SRC%\RootDirectoryListing.java" "%SRC%\HtmlSnippets.java" "%SRC%\FileTypeIcons.java"
> "%~dp0manifest.mf" echo Main-Class: RootDirectoryListing
>> "%~dp0manifest.mf" echo.
jar cfm "%JAR%" "%~dp0manifest.mf" -C "%OUT%" .
jar tf "%JAR%"
echo Built: %JAR%
endlocalmacOS/Linux — build_jar.sh
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SRC="$SCRIPT_DIR/src"
OUT="$SCRIPT_DIR/out"
JAR="$SCRIPT_DIR/Java-Directory-Tree.jar"
rm -rf "$OUT" && mkdir -p "$OUT"
# Compile (choose one)
# javac --release 17 -encoding UTF-8 -d "$OUT" "$SRC/RootDirectoryListing.java" "$SRC/HtmlSnippets.java" "$SRC/FileTypeIcons.java"
javac -encoding UTF-8 -d "$OUT" "$SRC/RootDirectoryListing.java" "$SRC/HtmlSnippets.java" "$SRC/FileTypeIcons.java"
printf "Main-Class: RootDirectoryListing\n\n" > "$SCRIPT_DIR/manifest.mf"
jar cfm "$JAR" "$SCRIPT_DIR/manifest.mf" -C "$OUT" .
jar tf "$JAR"
echo "Built: $JAR"- License: MIT — see LICENSE.
- Author: Jo Zapf
- Repository: https://github.com/JoZapf/Java-Directory-Tree-2-html
Build doc version: 1.4 (2025-02-04)