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

2
  • 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 Commented Jun 2, 2015 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. Commented Jun 2, 2015 at 16:00

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Strange. Maybe the errors are printed out to STDOUT rather than STDERR. Try to redirect the output as well. Refer here to redirect to the console. You can modify the accepted answer a bit for errorStream.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.