I am trying to write Java code to run wget to retrieve an image from a server
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.
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();
}
}
}
When I try to run it on OSX, I get the runtime error:
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.
If
wget
is indeed installed on your OS X system, then try to specify the full path to it.Try:
from the command line, then use that fully qualified path in your Java application.
It looks like
wget
is not installed on that OSX system. (It isn't on mine either, despite that being a few generations older.) Either install it or find another way to download a picture; Java does have HTTP support built-in natively after all (see thejava.net.URL
class).None of these answers explain what your actual problem is.
The reason Java is failing is that /usr/local/bin isn't on your PATH.
Obviously it is on the path of the bash shell you're running in Terminal. And probably of any new bash shell you start in Terminal (or via ssh, or whatever). That's probably because you've got a line like
export PATH=$PATH:/usr/local/bin
somewhere in ~/.profile, ~/.bash_profile, ~/.bashrc, or the /etc equivalents.On linux, all your GUI stuff is a child of a login shell, so putting something in one of those files (as long as you pick the right one) means Java will end up inheriting that PATH no matter how it gets launched. But on Mac, all your GUI stuff is a child of launchd, and any shell you run is just a sibling of your GUI apps, not the parent. So, setting PATH in bash's startup isn't going to affect something launched from the Finder or an IDE or whatever.
Once you understand the problem, you can understand all the different solutions—you can set the default environment launchd gives to user processes, or do the same thing system-wide, or modify /etc/paths, etc.
Pretty obviously,
wget
is not installed by default in OS X.Something more interesting is to write functionality like wget your own.
if you get this error one more time , execute command like this :