使用Java(OSX)在终端中调用命令 [英] Call a command in terminal using Java (OSX)
问题描述
我正在尝试编写Java代码以运行wget从服务器检索图像
I am trying to write Java code to run wget to retrieve an image from a server
我相信我已经正确安装了wget.如果我输入:
I believe that I have wget properly installed. If I type:
wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe
我在用户帐户文件夹中找到该图像.
I find the image in my user account folder.
以下Java代码在Ubuntu上正常运行,但是我不得不将项目移至OSX(Mountain Lion)
The following Java code was working properly on Ubuntu, but I had to move the project over to OSX (Mountain Lion)
import java.io.*;
import java.io.IOException;
public class runWget
{
public static void main (String args[])
{
String whatToRun = "wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe";
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(whatToRun);
int exitVal = proc.waitFor();
System.out.println("Process exitValue:" + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
当我尝试在OSX上运行它时,出现运行时错误:
When I try to run it on OSX, I get the runtime error:
java.io.IOException:无法运行程序"wget":error = 2,没有这样的文件 或目录
java.io.IOException: Cannot run program "wget": error=2, No such file or directory
如果有人能告诉我我做错了,我将不胜感激.
I would greatly appreciate if someone could tell me what I am doing wrong.
推荐答案
如果您的OS X系统上确实安装了wget
,请尝试指定其完整路径.
If wget
is indeed installed on your OS X system, then try to specify the full path to it.
尝试:
哪个wget
which wget
在命令行中,然后在Java应用程序中使用该标准路径.
from the command line, then use that fully qualified path in your Java application.
String whatToRun = "/usr/local/bin/wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe";
这篇关于使用Java(OSX)在终端中调用命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!