Back to Posts

Build a Java WiFi Scanner Using ProcessBuilder

Build a Java WiFi Scanner Using ProcessBuilder

Home > Java > Desktop Applications > Build a Java WiFi Scanner Using ProcessBuilder

Build a Java WiFi Scanner Using ProcessBuilder

In this project we'll create a simple command line application that scans for nearby WiFi networks on Windows. It's small enough to complete in one sitting and introduces an important Java class called ProcessBuilder.

What is ProcessBuilder?

Java cannot directly ask Windows for nearby WiFi networks, but Windows already has a command that can.

Open Command Prompt and type:

netsh wlan show networks mode=bssid

You'll see a list of nearby wireless networks.

Our Java program simply executes this command and displays the results.

Create the Project

Create a new Java project containing a single class called Main.


src
└── Main.java

The Complete Program

Copy the following code into Main.java.


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) {

        try {

            ProcessBuilder builder = new ProcessBuilder(
                    "netsh",
                    "wlan",
                    "show",
                    "networks",
                    "mode=bssid");

            Process process = builder.start();

            BufferedReader reader =
                    new BufferedReader(
                            new InputStreamReader(process.getInputStream()));

            String line;

            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            process.waitFor();

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

Running the Program

Compile the program.


javac Main.java

Now run it.


java Main

If your computer's WiFi adapter is enabled, you should see output similar to this:


SSID 1 : HomeWiFi
    Signal             : 96%
    Authentication     : WPA2-Personal

SSID 2 : CoffeeShop
    Signal             : 72%
    Authentication     : Open

SSID 3 : Office
    Signal             : 84%
    Authentication     : WPA3-Personal

How the Program Works

The application is surprisingly small.

  • ProcessBuilder starts a Windows command.
  • builder.start() launches the process.
  • BufferedReader reads the command's output.
  • The while loop prints each line to the console.
  • waitFor() waits until Windows finishes executing the command.

That's all there is to it.

Where Do You Go From Here?

This program is intentionally simple, but there are several ways you can improve it.

  • Only display the SSID and signal strength.
  • Store the results in a List.
  • Create a WifiNetwork class.
  • Sort the networks by signal strength.
  • Export the results to a CSV file.
  • Display the results in a Swing JTable.

Each of these improvements introduces another Java concept without making the project overwhelming.

What You Have Learned

Many beginners think Object-Oriented Programming begins with inheritance, but real desktop applications usually begin by solving a problem.

In this project you used Java to communicate with the operating system, launch an external process and capture its output.

It's a small project, but it demonstrates a technique that can be used to execute many Windows commands, making ProcessBuilder one of the most useful classes in the Java Standard Library.

Back to Posts