Java current machine name and logged in user?
To get the currently logged in user:
System.getProperty("user.name"); //platform independent
and the hostname of the machine:
java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
System.out.println("Hostname of local machine: " + localMachine.getHostName());
How to get current user name of Linux in Java
To get the output of the process, you need to attach the InputStream
of to the normal output of the subprocess. Then, you can read from it by creating a BufferedReader
. To get the first line of the output of the process (on stdout) you can use this code:
Process proc = Runtime.getRuntime().exec("whoami");
BufferedReader stdin = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String username = stdin.readLine();
System.out.println(username);
You can also get the InputStream
of the error output of the subprocess (stderr) with proc.getErrorStream()
if this should be necessary.
However, there is a better way if you want to get the username of the user executing the VM:
String username = System.getProperty("user.name");
System.out.println(username);
Getting the computer name in Java
The computer "name" is resolved from the IP address by the underlying DNS (Domain Name System) library of the OS. There's no universal concept of a computer name across OSes, but DNS is generally available. If the computer name hasn't been configured so DNS can resolve it, it isn't available.
import java.net.InetAddress;
import java.net.UnknownHostException;
String hostname = "Unknown";
try
{
InetAddress addr;
addr = InetAddress.getLocalHost();
hostname = addr.getHostName();
}
catch (UnknownHostException ex)
{
System.out.println("Hostname can not be resolved");
}
Get login username in java
System.getProperty("user.name");
Get Current logged User Id and User Name using JavaFX
Let me get this straight, you have the user login, then change the scene to a main window, but you want to remember the user that logged in and display that username on the homepage?
Sounds like you will have to pass data between scenes.
For that you will need to approach this in OOP. Have a object class representing your user with all the getters and setters.
public class User {
private String email;
public User(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
When you connect to the database at login, validate user then instantiate an object of the "User" class for example, then pass it to the mainwindow scene your are loading.
public class LoginController implements Initializable {
public User user;
// All your FXML code
@FXML
void handleLogin(ActionEvent actionEvent) throws IOException {
// Do your validation and then call the changeToMainWindow()
changeToMainWindow();
}
}
Have a "initData" class or something in the mainwindowcontroller.
Like
public void initData(User user) {
selectedUser = user;
labelUser.setText(selectedUser.getEmail());
}
Then from your login class, upon validation, send the data to the mainwindow before changing your scene by instantiating your User, then passing the object to the initData method from your second scene.
//User validation, then:
// Get the FXMLLoader then
//Instantiate the mainwindow controller:
public void changeToMainWindow() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("mainwindow.fxml"));
Parent root = loader.load();
Scene mainScene = new Scene(root);
// access the controller
MainWindowController mainWindowController = loader.getController();
mainWindowController.initData(user);
Stage primaryStage = (Stage) loginButton.getScene().getWindow();
primaryStage.setScene(mainScene);
primaryStage.show();
}
Then upoin login, use the changeToMainWindow() method and it'll pass the user.
In the above example I am simply passing email, but you get the point.
Related Topics
Access to Private Inherited Fields via Reflection in Java
Technique or Utility to Minimize Java "Warm-Up" Time
Using Powermockito.Whennew() Is Not Getting Mocked and Original Method Is Called
Java Serialization with Non Serializable Parts
How to Copy a Java.Util.List into Another Java.Util.List
Maven Compilation Error: (Use -Source 7 or Higher to Enable Diamond Operator)
Replace the Last Part of a String
Error: Servlet Jar Not Loaded... Offending Class: Javax/Servlet/Servlet.Class
Java 8 Instant.Now() with Nanosecond Resolution
Reference Is Ambiguous with Generics
Java, Simplified Check If Int Array Contains Int
Where Are Compiled Jsp Java (*_Jsp.Java) Files
How to Solve "Java.Io.Ioexception: Error=12, Cannot Allocate Memory" Calling Runtime#Exec()
How to Properly Express Jpql "Join Fetch" with "Where" Clause as JPA 2 Criteriaquery