Java-如何通过JLabel运行特定的DOS命令

Java-如何通过JLabel运行特定的DOS命令,java,swing,shell,batch-file,jlabel,Java,Swing,Shell,Batch File,Jlabel,我有一个JLabel,上面有一些文本,我想通过JLabel运行“echo%USERNAME%”命令,以便在NetBeans IDE 8.2上运行代码后,它应该在以下Windows 7最终用户的JLabel文本上打印用户名 例如:我用JLabel写了一段文字:“你好,访客” 我想在echo%USERNAME%的帮助下将访问者更改为用户名,以便它在JLabel上打印Windows 7最终用户的用户名 感谢您很抱歉用问题回答问题,但您想获取帐户的用户名并将其存储在字符串中,然后将其用作某个对象的属性吗

我有一个JLabel,上面有一些文本,我想通过JLabel运行“echo%USERNAME%”命令,以便在NetBeans IDE 8.2上运行代码后,它应该在以下Windows 7最终用户的JLabel文本上打印用户名

例如:我用JLabel写了一段文字:“你好,访客” 我想在echo%USERNAME%的帮助下将访问者更改为用户名,以便它在JLabel上打印Windows 7最终用户的用户名


感谢您

很抱歉用问题回答问题,但您想获取帐户的用户名并将其存储在字符串中,然后将其用作某个对象的属性吗

如果是这样,则有一个名为
System.getProperty(“user.name”)的方法;


这就是我理解你的问题,如果这是错误的,我道歉。同样对于运行shell命令(特定于平台),我将使用ProcessBuilder或
Runtime.exec(“%USERNAME”)取决于您使用的Java版本。对于这两种方法中的后一种,也会很有帮助

如果您只需要计算机的用户名,那么请务必使用
System.getProperty(“User.Name”)是一条路要走。但是,如果要在Windows命令提示符下抛出其他项,则可能需要使用类似于以下的runCMD()方法:

List List=runCMD(“/C echo%USERNAME%”);
如果(!list.isEmpty()){
对于(int i=0;i
控制台将显示当前用户名

该方法可能如下所示:

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;
    } 
}
public List runCMD(String命令字符串){
//从提供的命令字符串中删除CMD
//如果存在的话。
if(commandString.toLowerCase().startsWith(“cmd”)){
commandString=commandString.substring(4);
}
列表结果=新建ArrayList();
试一试{
//启动命令提示符并处理
//提供的命令字符串。
进程p=Runtime.getRuntime().exec(“cmd”+commandString);
//读取命令提示符的进程输入流。
try(BufferedReader in=新的BufferedReader(
新的InputStreamReader(p.getInputStream())){
字符串行=null;
//将流中的内容存储到我们的ArrayList中。
而((line=in.readLine())!=null){
结果。添加(行);
}
}
p、 destroy();//终止进程
返回结果;
} 
捕获(IOE异常){
System.err.println(“runCMD()方法错误!-处理期间IO错误”
+提供的命令字符串的“!\n”+e.getMessage());
返回null;
} 
}

谢谢您的回复,但是有没有办法运行System.getProperty(“user.name”);在NetBeansWell上编译代码后在JLabel上编译代码与运行代码不同。如果你想运行你的代码,编译是不够的,你必须让JVM执行你的代码。@Brenn Oct谢谢兄弟,我终于在Java中的jLabel的帮助下运行了它,即jLabel.setText(“欢迎”+System.getProperty(“user.name”);太神了很乐意帮忙
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;
    } 
}