tags:

views:

82

answers:

3

I have wondered for a while about the feasibility of having Java run on the CLR.

After seeing a previous question here, I realize that there are quite a few differences between the Sun Java platform and the .NET runtime that would make cross-compiling impossible in all but the most trivial cases.

That being said, isn't IL a Turing-complete language? Couldn't you write a JVM in IL? Of course, the answer is yes, but why even go that far?

My question is:

Is the CLR (as a platform) incompatible with Java as a language (not a platform)?

How much of Java would have to be contorted or broken in order to make it fit?


Surely, this could be compiled for the CLR + .NET:

import System.*;

public class HelloWorldExample
{
    public static void main(String args[])
    {
        Console.WriteLine("Hello World !");
    }
}

Clarification:

What I'm getting at, is that I want to know what language features of java are incompatible with their CLR counterparts.

For example, I think that Generics are somehow in-congruent. I believe that it is as similar story with exceptions.

A: 

A quick search reveals IKVM and the in-progress project Ja.NET.

That said, I could see value in going the other way.

Andy Thomas-Cramer
Those are JVMs/Class libraries, rather than java compilers. Right? Or am I confusing myself?
John Gietzen
A: 

Yes, but given that Java is much more widely used and more standard... wouldn't you want it the other way around, i.e. compile C# to Java bytecode? The answer of course is that it is possible to write a Java compiler that will emit bytecode for CLR or a C# compiler that will emit Java bytecode. The trickiness, though, is that you would need to provide a custom implementation of the standard library / libraries that would defer to the library or libraries for that platform. And given the size of both APIs that would be a fairly significant undertaking. This would probably require a fairly large team to get it done in a reasonable amount of time. I would suggest starting an opensource project if you intend to do that, although given that C#/CLR is a Microsoft-ism, I'm not sure how much enthusiam can be generated.

Michael Aaron Safyan
What's with the down vote? Someone angry that C#/CLR is proprietary?
Michael Aaron Safyan
It's so proprietary that it's open source. http://www.mono-project.com/Main_Page
TrueWill
I would say that is hardly the case given the current state of the mono project and the fact the oracle is suing google over java.
Wesley Wiser
My guess is that the downvotes are because you made fun of the question instead of answering it.
Gabe
@Gabe, I did answer the question. There is no reason why it would not be possible to create a C# to JVM or Java to CLR compiler.
Michael Aaron Safyan
The OP asked "Is the CLR incompatible with Java (as a language)?" and you answered "Yes" (the correct answer is "No") and then wrote 150 words explaining why he wouldn't want to even though he already said "I realize that there are quite a few differences ... that would make cross-compiling impossible in all but the most trivial cases."
Gabe
@Gabe, oh, I see the cause of confusion... I answered his last question, which was "Couldn't you write a JVM in it?"
Michael Aaron Safyan
Michael: I think most people took "Couldn't you write a JVM in it?" as a rhetorical question because he followed it up with "Of course, the answer is yes".
Gabe
+2  A: 

The CLR was designed with the assumption that people would have to be able to transition to .Net from Java by being able to compile their Java source code largely unmodified. Thus, things in Java you might consider to be problematic in a runtime (like covariant arrays) are included as features in the CLR.

The J# project implements enough of the Java standard library that most Java apps will compile with minimal changes. Note that J# can load JVM bytecodes for compatibility, but J# source always compiles to CLR bytecodes.

Gabe