45

I needed to convert a Java 1.5se app to C# 2.0.

Does anyone know of a tool (preferably free/open source) to do this?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
CSharpDevLondon
  • 919
  • 2
  • 9
  • 9

9 Answers9

30

Even if there is such a tool, I'd highly recommend you to do the conversion by hand. Automatic converters will often faithfully reproduce the code, but ignore idioms - because they'd be really, really hard to get right.

Furthermore, the differences between generics in .NET and Java could lead to some very different decisions in the two codebases.

Really, you'll be better off doing it by hand.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I agree. I did look at the Java Language Conversion Assistant for a relatively small project and the clean-up work needed after the conversion was not significantly smaller than the work needed for a rewrite. – Brian Rasmussen Jan 14 '09 at 14:10
  • Ditto, but more than once despite emphatically warning the respective clients. – Michael Meadows Jan 14 '09 at 15:45
27

A good tool to use is called Sharpen, which is open source.

This tool has been forked and updated by the Xamarin team and they used it to translate the Android APIs into C#. Their copy can be found here:

Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
alfdev
  • 1,039
  • 1
  • 10
  • 23
16

Java Language Conversion Assistant. Optionally installed with (at least) Visual Studio 2005 Standard Edition.

Select File/Open/Convert/Java Language Conversion Assistant.

Remember to manually go over the code afterwards. It will have lots of issues.

Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
  • Is there any other alternative in conversion of Java to C#? I've successfully use VS2005 to convert Java code to C# with not too many modifications by hand and I really like it. – Gant Jan 14 '09 at 14:07
  • 6
    Not only will it have issues at the implementation level, but it won't be designed in a .NET-idiomatic way. Again I say, hand-porting... – Jon Skeet Jan 14 '09 at 14:09
  • 2
    Hand conversion is always required, but a lot of code can go over as is. Replacing small syntax differences like uses of final, checked exceptions, and synchronized goes a long way when porting. – Eric Hauser Apr 01 '10 at 18:48
  • 1
    doesnt appear to be around in vs2010 though? – Brady Moritz Aug 20 '10 at 16:14
  • Thank God, it must be dead! Dude, do this by hand. – Brad Nov 01 '10 at 22:59
  • 2
    JLCA is discontinued, see http://stackoverflow.com/questions/3070261/where-is-the-java-language-conversion-assistant-jlca-3-0 – Peter Kofler Dec 04 '16 at 17:41
13

ikvm exposes java classes in .NET. Its not a converter, but based on my experience I'd recommend it to anyone making the transition from java to .NET

Jordão
  • 55,340
  • 13
  • 112
  • 144
7

Right now, i am testing Tangible Solution (that is not for free)

Tangible Source Converter

And it add a Class Helper. But outside it,the result looks fine.

Java: (original)

public class PackVariableArrays {


private ClassError myerror=new ClassError();


public VariableArrays unpack(String txt) {
    VariableArrays pc = new VariableArrays();
    Variable lc;
    txt=txt.replace("\r\n","\n");
    setMyerror(new ClassError());
    if (txt==null) {
        lc=new Variable();
        lc.name="ERV-5: Empty values";
        pc.addItem(lc);

        return pc;
    }
    String[] linecode = txt.split(ClassUtil.SEPARATOR2);
    int blen = 0;
    int tmpint = 0;

    int numelement = 9999;

C# (conversion)

public class PackVariableArrays {


    private ClassError myerror =new ClassError();


    public virtual VariableArrays unpack(string txt) {
        VariableArrays pc = new VariableArrays();
        Variable lc;
        txt=txt.Replace("\r\n","\n");
        Myerror = new ClassError();
        if (txt==null) {
            lc=new Variable();
            lc.name="ERV-5: Empty values";
            pc.addItem(lc);

            return pc;
        }
        string[] linecode = StringHelperClass.StringSplit(txt, ClassUtil.SEPARATOR2, true);
        int blen = 0;
        int tmpint = 0;

        int numelement = 9999;

It is a easy case but it works fine. However, as i said early, it uses a Class Helper ( StringHelperClass.StringSplit) that is fine but it is unneeding. Outside it, the result code is pretty clear.

magallanes
  • 6,583
  • 4
  • 54
  • 55
6

While the syntax of the two languages seems alike, the semantics are far different. To mention a few places the two languages go astray

  • Generics, Java compiles everything to objects, C# retains the generic types
  • Exceptions, Java has checked exceptions, C# doesn't
  • Anonymous classes and inner classes, Java has anonymous classes and nested classes, C# has neither. Instead C# has delegates and events. The programming model is thus very different
  • delegates, C# has the notion of function pointers which leads to a different way of programming.
  • events, C# has the notion of events and components which leads to a different way of programming.
  • API, setting all the semantical differences aside, both langauges has acompanied huge API's, neither of which are trivially converted.

In other words, you won't be able to make such a transition automatically. If the reason for changing to C# is to be able to translate your code into an .exe file, there are various options also in the Java market.

Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114
  • 1
    C# does have nested classes. I thought you were wrong so I did a bit of research and it seems inner classes (a Java thing) are different from nested classes (present in both). An interesting if obscure subtlety. – Peter Wone Jul 20 '14 at 08:32
  • Yes inner classes and nested classes are not the same. – Carlo V. Dango Jul 26 '14 at 12:14
  • None of those differences are fatal. There will certainly be a place for converting some part of a large code base. Things to do with HTTP etc. will be different. – Tuntable Mar 08 '21 at 02:29
3

Microsoft used to have their own Java to C# Converter - Microsoft Java Language Conversion Assistant 3.0

revs
  • 651
  • 4
  • 8
2

Have you tried XMLVM ? It has an option to automatically convert to C# like this:

xmlvm --in=myjar.jar --out=output_dir --target=csharp
Panayotis
  • 1,792
  • 23
  • 32
1

Recently Xamarin have ported android to mono using sharpen. Check out this link.

Alexander Oh
  • 24,223
  • 14
  • 73
  • 76