tags:

views:

8511

answers:

13

is there an effective tool to convert c# to java?

A: 

Such a tool doesn't apparently does exist, but it wouldn't be much use outside of anything but the most trivial of cases.

The libraries in C# and Java are very different and you can't write a tool that will know which library to use and how to call it differently.

William
+3  A: 

They don't convert directly, but it allows for interoperability between .NET and J2EE.

http://www.mainsoft.com/products/index.aspx

scubabbl
+3  A: 

There is a tool from Microsoft to convert java to C#. For the opposite direction take a look here and here. If this doesn't work out, it should not take too long to convert the source manually because C# and java are very similar,

Roger Ween
I used Microsoft's JLCA and it really did work: http://weblogs.asp.net/jgalloway/archive/2007/01/24/identicons-ported-from-java-servlet-to-httphandler.aspx
Jon Galloway
+12  A: 

I have never encountered a C#->Java conversion tool. The syntax would be easy enough, but the frameworks are dramatically different. Even if there were a tool, I would strongly advise against it. I have worked on several "migration" projects, and can't say emphatically enough that while conversion seems like a good choice, conversion projects always always always turn in to money pits. It's not a shortcut, what you end up with is code that is not readable, and doesn't take advantage of the target language. speaking from personal experience, assume that a rewrite is the cheaper option.

Michael Meadows
In other words, the most effective "tool" is a programmer. :-)
marcospereira
"The syntax would be easy enough" so long as you don't use lambdas, linq, events (doable but hard to get the semantics of multicasting excatly right), extension methods, any introspection, any generics relying on new() or default(T) (type erasure is a bitch), any iterator blocks, unsigned variables, operator overloads... I could go on but really. a java app, translated into c#, then back again would be easy enough I suppose. But that's a very restricted subset of c#
ShuggyCoUk
That said you still get a plus one because the rewrite will almost certainly be cheaper, produce a better product and a better understanding for the programmers that did it.
ShuggyCoUk
+1  A: 

C# has a few more features than Java. Take delegates for example: Many very simple C# applications use delegates, while the Java folks figures that the observer pattern was sufficient. So, in order for a tool to convert a C# application which uses delegates it would have to translate the structure from using delegates to an implementation of the observer pattern. Another problem is the fact that C# methods are not virtual by default while Java methods are. Additionally, Java doesn't have a way to make methods non virtual. This creates another problem: an application in C# could leverage non virtual method behavior through polymorphism in a way the does not translate directly to Java. If you look around you will probably find that there are lots of tools to convert Java to C# since it is a simpler language (please don't flame me I didn't say worse I said simpler); however, you will find very few if any decent tools that convert C# to Java.

I would recommend changing your approach to converting from Java to C# as it will create fewer headaches in the long run. Db4Objects recently released their internal tool which they use to convert Db4o into C# to the public. It is called Sharpen. If you register with their site you can view this link with instructions on how to use Sharpen: http://developer.db4o.com/Resources/view.aspx/Reference/Sharpen/How_To_Setup_Sharpen

(I've been registered with them for a while and they're good about not spamming)

linq comes to my mind. how translate code that uses linq to java?
marcospereira
A: 

I'm not sure what you are trying to do by wishing to convert C# to java, but if it is .net interoperability that you need, you might want to check out Mono

m_oLogin
+1  A: 

This is off the cuff, but isn't that what Grasshopper was for?

Ian Patrick Hughes
this converts CIL (MSIL) into java byte code. useful for sure but not directly what the OP asked for. I would say more likely to be actually *useful* for anything other than a one shot conversion but there you go.
ShuggyCoUk
+2  A: 

Try to look at Net2Java It seems to me the best option for automatic (or semi-automatic at least) conversion from C# to Java

+4  A: 

We have an application that we need to maintain in both C# and Java. Since we actively maintain this product, a one-time port wasn't an option. We investigated Net2Java and the Mainsoft tools, but neither met our requirements (Net2Java for lack of robustness and Mainsoft for cost and lack of source code conversion). We created our own tool called CS2J that runs as part of our nightly build script and does a very effective port of our C# code to Java. Right now it is precisely good enough to translate our application, but would have a long way to go before being considered a comprehensive tool. We've licensed the technology to a few parties with similar needs and we're toying with the idea of releasing it publicly, but our core business just keeps us too busy these days.

Mike Rustici
ah, but if you released it as open source, you'd find people who used it (well, some of them) would extend it to suit their needs - which might become your needs in the future. If you make little money from the licences, you might as well do this.
gbjbaanb
@gbjbaanb, the successful open source projects require nurturing and building a community. Dumping out source code without caring for it, means that most open source developers would consider other options first.
Thorbjørn Ravn Andersen
A: 

Well the syntax is almost the same but they rely on different frameworks so the only way to convert is by getting someone who knows both languages and convert the code :) the answer to your question is no there is no "effective" tool to convert c# to java

Yassir
+1  A: 

There is a tool called CSharpJavaMerger. Using it, you can write a set of code that can be used by both C# and Java. It is free and open source.

http://www.kevingao.net/csharp-java-conversion

Sindy
+2  A: 

This blog post suggests useful results from Tangible.

Nick Westgate
+1  A: 

These guys seem to have a solution for this, but I haven't tried yet. They also have a demo version of the converter.

Guven Demir