小编Ale*_*mek的帖子

使用Unix命令在java中打印我的Mac序列号

我想在java程序中打印我的mac [编辑:Apple计算机]序列号.我熟悉Unix命令

ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'
Run Code Online (Sandbox Code Playgroud)

这在终端完成了这项任务.
当我尝试

String command = "ioreg -l | awk '/IOPlatformSerialNumber/ { print $4; }'"
Runtime terminal = Runtime.getRuntime(); 
String input = new BufferedReader(
    new InputStreamReader(
        terminal.exec(commands).getInputStream())).readLine();
System.out.println(new BufferedReader(
    new InputStreamReader(
        terminal.exec(command, args).getInputStream())).readLine());
Run Code Online (Sandbox Code Playgroud)

我的序列号没有打印出来.相反它打印:

<+-o Root class IORegistryEntry, id 0x100000100, retain 10>  
Run Code Online (Sandbox Code Playgroud)

我认为问题在于terminal.exec()并不是要占用整个命令字符串.java中的某些东西是否类似于shell = Truepython中的参数,Popen(command, stdout=PIPE, shell=True)它允许我传递整个命令字符串?

java unix io macos

5
推荐指数
2
解决办法
5714
查看次数

标签 统计

io ×1

java ×1

macos ×1

unix ×1