tags:

views:

1987

answers:

6

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?

+3  A: 

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.

FlySwat
Thanks for the link - it's great reference material for what I'm doing.
Lee
A: 

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.

Mil
A: 

I heard something the other day about a .net version of Java. If such a monster was available, would you just be able to compile the java source into .net intermediate libraries?

Bill K
+1  A: 

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.
spilth
+2  A: 

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).

  • 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.

Judah Himango
Keep in mind that Java 1.4 is something like 10 years old now? For that reason J# should be used only in exceptional cases. It was intended as a path for existing J++ (remember that?) customers to .NET. Not as a general Java-on-.NET solution.
Cheeso
+1  A: 

One more quick-and-dirty idea: you could use IKVM to convert the Java jar to a .NET assembly, then use Reflector--combined with the FileDisassembler Add-in--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?)

Qwertie
The IKVM library can be used but if you attempt to disassemble it in Reflector you will find that some of the MSIL generated by IKVM cannot be dissasembled to C#. I guess C# constructs are a subset of all constructs that are possible with IL, or at least Reflector only handles a subset that is typical/expected.
locster