How to … Access Subversion from C#
December 21, 2006
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:
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:
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:
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





February 12, 2007 at 6:12 pm
The dependency ‘IKVM.Runtime’ could not be found.
The above error occours when i try the code in VB.NET 2003
thanks
sree
February 12, 2007 at 10:36 pm
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
February 26, 2007 at 11:53 pm
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.
March 2, 2007 at 8:04 pm
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…
March 16, 2007 at 11:45 pm
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.
March 17, 2007 at 2:28 am
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.
March 19, 2007 at 10:37 pm
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.
March 22, 2007 at 3:37 pm
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.
March 23, 2007 at 9:57 pm
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.
June 15, 2007 at 4:36 am
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()”
#####################################################
July 27, 2007 at 3:10 am
I have the same error :
The type initializer for ‘org.tigris.subversion.javahl.SVNClient’ threw an exception.
November 30, 2007 at 12:15 am
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.
December 11, 2007 at 2:27 am
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?
January 24, 2008 at 9:51 pm
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?
February 23, 2008 at 1:35 pm
Guys
Any update on the error “The type initializer for ‘org.tigris.subversion.javahl.SVNClient’ threw an exception.” ?
August 19, 2008 at 4:51 pm
{“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?
May 7, 2009 at 3:09 am
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..