如何在Java中运行bash脚本(下载文件)?

编程入门 行业动态 更新时间:2024-10-16 16:53:53
本文介绍了如何在Java中运行bash脚本(下载文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Mac OS X Lion中运行,我需要使用命令行中的脚本从远程服务器检索文件。我试图在代码中使用的命令是bash / my / path / here / myscript,我已经使用以下代码从命令行(atos)运行另一个进程。

Running in Mac OS X Lion, I need to retrieve a file from a remote server using a script in the command line. The command I'm trying to use in code is "bash /my/path/here/myscript" and I already run another process from the command line (atos) using the code below.

Process proc = Runtime.getRuntime().exec(cmd);

但是在调试时,程序继续没有错误,但脚本确实似乎已经运行。此外,脚本检索文件时应该暂停几秒钟,但是我的程序会立即继续执行。从终端运行时,脚本本身就像预期一样工作。我有点困惑,如何让这个工作,所以任何帮助将不胜感激。

But while debugging, the program continues without error, yet the script does appear to have actually run. Furthermore, there should be a pause of several seconds while the script retrieves the file, yet my program continues to execute immediately. The script itself works as intended when run from the terminal. I'm a little stumped on how to get this to work, so any help would be greatly appreciated.

推荐答案

使用以下代码 -

Process proc = Runtime.getRuntime().exec(cmd); BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); proc.waitFor(); while (in.ready()) { System.out.println(in.readLine()); }

另一件事是一个问题是脚本会下载到当前工作目录,而不是脚本本身的位置(如预期)。所以脚本会正常运行,而程序会继续找不到下载的文件。希望这有帮助。

The other thing that was an issue is that the script would download to the current working directory rather than the location of the script itself (as intended). So the script would run correctly while my program would continue to fail to find the downloaded file. Hope this helps.

更多推荐

如何在Java中运行bash脚本(下载文件)?

本文发布于:2023-11-23 19:07:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   文件   如何在   bash   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!