B4J Question GetEnvironmentVariable("COMPUTERNAME", "") issue on Mac

ThRuST

Well-Known Member
Licensed User
Longtime User
I'm trying to get the GetEnvironmentVariable("COMPUTERNAME", "") to work in vmWare v15.1.0 on my PC.
I'm running MacOS El capitan. JavaFX seems to work fine with RNE v8 and v9, went back to v8 but this command still returns an empty string.
My virtual Mac's name is Macstation in system properties. The command to find the Computername works on my PC however.
Can anyone of you out there test this on a real mac, or a Mac emulator to confirm if it works or not. Perhaps this is a vmware issue and not in B4J.

B4X:
Dim name As String = GetEnvironmentVariable("COMPUTERNAME", "")
Xui.MsgboxAsync(name, "Result")
 
Last edited:

madru

Active Member
Licensed User
Longtime User
Same on a real Mac ......

try to use the host name....

B4X:
InetAddress.getLocalHost().getHostName()

B4X:
Sub AppStart (Args() As String)
    Log("Hello world!!!")
    Dim NativeMe As JavaObject = Me
    Dim s As String = NativeMe.RunMethod("Test", Null)
    Log(s)
End Sub

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


public static String Test() {
 
String host = System.getenv("COMPUTERNAME");
if (host != null)
    return "COMPUTERNAME "+host;

host = System.getenv("HOSTNAME");
if (host != null)
    return "HOSTNAME "+host;

try {
    String result = InetAddress.getLocalHost().getHostName();
        return "getHostName "+result;
} catch (UnknownHostException e) {

}
// undetermined.
return null;
}
#End If
 
Last edited:
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Erel I tried the command env and this is what it shows. @madru I will try that, thanks

1599641609960.png
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Erel This is what shows when I use
B4X:
Dim name As String = GetEnvironmentVariable("COMPUTERNAME", "")

1599645932720.png
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@madru Here's the code I used, just to display the result

B4X:
Sub Button1_Click
    
    Log("Hello world!!!")
    Dim NativeMe As JavaObject = Me
    Dim s As String = NativeMe.RunMethod("Test", Null)
    Log(s)
    
    Dim sf As Object = xui.Msgbox2Async(s, "Title", "Yes", "Cancel", "No", Null)
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        ExitApplication
        Log("Deleted!!!")
    End If
    
    '
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@madru Yes thanks it works fine now. I put it in a code module to override the default computer name behavior just to make sure it works on vmWare.
Reinstalling my old macbook to try it out on a real device as well. Hat off for your solution, thanks to Master Erel as well 🙂 👍
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I put it in a code module to override the default computer name behavior just to make sure it works on vmWare
Please note, this has nothing to do with VMware, fake, or real Mac. Looks like MacOS does not set an environment variable for HOSTNAME or COMPUTERNAME (I don't see it in the posted screenshot). @madru's solution (a very good general purpose routine) is to check for both environmental variables and in case nothing turns up, the call he uses returns the fully qualified domain name of the machine. Just check for a period in the return value of his code, and if it exists, only use the string up to the first period and you have your host name.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@OliverA Your point is indeed relevant here, since the goal is to get the computername. I am using it to open folders on a Mac, so only the computername will do.
Not sure what Hostname will return in this case, since I have named my WMware MacOS El capitan "Macstation". It is needed to open the Documents folder on Mac
because the computername is part of the path. A little code module that checks these things could be useful. Please contribute 🙂
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You are so killing me:

B4X:
Sub GetSystemName As String
    Dim NativeMe As JavaObject = Me
    Dim name As String = NativeMe.RunMethod("SystemName", Null)
    If name = "" Then
        name = NativeMe.RunMethod("SystemName2", Null)
    Else
        If name.Contains(".") Then
            name = name.SubString2(0, name.IndexOf("."))
        End If
    End If
    Return name
End Sub

#If Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


// Adapted from https://stackoverflow.com/a/48476113
public static String SystemName2() { //throws java.io.IOException
    String host;
    try {
        Process hostname = Runtime.getRuntime().exec("hostname");
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(hostname.getInputStream()));
        host = stdInput.readLine().trim();
    } catch (IOException e) {
        host = "";
    }
    return host;
}

// Adapted from code provided by @madru
// https://www.b4x.com/android/forum/threads/getenvironmentvariable-computername-issue-on-mac.122073/post-763028

import java.net.InetAddress;
import java.net.UnknownHostException;

public static String SystemName() {
 
    String host = System.getenv("COMPUTERNAME");
    if (host == null) {
        host = System.getenv("HOSTNAME");
        if (host == null) {
            try {
                host = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                host = null;
            }
            if (host == null) host = "";
        }
    }
    
    return host.trim();
}

#End If
 
Upvote 0
Top