B4J Code Snippet Get Host (Computer) Name - inline JAVA

I came across www.codegrepr.com and found this useful snippet
Hope it will be of benefit
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    Dim jo As JavaObject = Me
    Log(jo.RunMethod("getHostName", Null))

End Sub

#if JAVA
 import java.net.InetAddress;
import java.net.UnknownHostException;

static String hostname = "Unknown";
public static String getHostName() throws Exception {
try
{
    InetAddress addr;
    addr = InetAddress.getLocalHost();
    String hostname = addr.getHostName();
    System.out.println(hostname);
    return hostname;
}
catch (UnknownHostException ex)
{
    System.out.println("Hostname can not be resolved");
    return hostname;
}
}
#end if
 

micro

Well-Known Member
Licensed User
Longtime User
I came across www.codegrepr.com and found this useful snippet
Hope it will be of benefit
B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    Dim jo As JavaObject = Me
    Log(jo.RunMethod("getHostName", Null))

End Sub

#if JAVA
 import java.net.InetAddress;
import java.net.UnknownHostException;

static String hostname = "Unknown";
public static String getHostName() throws Exception {
try
{
    InetAddress addr;
    addr = InetAddress.getLocalHost();
    String hostname = addr.getHostName();
    System.out.println(hostname);
    return hostname;
}
catch (UnknownHostException ex)
{
    System.out.println("Hostname can not be resolved");
    return hostname;
}
}
#end if
Thakns for your snippet

Log(GetEnvironmentVariable("COMPUTERNAME", "??"))

Hi to all
 
Last edited:

jkhazraji

Active Member
Licensed User
Longtime User
Thakns for your snippet

Log(GetEnvironmentVariable("COMPUTERNAME", "??"))

Hi to all
This is the 'computer' name.. Thanks
 
Top