I want to get the IP address when connecting to Wi-Fi in Java

First thing I tried

Try using InetAddress.getLocalHost ()

For the time being, when I googled, this page came out.  http://www.yukun.info/blog/2008/10/java-inetaddress-getlocalhost.html

It is the result of copying the circle and running it as it is. An IP address that I didn't recognize was output.  2017-04-12_23h11_41.png

If you tilt your head, launch a command prompt and ipconfig. 2017-04-12_23h12_47.png

"Who are you!"

I don't know what happens in other environments, but at least on this machine I found that using InetAddress.getLocalHost () doesn't give me a Wi-Fi IP. Even if you keep googled, only Android articles will hit, so it seems that you have to think of another method even if you push it.

Try using the Windows OS command ipconfig in Java

Since you can see it at the command prompt, I thought that I should be able to mess with this on the Java side, so I decided to try it. I googled that Java should also have System () in C language.


Based on this, if you get the output by the OS command ipconfig and display it ... 2017-04-13_00h29_36.png

Yes it was. The character code used in the Japanese version of Windows command prompt is Shift-JIS. I tried to make the output and the character string after acquisition into UTF-8, but it didn't come true whether I was a small fish or a specification.

@KIchiro gave us some advice in the comments section. It seems that the acquired execution result can be converted into a character string with any character code. Please skip the articles below from here and see the comments section. What were the four days I spent ... </ font>

I'll put one cushion

As I investigated, it turned out that "text files written in Shift-JIS can be read". You can use the text file generated by executing "ipconfig> ip.txt". As a flow

ipconfig > ip.txt    ↓ Specify the character code as Shift-JIS and read ip.txt    ↓ Search using indexOf () or split ()    ↓ Delete ip.txt with a face that doesn't eat anything

What a place, such as.


It should be noted here that the main body side that hits the OS command and the write side that returns the result when hit are parallel processing. If you start reading without waiting for the process on the writing side to finish, the IOException teacher will naturally come.



public class Main {
    public static void main(String[] args) {
        System.out.println(new getWiFiIP().get());


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class getWiFiIP {

    private String ip="";

    public String get(){
        return ip;

    public getWiFiIP() {

        try {

            ProcessBuilder pb=new ProcessBuilder("cmd.exe", "/c", "ipconfig",">","ip.txt");

            Process process = pb.start();   //Start the process
            process.waitFor();              //Wait until the export is finished

            ArrayList<String> list=new Text_read("ip.txt").getText(); //Read text file

            int cnt=list.indexOf("Wireless LAN adapter Wi-Fi:");////////////////////////////////////////////////
                }                                 //Wi from the character string group read and acquired-IP address of Fi connection(IPv4)To search for

            String[] temp=list.get(cnt).split(":"); //"IPv4 address. . . . . . . . . . . .: ×××.×××.×××.Since it is "XXX", divide it
            ip= temp[1];                            //Substitute the acquired IP address

            new File("ip.txt").delete();            //IP.Delete txt

       ////////////////////////////////////////////////////The following error handling
        } catch (IOException e) {
        } catch (InterruptedException e) {

        if(ip.equals(""))ip= "Failed to get";


import java.io.*;
import java.util.ArrayList;

//Read the entire text file and store it in ArrayList.//

public class Text_read {

    private ArrayList<String>text=new ArrayList<String>();      //ArrayList for storage

    public Text_read(String path/*File Path*/){

        try {
            BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(path), "Shift_JIS"));
        }catch(IOException e){
            System.out.println("I / O error");

    public ArrayList<String> getText(){
        return text;                                            

This solved it for the time being, but if anyone knows an easier way, please let me know ...

Thank you @KIchiro. </ font>

Recommended Posts