Is there an effective tool to convert C# code to Java code?
-
@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. – Darth Continent Jan 21 '11 at 21:46
-
5@Rachel: Alchemy. People have always tried to turn worthless things into gold. ;P – Bobby Apr 11 '11 at 08:52
-
1Yes 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 :S – SSpoke Jun 21 '11 at 19:56
-
2@Darth, isn't that what IKVM is for? – Teo Klestrup Röijezon Jul 04 '11 at 17:33
-
7How come this question was marked as duplicate if the other question asks completely opposite thing? – Episodex Aug 11 '16 at 07:02
14 Answers
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.

- 27,796
- 4
- 47
- 63
-
41
-
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 Aug 03 '09 at 14:20
-
6@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. – ToolmakerSteve Jun 20 '15 at 21:59
-
@ToolmakerSteve 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. – Michael Meadows Aug 20 '18 at 12:32
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.

- 2,328
- 17
- 13
-
4ah, 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 Aug 07 '09 at 09:03
-
3@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 Apr 10 '10 at 12:48
-
3Just to update - CS2J has been released as open source now. (Don't know anything beyond that personally, at least not yet.) – RenniePet Jul 13 '13 at 13:34
-
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? – theGreenCabbage Dec 06 '13 at 17:10
-
2@theGreenCabbage Rustici Software employee here. See http://www.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. – HardlyKnowEm Aug 13 '14 at 15:33
-
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. – Tuntable Jul 30 '19 at 01:20

- 3,088
- 2
- 34
- 41
-
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. – Andez Jun 18 '21 at 07:22
-
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,

- 5,705
- 2
- 45
- 58

- 368
- 5
- 14
-
1I 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 Sep 17 '08 at 02:00
-
7"C# and java are very similar" hahaha, good joke, try generics in java after working with them in C# – Pawel Cioch Feb 21 '15 at 18:31
-
2The early versions of C# were almost identical to Java, but now it seems to have moved further away. – ack Jul 18 '17 at 17:50
These guys seem to have a solution for this, but I haven't tried yet. They also have a demo version of the converter.

- 61
- 1
- 1
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

- 5,864
- 1
- 40
- 64
Try to look at Net2Java It seems to me the best option for automatic (or semi-automatic at least) conversion from C# to Java
They don't convert directly, but it allows for interoperability between .NET and J2EE.

- 12,657
- 7
- 36
- 36
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 Sep 17 '08 at 05:26
-
2"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. – Rokey Ge Jul 21 '11 at 22:26
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

- 20,583
- 23
- 77
- 112
This is off the cuff, but isn't that what Grasshopper was for?

- 10,386
- 3
- 31
- 37
-
1this 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 Aug 03 '09 at 14:14
-
It does not appear that Grasshopper is still available. If you read the comments on [this article](http://www.theserverside.com/news/thread.tss?thread_id=50450) it appears that Grasshopper was not widely used or feature rich. It also appears that [Mainsoft has changed focus](http://en.wikipedia.org/wiki/Mainsoft) (which might be why I can no longer find it). – Trisped Apr 17 '13 at 18:23
Possibly you could use jni4net - opensource bridge instead ? Or list of other options I know.

- 3,427
- 1
- 30
- 35
-
1For 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](http://zamboch.blogspot.com/search/label/jni4net) since there does not seem to be a page saying what it does. – Trisped Apr 17 '13 at 18:37
-
-
Folks, have a look at [Javonet](https://www.javonet.com/) if you're interested in a **bridge solution.** – AlexMelw May 14 '19 at 08:29
Why not write it in Haxe (http://haxe.org/) and convert it to whatever you want it to be?
-
4
-
Well, this changes the question into how to convert C# to Haxe, and then it should be convertible to Java. – Konard Aug 12 '19 at 18:05