Call a command in terminal using Java (OSX)

2019-06-09 05:32发布

问题:

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.

回答1:

If wget is indeed installed on your OS X system, then try to specify the full path to it.

Try:

which wget

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";


回答2:

Pretty obviously, wget is not installed by default in OS X.

Something more interesting is to write functionality like wget your own.

URL url = new URL("http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe");
InputStream in = url.openStream();
OutputStream os = new FileOutputStream(new File("picture.jpe"));
byte byffer[] = new byte[1024];
int nBytesRead;
while ((nBytesRead = in.read(buffer)) != -1)
{
    os.write(buffer, 0, nBytesRead);
}
os.flush();
os.close();
in.close();


回答3:

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.



回答4:

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 the java.net.URL class).



回答5:

if you get this error one more time , execute command like this :

which wget

Runtime.getRuntime().exec(new String[]{ "/bin/sh" , "-c" ,"/usr/local/bin/wget http://insitu.fruitfly.org/insitu_image_storage/img_dir_38/insitu38795.jpe" })