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?

Lee
  • 1,576
  • 2
  • 15
  • 21
  • See here for suggestions on automated Java-to-C# conversion: http://stackoverflow.com/questions/443010/where-can-i-find-a-java-to-c-sharp-converter – Anderson Green Nov 06 '12 at 18:02

5 Answers5

3

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
  • 172,459
  • 74
  • 246
  • 311
  • Thanks for the link - it's great reference material for what I'm doing. – Lee Oct 05 '08 at 13:36
  • 1
    "change all getter and setter methods into properties" - better think again on that one. It would be more correct to say, change to properties [as specified in the MSDN guidelines](https://msdn.microsoft.com/en-us/library/ms229054(v=vs.100).aspx). I just had to revert a bunch of "properties" back to methods that were 1) set with no get 2) non-deterministic 3) returned built arrays...that someone else did. Blindly converting to properties without considering that properties are supposed to act like fields is not a good strategy. If in doubt, keep it a method. – NightOwl888 Feb 04 '17 at 04:10
3

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.

Alf
  • 1,285
  • 3
  • 14
  • 24
Judah Gabriel Himango
  • 58,906
  • 38
  • 158
  • 212
  • 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 Mar 31 '09 at 20:14
1

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
  • 16,354
  • 20
  • 105
  • 148
  • 2
    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. – redcalx Jul 09 '09 at 13:54
0

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.
Brian Kelly
  • 5,564
  • 4
  • 27
  • 31
-1

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
  • 756
  • 2
  • 11
  • 30