1

I've a JLabel with some text on it and i would like to run "echo %USERNAME%" command through the JLabel so that once i've run the code on NetBeans IDE 8.2 it should print the USERNAME on the JLabel text of the following Windows 7 end user

For example : I've JLabel with the text : "Hello Visitor" I want to change Visitor to the USERNAME with the help of echo %USERNAME% so that it should print the USERNAME of the Windows 7 end user on the JLabel

Thank you

2 Answers2

2

Apologies for responding to a question with a question, but you want to get the username of an account and store it in a string, then use it as an attribute of some object?

If so, there is a method called System.getProperty("user.name");

This is what I've understood your question to be, apologies if that is incorrect. Also for running shell commands (platform specific), I would use the ProcessBuilder or Runtime.exec("%USERNAME"); depending on which version of Java you are using. With the latter of the two, this will also be helpful

Brenann
  • 104
  • 6
  • Thanks for the reply but is there any way to run System.getProperty("user.name"); on the JLabel after code compilation on NetBeans – Chetan Nautiyal Oct 21 '17 at 08:46
  • Well compiling your code is not the same as running it. If you wanted to run your code, compilation is not enough, you must let the JVM execute your code. – Brenann Oct 21 '17 at 17:28
  • 1
    @ Brenann Oct thanks bro , i just finally got it running with the help of jLabel in Java i.e. jLabel.setText("Welcome " + System.getProperty("user.name")); – Chetan Nautiyal Oct 25 '17 at 17:01
  • Amazing! Happy to help – Brenann Oct 29 '17 at 22:28
1

If all you want is the Computer's User Name then by all means using the System.getProperty("user.name"); is the way to go. However, if there are other items you want to throw across the Windows Command Prompt then you might want to utilize something like this with a runCMD() method:

List<String> list = runCMD("/C echo %USERNAME%");
if (!list.isEmpty()) {
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

Console will display the current User Name.

The method might look something like this:

public List<String> runCMD(String commandString) {
    // Remove CMD from the supplied Command String
    // if it exists.
    if (commandString.toLowerCase().startsWith("cmd ")) {
        commandString = commandString.substring(4);
    }

    List<String> result = new ArrayList<>();
    try {
        // Fire up the Command Prompt and process the
        // supplied Command String.
        Process p = Runtime.getRuntime().exec("cmd " + commandString);
        // Read the process input stream of the command prompt.
        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(p.getInputStream()))) {
            String line = null;
            // Store what is in the stream into our ArrayList.
            while ((line = in.readLine()) != null) {
                result.add(line);
            }
        }
        p.destroy(); // Kill the process
        return result;
    } 
    catch (IOException e) {
        System.err.println("runCMD() Method Error! - IO Error during processing "
                         + "of the supplied command string!\n" + e.getMessage());
        return null;
    } 
}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22