0

I am currently using a command called "curl" from Terminal in Ubuntu, tu upload my .RDF files to a Virtuoso RDF Store.

curl -T FILE URL -u USER:PASSWORD

I want to automatize these process, so that I create a Java function in eclipse. 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):

*"/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

User 23
  • 163
  • 1
  • 14
  • are you running it as yourself ? env variables may differ if not, and curl won't be visible if not in PATH variable.... you may want to debug something like `printenv PATH` and check – Palcente Jun 02 '15 at 15:09
  • If you want to simply execute a program on some files in Eclipse, then use "External tools", which are located next to Debug and Java toolbar icons. These launch configurations should be more convenient. – Michał Grzejszczak Jun 02 '15 at 16:00

2 Answers2

3

I have had a hard time trying to get commands run using Runtime.exec() in the past. Anyways I have shifted to using ProcessBuilder as follows:

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. 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.

Samuel Bushi
  • 341
  • 3
  • 16
  • I really appreciate it @blumonkey. I didn't know anything about the ProcessBuilder, but it really solves my problem : Anyway, I have problems catching the errors. This line doesn't work for me: ` pbuilder.redirectError(err); ` Instead, I have to use the `pbuilder.redirectErrorStream()` code, and I am not able to catch the errors. – User 23 Jun 03 '15 at 07:08
  • Strange. Maybe the errors are printed out to STDOUT rather than STDERR. Try to redirect the output as well. Refer [here](http://stackoverflow.com/questions/16714127/how-to-redirect-process-builders-output-to-a-string) to redirect to the console. You can modify the accepted answer a bit for errorStream. – Samuel Bushi Jun 03 '15 at 07:43
0

Using Runtime.exec is generally a bad idea, why not use the Java HTTP APIs to upload the files ?

Sunny Das
  • 91
  • 1
  • 3