[英]Executing a Linux command in Terminal from Java
I am currently using a command called "curl" from Terminal in Ubuntu, tu upload my .RDF files to a Virtuoso RDF Store. 我目前在Ubuntu的Terminal中使用名为“ curl”的命令,将.RDF文件上传到Virtuoso RDF商店。
curl -T FILE URL -u USER:PASSWORD
I want to automatize these process, so that I create a Java function in eclipse. 我想使这些过程自动化,以便在eclipse中创建Java函数。 This code is not working.
该代码不起作用。
String[] command = {"curl -T", FILENAME, URL, "-u", credentials.USERNAME+":"+credentials.PASSWORD};
Runtime.getRuntime().exec(command)
I have also tried with this one. 我也尝试过这个。 The xterm appears, but it shows this error (even though the file is in the Path of the function):
xterm出现,但是它显示此错误(即使文件位于函数的路径中):
*"/usr/bin/xterm. Can't execvp "curl" no such a directory or file"*
Runtime.getRuntime().exec("/usr/bin/xterm -e \"curl -T " + FILENAME
+ " " + URL + "-u " + credentials.USERNAME
+ ":" + credentials.PASSWORD + "\"");
I would appreciate any help on the matter. 我将不胜感激。
Thanks in advance 提前致谢
I have had a hard time trying to get commands run using Runtime.exec()
in the past. 过去,我很难尝试使用
Runtime.exec()
运行命令。 Anyways I have shifted to using ProcessBuilder
as follows: 无论如何,我已经转向使用
ProcessBuilder
,如下所示:
ProcessBuilder pbuilder = new ProcessBuilder("bash", "-c", <<your command as string>>);
File err = new File("err.txt");
try {
pbuilder.redirectError(err);
Process p = pbuilder.start();
p.waitFor();
} catch (Exception e)
{
//handle exceptions if any.
}
The stderr line is optional, for debugging purposes. stderr行是可选的,用于调试目的。 I am sure that it could be directly printed out to console, but havn't checked on it yet.
我确信它可以直接打印到控制台,但尚未对其进行检查。 Will update my answer, once I find more.
找到更多信息后,将更新我的答案。
You can check out the documentation page here . 您可以在此处查看文档页面。
PS: Also check if you have the necessary permissions to carry out the desired task. PS:还要检查您是否具有执行所需任务所需的权限。
使用Runtime.exec通常不是一个好主意,为什么不使用Java HTTP API上传文件呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.