gpt4 book ai didi

java - ProcessBuilder 抛出异常 : Arguement contains =

转载 作者:太空宇宙 更新时间:2023-11-04 05:04:52 24 4
gpt4 key购买 nike

我正在尝试使用 java 在 Linux 上为 raid 驱动器创建文件系统。

当我尝试使用 ProcessBuilder 执行以下命令时遇到以下问题。

Process '[/usr/bin/sudo, /sbin/mkfs.ext4, /dev/md0, -m 1, -O uninit_bg, -E lazy_itable_init=1]' stderr dump: mke2fs 1.42.3 (14-May-2012)

Bad option(s) specified: lazy_ _init

Extended options are separated by commas, and may take an argument which is set off by an equals ('=') sign.

Valid extended options are: stride= stripe-width= resize= lazy_itable_init=<0 to disable, 1 to enable> lazy_journal_init=<0 to disable, 1 to enable> test_fs discard nodiscard quotatype=

我的 ProcessBuilder 对象看起来像这样

mkfs("ext4", "/dev/md0", "-m 1", "-O uninit_bg", "-E lazy_itable_init=1")

public void mkfs(String filesystem, String devicename, String... options) {
mkfsCommand.add(SUDO_CMD);
if(fileSystemType.equals("xfs")){
mkfsCommand.add("/sbin/mkfs.xfs");
} else if (fileSystemType.equals("ext4")) {
mkfsCommand.add("/sbin/mkfs.ext4");
}

mkfsCommand.add(deviceName);
for (String option:options) {
mkfsCommand.add(option);
}
List<String> mkfsCommand = Lists.newLinkedList();
Exec.exec(ProcessBuilder(mkfsCommand))
}

Exec.exec 是我用来执行系统命令的库。我无法更改它,但这是它的作用的片段。

public String exec(ProcessBuilder pb) {
String cmdString = Arrays.deepToString(pb.command().toArray(new String[0]));
int retval = 0;
Process p = null;
String output = "";
if(shouldLog) {
logger.debug("in exec: " + cmdString);
}
String error = null;
try {
p = pb.start();
if (executionDeadline != null) {
Thread durationEnforcer = new DurationEnforcer(executionDeadline, p);
durationEnforcer.start();
}
Future<String> futureErr = null;
if (!pb.redirectErrorStream()) {
futureErr = SingleExecutor.submit(new StreamReader(p.getErrorStream()));
}
StreamReader outputReader = new StreamReader(p.getInputStream());
p.getOutputStream().close();
output = outputReader.call();
retval = p.waitFor();

if (futureErr != null) {
error = futureErr.get();
}
if (error != null && !error.isEmpty()) {
if(shouldLog) {
logger.warn("Process '" + cmdString + "' stderr dump:\n" + error);
}
}
this.stdOutput = output;
this.stdError = error;
} catch (Exception e) {
logger.warn("exec failed: " + e + "\nstdout:\n" + output);
throw new RuntimeException("exec failed: ", e);
} finally {
StreamHelper.closeStreamsAndDestroy(p);
}
if (retval != 0) {
String msg = "exec failed with " + retval + ": " + cmdString;
if(shouldLog) {
logger.warn(msg + "\nstdout:\n" + output);
}
throw new ExecException(error, cmdString, retval, output);
}
if(shouldLog) {
logger.debug("finished exec: " + cmdString);
}

return output;
}
}

我不确定为什么会收到此错误。我怀疑 -E lazy_itable_init=1 没有正确传递给 Processbuilder。对发生的事情有什么想法吗?

最佳答案

我怀疑这是你的命令String....

"ext4", "/dev/md0", "-m 1", "-O uninit_bg", "-E lazy_itable_init=1"

这基本上是对 ext4 说,你有 4 个参数......

  • /dev/md0
  • -m 1
  • -O uninit_bg
  • -Elazy_itable_init=1

当我怀疑它实际上想要 7 时。

  • /dev/md0
  • -m
  • 1
  • -O
  • uninit_bg
  • -E
  • lazy_itable_init=1

或者类似...

相反,尝试使用诸如...之类的东西

"ext4", "/dev/md0", "-m", "1", "-O", "uninit_bg", "-E", "lazy_itable_init=1" 

您传递的 ProcessBuilder 的每个 String 元素都将被视为命令的单独参数(例如,每个参数在命令行上用空格分隔)。

这使得 ProcessBuilder 非常强大,因为它可以处理实际上包含空格的命令参数,而无需担心转义引号:P

关于java - ProcessBuilder 抛出异常 : Arguement contains =,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com