Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing C# for the JVM

Tags:

java

c#

jvm

clr

Is anyone attempting to implement C# for the JVM? As a Java developer, I've been eyeing C# with envy, but am unwilling to give up the portability and maturity of the JVM, not to mention the diverse range of tools for it.

I know there are some important differences between the JVM and CLR but is there anything that is a showstopper?

like image 525
Rob Avatar asked Mar 25 '09 17:03

Rob


People also ask

What is a implementation C?

A standard conforming C implementation consists of a compiler that translates compilation units as mandated by the standard, an implementation of the standard library for all functions required by the standard and something (normally a linker) that puts everything together to build an executable file.

How do you implement a class in C?

The basic idea of implementing a class in C is to group the data for a C object into structs so that you can have any number of objects. The struct is declared in the . h file so that it is shared between the class and its clients. In addition, you usually need a function that initializes the objects in the class.

When was C originally implemented?

C came into being in the years 1969-1973, in parallel with the early development of the Unix operating system; the most creative period occurred during 1972.


7 Answers

There are very significant differences between the CLR and the JVM.

A few examples:

  • Java doesn't have user-defined value types
  • Java generics is completely different to .NET generics
  • Many aspects of C# depend on elements of the framework - delegates etc. You'd need to port the library as well, even for language aspects.
  • Java doesn't support things like properties and events at a JVM level. You could fake some of this, but it wouldn't be the same.
  • I don't believe Java has any equivalent to pass-by-reference parameters, even at the JVM level
  • Subtleties to do with the different memory models would quite possibly bite, although I'm not sure how much is in the C# spec.
  • Unsafe code in general probably isn't possible in Java
  • Interoperability with native code is very different between JNI and P/Invoke. This probably isn't much of a problem for you.
  • You'd have to fake operator overloading and user-defined conversions

You could probably port a lot of C# - but you'd be left with a pretty unsatisfactory experience, IMO.

Going the other way, are you aware of IKVM? It allows you to run Java code in .NET.

like image 82
Jon Skeet Avatar answered Oct 09 '22 04:10

Jon Skeet


Visit http://code.google.com/p/stab-language

The code below if a Stab language code for JVM

using java.lang;
using stab.query;
public class Test {
   public static void main(String[] args) {
   // Sorts the arguments starting with "-" by length and then using the default   
        // string comparison
        var query = from s in Query.asIterable(args)
                    where s.startsWith("-")
                    orderby s.length(), s
                    select s;
        foreach (var s in query) {
            System.out.println(s);
        }
    }
}
like image 20
Vns Avatar answered Oct 09 '22 03:10

Vns


Bytecode transpilers

Grasshopper can take a CLR bytecode and transpile it for JVM. Intended primarily for web apps, it does not provide e.g. JVM implementation of Windows Forms classes. Seems somewhat dated, though. The web talks about ASP.NET 2.0, Visual Studio 2008 and so on. First mentioned by @alex

XMLVM can take CLR or JVM bytecode as input and produce either as output. Additionally it can output Javascript or Objective-C. No releases yet, only Subversion. "Experimental development version that is not to be used in a production environment."

IKVM goes in the other direction than OP wants. It provides a JVM implementation running on CLR, a JVM to CLR bytecode transpiler and a CLR library method stub generator for Java. http://www.ikvm.net/uses.html Mentioned by @Jon Skeet

RPC

Why not have CLR and JVM running alongside and make the communication as much frictionless as possible? This is not what the OP wants, but some other answers are already quite off topic in different ways, so let's cover it.

RabbitMQ, has a free option, it is a RPC server written in Erlang with API libraries for C#, Java and more.

jnBridge, the licence may be too expensive for some prospective users.

gRPC, and similar modern RPC libraries offer wide language support, code generation for client libraries in these languages, language independent wire format for data, advanced features like cascading call-cancellation and so on.

Programming languages

Write once, run everywhere ;)

Haxe, compiles to C#/CLR, Java/JVM, Javascript, Flash, Python, … Provides interop mechanisms for each of the target languages. Can be thought about as an ActionScript3 successor to some degree. Seems pretty solid stuff, with at least one company actually depending on it. Much more trustworthy than Stab, mentioned next.

Stab brings some C# features and Java interoperability. Not very useful, you get some C# features, but what you interact with is Java code which does not use them. https://softwareengineering.stackexchange.com/a/132080/45826 The language is relatively obscure, possibly abandoned, with little promise to become better. First mentioned here by @Vns.

Gust of fresh air for the JVM platform ;)

Scala, Kotlin, others, are fairly nice languages running on top of JVM which bring features that a C# programmer may miss in Java. Especially Kotlin feels like a reasonable alternative to C# in the JVM world. Scala may be a bit too large language for a programmer to get comfortable with in a short time.

Mono

That is certainly an option too. Why transpile to JVM if Mono can run it as it is. First mentioned by @ferhrosa

NEW YORK — Nov. 12, 2014 — On Wednesday, Microsoft Corp. reinforced its commitment to cross-platform developer experiences by open sourcing the full server-side .NET stack and expanding .NET to run on the Linux and Mac OS platforms.

According to this press release from which the quote comes, Visual Studio 2015 will add Linux/Mono as a supported platform.

This is a blog written by the Mono project people about it, from the other side: .NET Source Code Integration (November 2014).

.NET Core

A Windows/Linux multiplatform version of (some of) .Net governed by Microsoft. 'nuff said https://github.com/dotnet/core.

Conclusion

It would be now necessary to give these tools/frameworks a try and see how much friction there is. The OP wants to write in C# for the JVM, which may actually work quite well using Grasshopper.

Doing this with the goal to mix C# and Java world libraries in a single codebase may not work so well.

Sources

http://blog.pluralsight.com/new-course-making-java-and-c-work-together-jvm-and-net-clr-interop

like image 31
7 revs Avatar answered Oct 09 '22 04:10

7 revs


It might be simpler to write a converter from IL to bytecode. That way you'd automatically get support for any .NET language on the JVM.

However, this is such an obvious idea that if this hasn't already been done, it's probably extremely hard, or hard to do well/usefully.

like image 31
Daniel Earwicker Avatar answered Oct 09 '22 03:10

Daniel Earwicker


Look at Grasshopper. It is a Visual Studio-based SDK and patented .NET to Java converter that enables you to run .NET Web and server applications on Linux® and other Java-enabled platforms.

like image 25
alex Avatar answered Oct 09 '22 04:10

alex


An option for cross-platform development in C# could be mono: http://www.mono-project.com/

like image 28
ferhrosa Avatar answered Oct 09 '22 02:10

ferhrosa


You can use a source-to-source compiler to translate C# into a language than runs on the JVM. For example, there are several C# to Java converters that would allow C# applications to run on the JVM after being translated into Java.

like image 44
Anderson Green Avatar answered Oct 09 '22 02:10

Anderson Green