views:

108

answers:

4

Dotnet CLR converts the language code to MSIL. While Java Code be converted to Native Code that is platform independent. Can we convert this MSIL to Native Code ? So the Dotnet will automatically become platform independent.

A: 

Try reading this discussion: http://stackoverflow.com/questions/78811/is-there-an-effective-tool-to-convert-c-to-java

Kurt Du Bois
I think you misread his question...I think he means either how to convert MSIL to native bytecode, or to native java bytecode...I'm not sure which one.
Bobby
+7  A: 

Be careful about your terminology here:

  • The CLR doesn't convert "language code" to MSIL - it converts MSIL to native code on the fly.
  • Java code isn't converted to "native code that is platform independent"; that's an oxymoron. Java source code is converted into platform independent Java bytecode. The JVM converts bytecode into native code in the same way as the CLR does with MSIL (well, to some extent)

Note that both MSIL and bytecode can be interpreted; HotSpot will sometimes interpret, sometimes JIT compile, and often JIT compile multiple times. The .NET desktop CLR always JIT compiles a single time at the moment.

It's also worth understanding that bytecode is only platform independent to the extent that there is a JVM available for the platform you're interested in. The same is true for MSIL. You can compile a C# program on Windows and then run it on Linux under Mono if there's a build of Mono for your platform and if it doesn't use any libraries which aren't available on Mono.

Now as for converting MSIL to Java bytecode... the reverse is more feasible, as MSIL is generally more powerful than Java bytecode. For example, custom value types and generics in MSIL wouldn't translate easily into bytecode. You'd probably need something closer to a CLR written in Java. I dare say there are some clever possibilities, but there are fundamental difficulties there. Even the reverse is unlikely to be easy - especially if Java 7 introduces dynamic invocation to bytecode (whereas dynamic typing in .NET is performed by the DLR just as a library).

Jon Skeet
Additionally, a significant portion of the standard libraries that are part of the Microsoft CLR are tied to Microsoft operating system features. The JVM libraries are significantly more OS independent.
Jim Rush
A: 

I can answer your question only partially.

Yes, MSIL can be converted to native code using ngen.exe. In that case, however, you lose the ability to run efficiently on different target architectures, e.g. 32 bit vs 64 bit. Also, you would need to create multiple versions, one for each target platform.

Apart from that I suggest using Kurt's answer.

John
A: 

This reply may be a bit offtopic, but worth mentioning. MSIL to Java byte code may tough as detailed by others answering the question. But you can handle conversion from one framework to another at a source code level.

If you have a Java library that you would like to use in a .NET application - use IKVM. It compiles Java code into .NET assemblies using IKVMC,a Java bytecode to .NET IL translator. Do check the licensing terms before you jump into using this!

Also I am curious to know if the reverse is possible i.e. .NET source code to Java?

Bharath K