Partager via


Query about ClassLoaders in J#

Recently one of our customer asked us
Q> I’m trying to compile some pure java code using VJ# (VS2005). Mostly it compiles fine, but there are problems with ClassLoaders. In particular, the original code calls getSystemClassLoader(), which doesn’t seem to exist in J#.

I’m a professional programmer but a Java newbie, so any help would be appreciated.

A> Unlike in J++ and Java, custom class loaders and security managers are not supported in J#.

The reason being that the way classloaders probe for classes in Java is different than what is used on .NET. In Java they use the notion of a CLASSPATH, while on .NET we lookup managed assemblies in the GAC, etc. Also there is the issue of how resources are represented in Java and on .NET, etc.
Security semantics too are different in Java and .NET. While J# does provide mappings, such components (security managers) need to be aware of Code Access Secuity semantics and all that.

So basically, J# does not support custom class loaders and security managers. J# uses a default classloader (named VJCClassLoader) that is not programmatically exposed. The method java.lang.Class.getClassLoader always returns null.

Applications using custom class loaders need to be modified to use the .NET Framework classes to now load managed assemblies. Applications can use methods like Assembly.Load or Assembly.LoadFrom to load an assembly and then retrieve a specific type from the loaded assembly. Our product docs on MSDN talk about in more detail. Also, the following link has a sample: https://msdn.microsoft.com/library/en-us/dv_vjsample/html/vjsamClassLoaderSampleModifyCustomClassLoaderForAssemblies.asp?frame=true

Comments

  • Anonymous
    January 20, 2006
    If you want a Java implementation for .NET which is more Java-compatible and actually does support things like classloaders and security managers (I think the latter is work in progress in the underlying class library implementation though) as well as most of JDK1.4 and 1.5 rather than 1.1ish, I suggest IKVM (http://www.ikvm.net/). A much nicer approach, IMHO, to Java/.NET interoperability and completely open source.

    It's so java-compatible that it can actually run Eclipse, which as I understand it is one of the more demanding applications out there in use of classloaders because of its plugin architecture.

    I'm using it in production and it works great.

    Just a thought.