Is there an effective tool to convert C# code to Java code? [duplicate]
Asked Answered
Y

14

94

Is there an effective tool to convert C# code to Java code?

Yvonneyvonner answered 17/9, 2008 at 1:4 Comment(5)
@Rachel: I'm new to Java but know C# pretty well, and I stumbled upon an open-source Java project on SourceForce that appears to be dead, whose functionality I'd like to transform into a new C# project. I'd like to have such a tool to have it do some of the heavy lifting and convert at least the basic methods and structures, after which I can see the result and use my brain to translate whatever's left into something I can use within my .NET comfort zone.Grapeshot
@Rachel: Alchemy. People have always tried to turn worthless things into gold. ;PCarollcarolle
Yes most of the time abandoned projects can be extremely valuable.. last abandoned project I found just lurking PSC Code made me 7200$ usd completely automated how i like it. Until it became obsolete again :SJanitajanith
@Darth, isn't that what IKVM is for?Gloomy
How come this question was marked as duplicate if the other question asks completely opposite thing?Marche
A
50

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.

Alguire answered 17/9, 2008 at 1:30 Comment(4)
In other words, the most effective "tool" is a programmer. :-)Niu
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.Sydel
@MichaelMeadows: do you speak from personal experience? In the opposite direction, Java->C# (which is a much easier direction for conversion), I personally saved a LOT of time and hassle using Tangible Software's converter, to convert 50K lines. It did EXACTLY what I expected from it: it handled all the annoying menial details that I would have had to do myself if I was manually re-typing all that code in the new language. Freeing me to focus on the places where actual thought was needed. Lots of small changes still needed by hand, but avoided careless errors that were likely if retyped.Delusion
@Delusion Yes, in fact I do. I'd say this. Most of the effort that goes in to an application isn't typing. It's problem solving. How those problems are solved, the core of the solution, differs dramatically based on the language and frameworks employed. Going from Java to C# may be easier (because the .Net stack tends to be a little more standardized), but you still end up with code that is difficult to reason about, doesn't take advantage of the target language, and ultimately creates a much higher ramp-up and maintenance drag on productivity.Alguire
B
31

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.

Bellarmine answered 3/8, 2009 at 14:0 Comment(6)
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.Vogue
@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.Radicel
Just to update - CS2J has been released as open source now. (Don't know anything beyond that personally, at least not yet.)Mottled
I would assume that your tool targets a very specific set of libraries (which would make things easier to convert from X to Y and back), but may not work on broader libraries, or specific Java and/or C# libraries that are independent from each other (what comes to mind is LINQ). What do you think, Mike?Misappropriate
@Misappropriate Rustici Software employee here. See cs2j.com/features. The tool does target a limited subset of the .NET system libraries (and LINQ in particular is not translatable), probably based on what we needed at the time. But the translations of individual classes are handled by XML files, and it is easy enough to add a new XML file for any class you need.Ubangishari
We are attempting a similar thing, but the other way, Java to C#. And it must be repeatable. With a language specific layer, the bulk of the logic should move across OK. Looking at tools now.Shine
J
10

This blog post suggests useful results from Tangible.

Jamajamaal answered 10/4, 2010 at 11:57 Comment(2)
These are good - there is free and paid version here in 2021. Not everything can be converted accurate 100%. I do like the way events are converted. Big +1 to Tangible.Simons
@Simons Thanks for the update. Good to know they're still going!Jamajamaal
P
8

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,

Pennsylvanian answered 17/9, 2008 at 1:27 Comment(3)
I used Microsoft's JLCA and it really did work: weblogs.asp.net/jgalloway/archive/2007/01/24/…Underwood
"C# and java are very similar" hahaha, good joke, try generics in java after working with them in C#Telecast
The early versions of C# were almost identical to Java, but now it seems to have moved further away.Countrywide
I
6

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

Indigestion answered 29/6, 2010 at 7:24 Comment(0)
T
5

Although this is an old-ish question, take a look at xmlVM http://www.xmlvm.org/clr2jvm, I'm not sure if it's mature enough yet, although it has been around for several years now. XMLvm was made, I believe, primarily for translating Android Java apps to the iPhone, however, its XML-code-translation-based framework is flexible enough to do other combinations (see the diagrams on the site).

As for a reason to do this conversion, maybe there is a need to 'hijack' some of the highly abundant oss code out there and use it within his/their own [Java] project.

Cheers

Rich

Timisoara answered 13/1, 2011 at 16:47 Comment(0)
P
4

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

Pender answered 12/1, 2009 at 16:39 Comment(0)
C
3

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

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

Chagres answered 17/9, 2008 at 1:13 Comment(1)
The link is old and only points to SharePoint products now.Eskimoaleut
U
3

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)

Utopian answered 17/9, 2008 at 1:57 Comment(2)
linq comes to my mind. how translate code that uses linq to java?Niu
"Java doesn't have a way to make methods non virtual." - Java has 'final' keyword, equivalent to 'seal' in c sharp, when placed before a method.Antrum
P
1

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

Pawpaw answered 13/11, 2008 at 14:45 Comment(0)
T
1

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

Tharpe answered 13/11, 2008 at 14:48 Comment(2)
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.Sydel
It does not appear that Grasshopper is still available. If you read the comments on this article it appears that Grasshopper was not widely used or feature rich. It also appears that Mainsoft has changed focus (which might be why I can no longer find it).Eskimoaleut
Y
1

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

Yea answered 3/8, 2009 at 14:4 Comment(0)
L
1

Possibly you could use jni4net - opensource bridge instead ? Or list of other options I know.

Lapith answered 13/12, 2010 at 23:57 Comment(3)
For those who are wondering, jni4net is a bridge between the JVM and CLR. It allows your .Net code (like VB or C#) running on the CLR to also execute code in the JVM and Java code running on the JVM to execute cod in the CLR. For more information read the 2009/10/10, 2009/10/31, and 2009/11/03 posts on the blog since there does not seem to be a page saying what it does.Eskimoaleut
jni4net.sourceforge.net is a little betterGlaucous
Folks, have a look at Javonet if you're interested in a bridge solution.Rowney
V
-2

Why not write it in Haxe (http://haxe.org/) and convert it to whatever you want it to be?

Vardar answered 27/8, 2015 at 6:59 Comment(2)
Just because he already has code in C#, I presume.Chauvinism
Well, this changes the question into how to convert C# to Haxe, and then it should be convertible to Java.Smote

© 2022 - 2024 — McMap. All rights reserved.