# Solved: Java: List all computers in the LAN



## rockballad (May 4, 2007)

A old question, but I couldn't get the real answer:

How can I list all computers in the LAN? It looks like we're using Windows Explorer to access \\Entire Network and then \\Microsoft Windows Network\...

There're some ways I've known but got no success:

- Use command line "net view"
- http://jcifs.samba.org/

If you have any experience with this task, please share with me. Thanks in advance!


----------



## Chicon (Jul 29, 2004)

Hi rockballad,

I'm having a look at the *jcifs* API you're referring to.
BTW, a solution is proposed on this page .

*Edit* :

As the proposed solution is not multi-platform, your application may need to know the OS on which it's running :


```
[SIZE=2]
Properties p = System.getProperties();
String os = p.getProperty("os.name");
System.out.println(os);
[/SIZE]
```


----------



## rockballad (May 4, 2007)

Thank you, Chicon!

The former solution, "net view", I have found out the progress: 
- list = get result of "net view /domain"
- for each of list : "net view /domain:list_item"

But as you said, it's for Windows, not all platforms. In Linux, e.g. I will need to add other code. BTW, do you know about it?

The latter, I don't know the progress yet. If you find out, please help me.

Thanks again!


----------



## Chicon (Jul 29, 2004)

Unfortunately, my knowledges in bash scripting are evaporated.

But I would proceed this way in Java :

- detect the OS on which the application is running and the user home directory :


```
Properties p = System.getProperties();
String os = p.getProperty("os.name");
String home = p.getProperty("user.home");
```
- build dynamically a script file using the *FileWriter* utilities and store it in the home directory, for example : */any_user/home/script*

- run the script :


```
Runtime run = run.getRuntime();
run.exec("./any_user/home/script");
```


----------



## rockballad (May 4, 2007)

Thank you, Chicon, but I found out the way to do it easily using JCIFS:


```
SmbFile[] domains;
        try {
            domains = (new SmbFile("smb://")).listFiles();
            for (int i = 0; i < domains.length; i++) {
                System.out.println(domains[i]);
                SmbFile[] servers = domains[i].listFiles();
                for (int j = 0; j < servers.length ; j++) {
                    System.out.println("\t"+servers[j]);
                }
            }
        } catch (SmbException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
```
Download jCIFS lib here : http://jcifs.samba.org/

Thanks any way!

Have a nice day!


----------

