How to … Access Subversion from C#

Allthough there are a lot of language bindings available for Subversion, C# is not one of them. A few resources on the internet provide some kind of binding but they are all external to the Subversion project.

With IKVM however, you can compile the Java language binding of the Subversion project to make it accessible using C#.

Here is how you do it:

Getting the necessary files

Install IKVM.NET

You can download IKVM.NET from their website. Just go to the download section and click through to Sourceforge. Select the file with “bin” in its name because that file contains the binaries. Because I used version 0.30.0.0 the file was named ” ikvmbin-0.30.0.0.zip”. You can download it using this link.

After downloading this file, you must unzip it in some folder. This is the folder structure that will result:

ikvm.gif

Install the Subversion java language bindings

For this you must go to the Subversion website and select the “Documents & files” inside the “Project tools” item on the left. Next expand following nodes in the central tree structure:
subversion > Releases > Windows

Then look for a file named svn-win32-(version)_javahl.zip in which (version) is of course the version. I used version 1.3.1 which is not the most recent. You can find it in the “Windows Archive” node.

The file I used had thus the name “svn-win32-1.3.1_javahl.zip”.

Once downloaded you unzip it into a folder of your choice. After this you end up with following folderstructure:

subversionjava.gif

Using the Subversion java language bindings

Compiling the java language bindings for .NET

Copy the complete content of the IKVM.NET “bin” folder to the Subversion language bindings “javahl” folder. Like this all files are in the same folder.

Open a command window in this folder and type following on the commandline:

ikvmc svnjavahl.jar

IKVM.NET will compile the provided jar file and produce a dll with the same name. This dll contains the .NET version of the java classes.

Accessing a Subversion repository from C#

Create a console application in your favourite IDE.

Add references to the following files:

  • IKVM.GNU.Classpath.dll (which comes with IKVM.NET)
  • svnjavahl.dll (which you created by compiling svnjavahl.jzr with IKVM.NET)

Copy the following code in the project you just created:

using System;

using System.Collections.Generic;

 

namespace SubversionTest

{

    class MainClass

    {

        public static void Main(string[] args)

        {

            org.tigris.subversion.javahl.SVNClient svnClient

                = new org.tigris.subversion.javahl.SVNClient();   

 

            // replace the following string with the name of a

            //  subversion repsoitory available to you

            //  example “svn://localhost/MyProject”

            string moduleName = “<yourRepo>”;

 

            // replace the following string with the name of a

            //  folder available on your computer

            //  example “c:/MyProject”

            string destinationFolder = “<yourFolder>”;

            org.tigris.subversion.javahl.Revision revision =

                org.tigris.subversion.javahl.Revision.HEAD;

            bool recurse = true;

 

            svnClient.checkout(moduleName, destinationFolder, revision, recurse);

        }

    }

}

Do not forget to replace the strings with appropriate values for your setup.

Compile the project

Copy following files to the folder containing the build results (typically a subfolder bin\Debug for the debug version or bin\Release for the release version)

  • ikvm-native.dll
  • libsvnjavahl-1.dll

Hit the Run button.
If everything went well your repository shuold be chacked out on your computer.

Resources

[1] IKVM.NET and how to convert a java application to .NET
[2] Subversion and Documents & files
[3] Online documentation for javahl classes can be viewed here

Updates

21 December 2006: original version

Advertisements

17 thoughts on “How to … Access Subversion from C#

  1. Hi sree,

    I’ve just tried it with VB.NET 2003 and it works fine. I do notice however, through ildasm, that there is a dependency of IKVM.GNU.Classpath on IKVM.Runtime. But in Visual Studio this dependency gets resolved and the IKVM.Runtime.dll gets copied and everything works fine.

    I also set the references to the dll’s in the folder where I compiled everything, so IKVM.Runtime.dll is available alongside the two referenced dll’s.

    Hope this helps.

    Serge

  2. Can you provide an example of how you included the references to the two dll’s in C#? I am having a problem getting the org.tigris lines to compile in a Visual 2005 C# project. Thanks.

  3. Hi Andrew,

    sorry for the late answer.

    I simply use the “Browse” tab of the “Add Reference” dialogbox. This last dialogbox you get when you right-click on the “References” folder in the Visual Studio Solution Explorer.

    Hope this helps…

  4. I have walked through all of these steps (and repeated them several times, just in case) and I can’t get this to work. The program compiles justI’ve gotten past the errors the previous two people asked about, but now whenever I run my application I get the following error:

    java.lang.UnsatisfiedLinkError: Native library `svnjavahl’ not found (as file `svnjavahl.dll’) in gnu.classpath.boot.library.path and java.library.path .

    I’m using Visual Studio .NET 2003 on a Windows XP box. I’ve got Java 1.4.2 installed, although I think that shouldn’t matter, because the point of this article is to compile these libraries for .NET . Any ideas? Thanks.

  5. I tried your instructions but I keep getting an java.lang.UnsatisfiedLinkError from IKVM.Runtime. The message in the exception is “org/tigris/subversion/javahl/SVNClient.initNative()V”. Do I need the Java Runtime Environment installed or something? I’ve tried different combinations of versions of ikvm and the svnjavahl but none of them worked and I kept getting the exception. I know these products aren’t your’s but any help you could provide would be much appreciated.

  6. Hi Ben and Shawn,

    Are you sure you have the necessary files copied to your applications folder:
    – IKVM.Runtime.dll
    – IKVM.GNU.Classpath.dll
    – svnjavahl.dll
    – ikvm-native.dll
    – libsvnjavahl-1.dll

    Especially Shawn: I experienced the same problem and copying the libsvnjavahl-1.dll file next to my application solved it.

  7. Thank you for your response.

    I did have the list of DLLs (especially the libsvnjavahl-1.dll) that you mentioned together next to the application but I was still getting the exception. I experimented a little further and launched the vnjavahlTest.exe (the app I wrote to test this) and I got an error that I did not get while running in debug mode using the VS2K5 IDE. It was complaining about not being able to find one of the Subversion DLLs. I added the Subversion DLLs it was looking for next to test app and it seems to work now. I was hoping to avoid including the Subversion binaries with the app but it doesn’t look like I’ll be able to.

  8. Hi Shawn,

    In hintsight it does make sence, because I have the Subversion installation folder included in the windows environment PATH variable which enables windows to find those DLL’s. I will adapt my post to include this.

    Thank you for sharing your experience.

  9. hi guys,

    so does anybody success on this experiment? i’m getting error message as shown below. i wonder what it is. can someone help me?

    here is my spec:
    – IKVM 0.34.0.2
    – svnjavahl.jar 1.4.3.23084
    – subversion 1.4.3
    – .NET 2.0
    – jre1.5.0_11

    ################### ERROR MESSAGE ###################
    The type initializer for ‘org.tigris.subversion.javahl.SVNClient’ threw an exception.
    ” at IKVM.Runtime.JNI.Frame.GetFuncPtr(RuntimeMethodHandle method, String clazz, String name, String sig)\r\n at org.tigris.subversion.javahl.SVNClient.initNative()\r\n at org.tigris.subversion.javahl.NativeResources.init()\r\n at org.tigris.subversion.javahl.NativeResources.loadNativeLibrary()\r\n at org.tigris.subversion.javahl.SVNClient..cctor()”
    #####################################################

  10. I have the same error :
    The type initializer for ‘org.tigris.subversion.javahl.SVNClient’ threw an exception.

  11. Just to let you know, if some of you are having issues with this, make sure you are building the app for a 32 bit executable… I ran into all sorts of trouble because it defaults to a 64 bit build and apparently calling the library from 64 bit did not go over too well.

  12. This is very strange. I can develop my application just perfectly on my computer, and running it on that computer works fine. I then try to test distributing my program, so I load it on my laptop (which has visual studio on it) and run the exe and the program works perfectly… (SVN is not installed on the laptop). But then I move it to my friends computer, and I have them run it, and it gives me that Type Initializer error. I have included, ALL the dll’s from my bin folder that would need to be copied over, this includes all of the IKVM dlls and the svnjavahl dlls and everything, and it still gives me that error. Am I missing a required reference by any chance?

  13. Thanks for your post.

    But I have an issue – Native library ‘svnjavahl’ not found in gnu.classpath.boot.library and java.library.path.

    I followed all instructions and all .dll files I have in the .net bin folder.

    Any idears?

  14. Guys

    Any update on the error “The type initializer for ‘org.tigris.subversion.javahl.SVNClient’ threw an exception.” ?

  15. {“The type initializer for ‘org.tigris.subversion.javahl.SVNClient’ threw an exception.”}

    Inner Exception: {“org/tigris/subversion/javahl/SVNClient.initNative()V”}
    Source: “IKVM.Runtime”
    Stack Trace: ” at IKVM.Runtime.JNI.Frame.GetFuncPtr(RuntimeMethodHandle method, String clazz, String name, String sig)\r\n at org.tigris.subversion.javahl.SVNClient.initNative()\r\n at org.tigris.subversion.javahl.NativeResources.loadNativeLibrary()\r\n at org.tigris.subversion.javahl.SVNClient..cctor()”

    Can anyone help for this problem?

  16. Any update on this error?

    I’m also having troubles wiht the Type Initializer error.

    In my dev machine everything works fine… but when running on a server (Win Server 2008) I’m getting that error! I don’t understand.. I copy all the required dlls..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s