Skip to main content

Why doesn"t Sun do a C# to Java byte code compiler?



We Want to Run Our C# Code on the JVM





My company has a large C# code base. Well over half of this code is our core engine for creating, reading, modifying, calculating and writing Excel workbooks. We frequently get questions from customers and potential customers asking whether we are going to build a Java version of our engine - many of them are not at all interested in the UI. We even have a few customers who have taken the trouble to use our .NET library from their Java applications.





So, we would like to build a Java version of our core engine, ideally without maintaining a separate Java source code base.





Eric Sink described this problem very well. I am in a similar position except for the fact that our software license includes royalty free deployment, making Eric's choice of Mainsoft a nonstarter for us.





I have been Googling the likes of "c# to jvm" every few months for several years now with no joy. Having spent ~7 years developing similar software for Java, I am confident that the .NET APIs we use in our core engine could easily be encapsulated and we could accomplish everything we need using the Java libraries. So, if we only had a C# -> JVM compiler we could build our core engine for Java and we would no longer have to turn away Java developers who would like to use it.





I am not asking for the technical reasons why Sun does not do a C# compiler. I recognize that Java has no properties or an unsigned 64 bit long, etc... For the sake of argument, just assume that all of these technical issues could be dealt with by extending the JVM and / or other means.





And I am not asking for yet another debate on why one language / stack might be better than the other. The reality in our business is that there are plenty of potential customers using each.





Why Should Sun do a C# Compiler? (IMO of course)





Making it easier to run C# code on the Java platform means more developers and more software for the platform. Is there anything more important to the success of a platform? Jonathan Schwartz is a software guy. I will leave it to others smarter than me to decide whether or not he took on an impossible job as President and CEO of Sun, but having met with Jonathan shortly after he joined Sun my impression is that he understands software and the need for a large base of developers.





So Why Doesn't Sun do a C# Compiler?







  1. NIH syndrome?



  2. The ghost of Scott McNealy ?



  3. Too many Java developers dislike or distrust anything related to Microsoft?



  4. They agreed not to as part of taking the big bucks ?



  5. ???







There must be a good reason. I just cannot for the life of me figure out what it is...


Comments

  1. Firstly Sun have zero incentive to implement a C# compiler on the JVM because they have something very similar called the Java programming language.

    Its also not really as simple as just implementing a compiler as the Java standard class libraries are not the same as the .net Base Class Libraries. You would end up having to change all the .NET API calls to Java API calls.

    Micrsoft had a product called J# which was meant to be for Java to .NET conversion but in the end no one used it as the API was limited to pre Java 2 API so it was mostly useless. It would be the same if Sun implemented parts of the .NET BCL, as only the core portons of it are standardised and royalty free. Parts like ASP.NET and WPF, WCF etc are not part of the ECMA standards and so Sun would need Microsofts permission to implement those API's.

    If enough customers want a java version to make business sense to port your application to java then do it, you just wont ever get any help from Sun via a C# to JVM compiler.

    ReplyDelete
  2. Why doesn't Microsoft do a C# to Java byte code compiler? Why don't you do it? There are open specs on each side...

    ReplyDelete
  3. Joe Erickson wrote:

    Making it easier to run C# code on the
    Java platform means more developers
    and more software for the platform.


    This is an untrue statement. Running C# code on the JVM does not create Java programmers, it creates C# programmers who can execute on a JVM. It only expands the reach of C#, assuming the JVM also translates any microsoft specific calls (i.e. win32) into something that is platform neutral. So if Sun translates IL to Java Bytecode, the only group it helps is: Microsoft. And, given Sun's history with Microsoft during the original C#-Java schism/Visual J++ lawsuits...

    Plus, you have to face the technical infeasibility, whether you want to or not. There are fundamental differences in the way the bytecodes are executed that are far more important issues than whether or not there's an unsigned long datatype.

    If you must have C# on a non-Microsoft platform, use Mono

    ReplyDelete
  4. "So, we would like to build a Java version of our core engine, ideally without maintaining a separate Java source code base."

    Basically, you want to compile your C# code unmodified, and have it run in a Java-only environment.

    IKVM is not what you want. IKVM is three main things.

    a. ikvm - CLI implementation of a Java virtual machine (note that this uses Classpath (now OpenJDK) for the Java class library).

    b. ikvmc - Compiles java bytecode to CLI bytecode.

    c. ikvmstub - Generates java stub classes that call CLI code.

    Note that all of these tools depend on CLI at runtime. What you want is exactly the opposite of IKVM, which is of course MVKI (Most Venerable Kompiler Intermediary) :):

    a. mvki - Java implementation of a CLI virtual machine (presumably this would use Mono or DotGNU for the class library).

    b. mvkic - Compiles CLI bytecode to Java bytecode.

    c. mvkistub - Generates CLI stub classes that call Java

    Note that none of these would require an existing implementation of the .NET Framework at runtime, so they should be satisfactory to your Java-only customers.

    Unfortunately, as far as I know MVKI does not exist, so you're best off doing a manual port (which would inevitably be cleaner, albeit more work).

    Edit: Based on the description of Mainsoft, it appears to be similar to MVKI, though I'm not sure what they do for the class library, and unlike IKVM it's not FOSS.

    ReplyDelete
  5. Expose your .NET API as ASMX web services and you should be good to go.

    EDIT: For more heavy-usage scenarios, it would be worth looking into Windows Communication Foundation (WCF). This has built-in, configurable support for security, streaming, different transport scenarios (HTTP, TCP/IP, local named pipes). You are not restricted to SOAP message encoding, but that would probably be the easiest way to interop with Java.

    I'm not too sure about your exact scenario, but if you're dealing with large files and the .NET code and Java code are both running locally, you can just save the file to the user's hard drive using .NET and then fetch it from your Java app.

    ReplyDelete
  6. There must be a good reason. I just cannot for the life of me figure out what it is...


    It is easy to believe that companies are non-profit organisations which have your interests at heart. It is easy to forget that the only purpose for a listed company is to make money for it share holders. This is a legal obligation and directors/executives have been sacked/sued if share holders don't believe they have failed to do this.

    Sun makes Java free because it helps sell their hardware which how it makes money from Java. IBM makes Java free because it helps them make more money on their consulting.

    If Sun were to spend money on a C# converter how would it make that money back and make a profit. Imagine you have to convince Sun's share holders. If you can, Sun will make a C# converter.

    ReplyDelete
  7. I wonder whether the Mono project could have made less work for themselves by targeting the JVM instead of developing and maintaining their own virtual machine from scratch. Development effort could have focused on a C# cross-compiler and porting clean-room implementations of the .NET libraries to the JVM. Sort of like an open-source version of what Mainsoft has done. You could then enable inter-language calls between Java and C# code in the same JVM, and deploy Applets and Java Web Start applications written in C#.

    ReplyDelete
  8. Have fun.


    Must break checked exceptions.
    Must find a way to implement delegates (which are like single-method interfaces added no earlier than load time).

    ReplyDelete
  9. You can run your .NET code and Java code in the same interpreter! See the IKVM .NET-based JVM, and the Boo and Java wiki page for an example use case (using the .NET-based Boo language to write applications using Java libraries).

    ReplyDelete
  10. Joe, I suggest you investigate IKVM. You might find something there that scratches your itch

    ReplyDelete
  11. If i was doing something like cross platform cross language support, I would create a 'common api' since the languages are similar in syntax you could make a translator easy enough. Then instead of calling java or .net apis directly from the core, you would call your 'common api' which would re implement the java and .net apis you would need. In this way you could create a cross language sandbox if you will. Since the main differences in java and c# are object definitions, I would get those by reflecting the C# dlls, and then reconstruct the constructs, then it would be easy to have an interpreter run through and implement the function bodies and convert properties to getters setters already knowing the structure of the files. This of course is assuming .net 2.0, some of the features in 3.0 and 3.5 become very difficult to 'interpret'

    It would be complex, but probably not as complex as reconstructing a core in java by hand, and having to have 2 teams working on them seperatly. If this idea sparks some inspiration i might create one. I would really rather see a simpler stable mono installation for mac.

    Basically I think a code level interpreter based on a set of common api classes is something very possible to write with a team in a week or two.

    ReplyDelete
  12. Simple. Because Sun would rather not be sued by Microsoft. And while we as programmers might see no viable reason for a suit, keep in mind that Microsoft is quite a bigger company than Sun and has the power to boss them around.

    Sun fortunately is in the position of being quite more open than Microsoft however. If such a demand exists, either open source or a third-party could make this compiler for Java and Sun wouldn't have to take the heat.

    ReplyDelete
  13. JACIL is an apparently dead project that attempts to do the reverse of IKVM. Perhaps some motivated folks could use it as a starting point for a viable .Net to JVM compiler.

    ReplyDelete
  14. I guess the better question is why don't you write a C# to Java byte code compiler, if you want one to exist. Waiting for corporate overlords to do something is a bad idea.

    A suggestion for creating such an implementation: take Mono's or .GNU's C# front end. Don't bother writing your own.

    ReplyDelete
  15. I made a comment that should really have been an answer:

    It is just not technically possible at this point to implement the C# spec on the JVM. C# supports pointers, unsigned types, user defined value types, true generics, passing by reference, and loads of other things. For a very C#-like JVM language, check out Stab.

    Mainsoft fakes it. I am sure it takes enormous effort on their part.

    ReplyDelete
  16. I know this might not be what your looking for, but here are some possible alternatives that (if not to you) could be useful to others.


    C: You could port as much as possible to C. Now you can make a wrapper in C# and Java (and any other language that can communicate with C) that makes it feel native to their language while programming. The problem now is that you (or them, depending your license) has to build the C part for each platform.
    Fantom*: A programming language that according to their home page,
    is:



    Portable: Write code portable to the Java VM, .NET CLR, and JavaScript in the browser.
    Familiar: Syntax Java and C# programmers will feel at home with Fantom's evolutionary syntax.
    Object Oriented: Everything subclasses from Obj. Value types when you need the performance.
    Functional: Functions and closures are baked in.
    Static and Dynamically Typed: Don't like the extremes - take the middle of the road.


    haXe*: I(pronounced hex) is cross platform language that compiles to other languages. Soon C# and Java will be supported. It currently supports:



    Javascript: You can compile a haXe program to a single .js file. You can access the typed browser DOM APIs with autocompletion
    support, and all the dependencies will be resolved at compilation
    time.
    Flash: You can compile a haXe program to a .swf file. haXe is compatible with Flash Players 6 to 10, with either "old" Flash 8 API
    or newest AS3/Flash9+ API. haXe offers very good performance and
    language features to develop Flash content.
    NekoVM: You can compile a haXe program to NekoVM bytecode. This can be used for server-side programming such as dynamic webpages
    (using mod_neko for Apache) and also for command-line or desktop
    applications, since NekoVM can be embedded and extended with some
    other DLL.
    PHP: You can compile a haXe program to .php files. This will enable you to use a high level strictly-typed language such as haXe
    while keeping full compatibility with your existing server platform
    and libraries.
    C++: You can now generate C++ code from your haXe source code, with the required Makefiles. This is very useful for creating native
    applications, for instance in iPhone development.



    Haxe Community. (2011, March 11). haXe Introduction. Retrieved January 29, 2011, from http://haxe.org/doc/intro

    You can learn more about why HaXe is useful here


    *I have never used this language and I don't know how well this would work.

    ReplyDelete
  17. I think you will find that the Mainsoft, Enterprise Edition tool allows you to run most/maybe all your .NET code under the Java JVM... Seems to be focused more on ASP.NET but will allow C#. It has been available for some time, pity they don't publicize it better!

    Warning blurb follows....


    Mainsoft® is Java-.NET
    interoperability software that enables
    IT organizations to move to
    Java-enabled platforms such as Linux
    while preserving existing investments
    in .NET code and skills. The software
    integrates seamlessly into the Visual
    Studio® development environment,
    enabling C# and Visual Basic
    developers to rapidly develop and
    maintain server and Web applications
    that run on Windows, Java EE platforms
    or both, thereby reducing application
    development and maintenance costs,
    time-to-production and total cost of
    ownership.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex