微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从java传递命令提示符函数

我想从Java获取Windows命令提示符的结果。

Java代码

Process process2 = Runtime.getRuntime().exec("cmd /c getmac");

除了Runtime是否还有其他可用的Runtime ?

closures从批处理脚本启动的新控制台窗口会提示终止脚本

如何使用Java或CMD获取PC硬件信息

如何使用batch file中的if-clause中的括号来转义variables?

如何从命令行获取当前的Linux进程ID a与shell无关,与语言无关

命令提示符“networking视图系统错误6118”

在CentOS中尾随多个文件

将django manage.py输出(在Windows中)redirect到一个文本文件

使用wget和Windows Scheduler运行一个PHP脚本

如何从windows命令行中以分离状态运行应用程序

Ack有时不会显示行号并丢失着色

使用Apache Commons Exec 。 它可以用来获得正在运行的进程的控制台输出

来自真实项目的代码的一部分

private final Executor executor = new DefaultExecutor(); private final ExecuteWatchdog watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); private final ProcessDestroyer shutdownHookProcessDestroyer = new ShutdownHookProcessDestroyer(); private final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler() { @Override public void onProcessFailed(ExecuteException ex) { super.onProcessFailed(ex); LOG.error("Error executing xxx.exe",ex); } }; public void startInSaveRecordMode(Long callId,File pathToResult) throws IOException { CommandLine commandLine = createModeUserPasswordCommandLine(SAVE_RECORD_MODE_COMMAND) .addArgument(ValidationUtil.toString(callId)).addArgument( ValidationUtil.toString(pathToResult)); execute(commandLine); } private void execute(CommandLine commandLine) throws IOException { Assert.notNull(pathToProcess); executor.setWatchdog(watchDog); executor.setProcessDestroyer(shutdownHookProcessDestroyer); executor.setStreamHandler(createStreamHandler()); if (LOG.isDebugEnabled()) { LOG.debug("Executing " + commandLine); } executor.execute(commandLine,resultHandler); } private CommandLine createModeUserPasswordCommandLine(String mode) { Assert.hasLength(sensormUser); Assert.notNull(sensormPassword); return createCommandLine().addArgument(mode).addArgument(sensormUser); } private CommandLine createCommandLine() { return new CommandLine(pathToProcess); } private ExecuteStreamHandler createStreamHandler() { OutputEventsHandler eventsHandler = new OutputEventsHandler(eventsQueue); SensormlogHandler errorLogHandler = new SensormlogHandler(LOG,Level.ERROR); return new PumpStreamHandler(eventsHandler,errorLogHandler); } public int waitFor() throws InterruptedException { resultHandler.waitFor(); return resultHandler.getExitValue(); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐