18

I have a Java project which I'd like to convert to C#, and after looking here and elsewhere found out about a tool, Sharpen.

However, in order to get it up and running (I'm new to Java) the site linked above describes grabbing the source and building it as a plugin to the Eclipse IDE, which I'd rather not do since I don't ordinarily do Java.

Does anyone know whether a standalone EXE exists which can simply utilize Sharpen to take a given Java project and run it through the wringer to convert it to C#? Or are Java and C# similar enough that it should be pretty straightforward to just port the code over to .NET manually?

Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
Darth Continent
  • 2,319
  • 3
  • 25
  • 41

3 Answers3

31

I would personally do it manually. You can reflect on where the Java design choices simply aren't appropriate for .NET, and end up with idiomatic C# code instead of code which looks very much like C# with a Java accent.

It also means you're more likely to understand the code at the end :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    As OP sounds uncomfortable with Java coding, will he run into problems doing it manually? Anything specific to watch out for? I'll probably be in the same boat at some point, so I'm interested. – Justin Morgan - On strike Jan 21 '11 at 22:14
  • 3
    @Justin: Probably not -- if he were going the other way, then *heck yeah* he could run into trouble. But going from Java to C# is pretty straightforward, and the language itself can probably get converted with very little modification. (An exception is if you make extensive use of a few newer features like using lots of anonymous classes or like using generics like `Class extends MyClass>`, in which case yeah, you might need to do more than just text replacement. But overall, it's very straightforward... probably even easier than going from VB.NET to C#.) – user541686 Jan 21 '11 at 22:15
  • I have to disagree with this popular answer. I strongly doubt that in most cases understanding the code is the end goal here nor is having it be extremely efficient. The goal is most likely some business outcome that is unconcerned with both those targets. Ultimately we as an industry need to do more to satisfy business need without getting constantly side tracked into interesting ideas. – Schalk Dormehl Sep 27 '18 at 14:20
  • 2
    @SchalkDormehl: If the goal is to never touch the software again, that's one thing. But maintenance is *hugely* important in most software projects. Trying to maintain "Java written in C#" for years is likely to be far more expensive than porting it idiomatically to start with, IMO. Of course there are some situations where you really do just want to "port and then kill" but I'd say that's less often the case than "port, maintain and extend". Being able to understand the code easily is *critical* to that. – Jon Skeet Sep 27 '18 at 14:35
3

There's no one-to-one conversion (or even close), so you should really do it manually. However, if you need a decent, automated piece of software, either as a starting point or as a rough converter, see this link.

user541686
  • 205,094
  • 128
  • 528
  • 886
2

If the project is so big, I would say use a tool but then review it yourself and change the stupid things in the code.

Peyman
  • 3,059
  • 1
  • 33
  • 68