← Back to list

How to Unzip and Open a Java Project in IntelliJ IDEA from the Command Line

Overview

Temesgen Dessalegn · 2026-04-04 19:39 · 0 claps · 3.3 min read
#java21 #intellij-idea #spring-boot #command-line #unzip
Open on Medium ↗
Wiki topics: 🥊 · Combat Sports

How to Unzip and Open a Java Project in IntelliJ IDEA from the Command Line

Overview

UnzipAndOpen is a lightweight, single-file Java utility that automates a common developer workflow: extracting a zipped Java project and immediately opening it in IntelliJ IDEA. It is designed to be invoked from the command line with a single argument — the path to a .zip file — and handles the rest.

uzo ./service.zip

That’s it. The archive is extracted, the build system is detected, and IntelliJ IDEA opens the project — all in one step.

Source Code

import java.io.File;
import java.util.Set;

void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Usage: java UnzipAndOpen <zip-file-path>");
        return;
    }

    var zipFilePath = new File(args[0]);
    var zipFileAbsolutePath = zipFilePath.getAbsolutePath();
    var folder = new File(zipFileAbsolutePath.substring(0, zipFileAbsolutePath.lastIndexOf('.')));

    new ProcessBuilder().command("unzip", "-a", zipFileAbsolutePath).inheritIO().start().waitFor();

    for (var bFile: Set.of("build.gradle", "build.gradle.kts", "pom.xml",
                        "settings.gradle", "settings.gradle.kts")) {
        var buildFile = new File(folder, bFile);  // construct full path: <extracted-folder>/<build-file>
        if (buildFile.exists()) {
            // Launch IntelliJ IDEA, passing the build file path so the IDE imports the project
            new ProcessBuilder().command("idea", buildFile.getAbsolutePath()).inheritIO().start().waitFor();
            break; // open only once — first match wins
        }
    }
}

Line-by-Line Walkthrough

Entry Point

void main(String[] args) throws Exception {

This method uses instance main methods, a language feature introduced as a preview in Java 21 and stabilized in later releases. Notice the absence of the static keyword — the JVM automatically instantiates the enclosing class and invokes this method. This is part of Java's ongoing effort to simplify entry points, especially for small utility programs and scripts.

Argument Validation

if (args.length != 1) {
    System.out.println("Usage: java UnzipAndOpen <zip-file-path>");
    return;
}

A basic guard clause. The program expects exactly one argument: the path to a .zip file. If the argument count is wrong, it prints a usage hint and exits gracefully — no stack trace, no cryptic error.

Resolving the Zip File Path

var zipFilePath = new File(args[0]);
var zipFileAbsolutePath = zipFilePath.getAbsolutePath();

The raw argument (e.g., ./service.zip or ../downloads/project.zip) is wrapped in a File object, then resolved to its absolute path. This ensures that all subsequent operations — extraction and IDE launch — work correctly regardless of the current working directory.

Deriving the Output Folder Name

var folder = new File(zipFileAbsolutePath.substring(0, zipFileAbsolutePath.lastIndexOf('.')));

The .zip extension is stripped to determine the expected folder name after extraction. For example:

|    Input Path            |   Derived Folder           |
|--------------------------|----------------------------|
| `/home/user/service.zip` | `/home/user/service`       |
| `./my-app.zip`           | `/absolute/path/to/my-app` |

This assumes the archive contains a top-level directory whose name matches the zip file (a convention followed by most project generators such as Spring Initializer).

Extracting the Archive

new ProcessBuilder().command("unzip", "-a", zipFileAbsolutePath)
        .inheritIO().start().waitFor();

This line delegates extraction to the operating system’s native unzip command.

Detecting and Opening the Build File

for (var k : Set.of("build.gradle", "build.gradle.kts", "pom.xml",
                    "settings.gradle", "settings.gradle.kts")) {
    var buildFile = new File(folder, k);
    if (buildFile.exists()) {
        new ProcessBuilder().command("idea", buildFile.getAbsolutePath())
                .inheritIO().start().waitFor();
        break;
    }
}

The utility supports the most common JVM build systems, covering projects written in Java, Kotlin, Groovy, Scala, and other JVM languages: build.gradle — Java Gradle project build.gradle.kts — Kotlin Gradle project pom.xml — Java Maven project settings.gradle — Multi-module Gradle projects settings.gradle.kts — Same as above, but using Kotlin Script

"idea", buildFile.getAbsolutePath() — Invokes IntelliJ IDEA's command-line launcher, passing the build file's absolute path so the IDE opens and imports the project with full build-tool integration.

Installation

macOS / Linux

The simplest way to make this utility available system-wide is to define a shell function. Add the following to your shell configuration file (~/.zshrc on macOS, ~/.bashrc on Linux):

uzo() {
  java --source 25 /path/to/UnzipAndOpen.java "$1"
}

Replace /path/to/UnzipAndOpen.java with the actual absolute path to the source file.

The 25 here is for Java version. It could also be any Java version 21 or above

Reload your shell:

source ~/.zshrc

You can now run the utility from any directory:

uzo ./service.zip

Alternative: Standalone Executable Script

Create a file at /usr/local/bin/uzo:

#!/usr/bin/env bash
java --source 25 /path/to/UnzipAndOpen.java "$1"

Make it executable:

chmod +x /usr/local/bin/uzo

Windows

Create a file named uzo.bat and place it in a directory on your PATH:

@echo off
java --source 25 C:\path\to\UnzipAndOpen.java %1

Note: You will also need to replace the unzip command in the source code with a Windows-compatible alternative (e.g., tar -xf on Windows 10+) or switch to pure Java zip extraction.

Usage

uzo <zip-file-path>

Example:

# Download a project from Spring Initializr, then:
uzo ~/Downloads/demo.zip

The utility will:

  1. Extract demo.zip into a demo/ folder in the current directory.
  2. Scan for build.gradle, build.gradle.kts, settings.gradle, settings.gradle.kts, or pom.xml inside demo/.
  3. Open the first detected build file in IntelliJ IDEA with full build-tool support.

Summary

  • macOS and Linux — Fully supported out of the box. No additional setup beyond enabling the idea CLI launcher.
  • Windows — Functional with minor adjustments. Replace unzip with a Windows-native alternative and use the correct IntelliJ executable name. A cross-platform improvement would be to replace the unzip shell command with Java's built-in java.util.zip.ZipInputStream to eliminate the OS dependency entirely.

Tip: If you need true cross-platform support without external dependencies, consider replacing the ProcessBuilder("unzip", ...) call with pure Java zip extraction using java.util.zip.ZipInputStream. The idea launcher, however, will always be platform-specific.


메타데이터
post_id
40917b1a48ee
slug
how-to-unzip-and-open-a-java-project-in-intellij-idea-from-the-command-line-40917b1a48ee
url
https://medium.com/@carspeed1900/how-to-unzip-and-open-a-java-project-in-intellij-idea-from-the-command-line-40917b1a48ee
canonical_url
https://medium.com/@carspeed1900/how-to-unzip-and-open-a-java-project-in-intellij-idea-from-the-command-line-40917b1a48ee
author_url
https://medium.com/@carspeed1900
status
ok
fetched_at
2026-07-11 12:56:19