views:

2838

answers:

9

I've been researching a little and I found some rsync algorithm implementations written in Java but it seems all the projects died some time ago.

What is a good Java library implementing rsync? If there's not one: Is the best way for use rsync to invoke the program from my Java app?

+1  A: 

I don't know of any libraries, but you can invoke system commands in Java using Runtime.getRuntime().exec(command);

Here's an example:

 Runtime rt = Runtime.getRuntime();
 Process proc = null;
 try
 {
  proc = rt.exec(cmd);

  InputStream stderr = proc.getErrorStream();
  InputStreamReader isrErr = new InputStreamReader(stderr);
  BufferedReader brErr = new BufferedReader(isrErr);

  InputStream stdout = proc.getInputStream();
  InputStreamReader isrStd = new InputStreamReader(stdout);
  BufferedReader brStd = new BufferedReader(isrStd);

  String val = null;
  while ((val = brStd.readLine()) != null)
  {
   //handle program output
  }

  while ((val = brErr.readLine()) != null)
  {
   //handle program errors
  }

  int exitVal = proc.waitFor();
 }
 catch (Exception e)
 {
  //handle exception
 }
Jesse
Thanks, I do know how to invoke commands. My question was if the best approach was to run it instead of using a library
victor hugo
That depends on what you're doing with it. If you're using java to call rsync for backup purposes, then you should consider something else (i.e. cron). If you're doing something with the data in your java app, then a library would be better.
Jesse
I consider your comment a better answer :)
victor hugo
+1  A: 

The only library I have found is: jarsync but it is not actively developed. Another future possibility is: java-rsync

Joshua
Nice googling, the first one is the one I was refering to in the question and java-rsync has no functionality, it's just a project with some empty classes but with no actual working code
victor hugo
A: 

i use rsync with cron for java apps, say rsync is platformdependent, you can have the "same" function, not the name rsync, per definition rsync is too close to os to think its platformindependent.

LarsOn
How come you think it is "too close to os"?
Thorbjørn Ravn Andersen
instructed so, after using ready script to tie together frequency analyses. pipline script to build concordance db was too fast, too easy, supervisor changed rules to forbid using wellproven tokenizer, awk and didn't buy it, requiring java methods instead of chaing gnu and os tools to build concordance database
LarsOn
A: 

If Java's cross-platform qualities aren't a huge boon for you (e.g. you're only planning to run your software on Unix flavors), I'd recommend going with the command execution approach. I too have searched for a Java rsync library in the past, albeit about 2 years ago. Unless something has changed since then, you'd be way better off executing OS commands than using a piece of software that is only dubiously compatible with rsync itself and not actively maintained or used by anyone else. Understand that in the long term such a library will become a liability, especially if it breaks or needs to have its functionality expanded.

If none of the above appeals to you, see if you can survive without using rsync at all. Simple file transfer can be accomplished using a plethora of other methods.

Max A.
+2  A: 

There is a C library called librsync that provides an C API for the rsync protocol. You could easily write a Java wrapper using JNI. Only problem is the project seems dormant. But if it works for you, there's a solution right there.

gavinb
A: 

Yeah, like most people have already pointed out, executing an underlying commandline execution from your Java program (where rsync is already installed on the computers in question) is the best route. What I do is...

        // Currently uses passwordless SSH keys to login to sword
        String[] cmd = new String[]{"rsync", "-r", USER + "@" + HOST + ":" + REMOTE_FILE_ROOT, LOCAL_FILE_ROOT};
        ProcessBuilder pb = new ProcessBuilder(cmd);
        Process p = pb.start();
        int val = p.waitFor();
        if (val != 0) {
            throw new Exception("Exception during RSync; return code = " + val);
        }

I actually run my rsync command as a cron-style job in my Java programs using the cron4j library.

David Johnson
A: 

I ended up writing my own synchronisation code because I only required one-way-sync (but for a large file repository). If you do not need two-way-sync, you might have a look at software for mirroring sites (like wget).

As I needed a pure-Java solution, I did as follows:

  • Rerieve a file list from the server with file length and modification date
  • Loop over the file list and compare to the local files
    • Retrieve file from the server if new or changed (create directories if needed)
  • Loop over local files and see if there are files that not in the retrieved file list; delete these files (delete empty directories)

This is somewhat simplicistic and feels like re-inventing the wheel, but did the job for me in just a few hours. :-)

Matthias Wuttke