Lee
Lee

Reputation: 1596

How should I convert Java code to C# code?

I'm porting a Java library to C#. I'm using Visual Studio 2008, so I don't have the discontinued Microsoft Java Language Conversion Assistant program (JLCA).

My approach is to create a new solution with a similar project structure to the Java library, and to then copy the java code into a c# file and convert it to valid c# line-by-line. Considering that I find Java easy to read, the subtle differences in the two languages have surprised me.

Some things are easy to port (namespaces, inheritance etc.) but some things have been unexpectedly different, such as visibility of private members in nested classes, overriding virtual methods and the behaviour of built-in types. I don't fully understand these things and I'm sure there are lots of other differences I haven't seen yet.

I've got a long way to go on this project. What rules-of-thumb I can apply during this conversion to manage the language differences correctly?

Upvotes: 6

Views: 11985

Answers (5)

Qwertie
Qwertie

Reputation: 17216

One more quick-and-dirty idea: you could use IKVM to convert the Java jar to a .NET assembly, then use Reflector ILSpy's "Save Code" command to disassemble it into a Visual C# project.

(By the way, I haven't actually used IKVM--anyone care to vouch that this process would work?)

Upvotes: 1

Judah Gabriel Himango
Judah Gabriel Himango

Reputation: 60061

Couple other options worth noting:

  • J# is Microsoft's Java language implementation on .NET. You can access Java libraries (up to version 1.4*, anyways). *actually Java 1.1.4 for java.io/lang, and 1.2 for java.util + keep in mind that J# end of life is ~ 2015-2017 for J# 2.0 redist

  • Mono's IKVM also runs Java on the CLR, with access to other .NET programs.

  • Microsoft Visual Studio 2005 comes with a "Java language conversion assistant" that converts Java programs to C# programs automatically for you.

Upvotes: 3

Brian Kelly
Brian Kelly

Reputation: 5664

If you have a small amount of code then a line by line conversion is probably the most efficient.

If you have a large amount of code I would consider:

  1. Looking for a product that does the conversation for you.
  2. Writing a script (Ruby or Perl might be a good candidate) to do the conversion for you - at least the monotonous stuff! It could be a simple search/replace for keyword differences and renaming of files. Gives you more time/fingers to concentrate on the harder stuff.

Upvotes: 0

Mil
Mil

Reputation: 756

I'm not sure if it is really the best way to convert the code line by line especially if the obstacles become overwhelming. Of course the Java code gives you a guideline and the basic structure but I think at the end the most important thing is that the library does provide the same functionality like it does in Java.

Upvotes: -1

FlySwat
FlySwat

Reputation: 175733

Your doing it in the only sane way you can...the biggest help will be this document from Dare Obasanjo that lists the differences between the two languages:

http://www.25hoursaday.com/CsharpVsJava.html

BTW, change all getter and setter methods into properties...No need to have the C# library function just the same as the java library unless you are going for perfect interface compatibility.

Upvotes: 3

Related Questions