Home > Java > Desktop Applications > 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.
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 a new Java project containing a single class called Main.
src └── Main.java
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(); } } }
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
The application is surprisingly small.
That's all there is to it.
This program is intentionally simple, but there are several ways you can improve it.
Each of these improvements introduces another Java concept without making the project overwhelming.
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.