Convert Java project code to C # code

Source: Internet
Author: User

Due to project requirements, it is necessary to convert the code developed by a Java project to the code of C # recently. It is time-consuming to convert the code of a file to a file. You can use the tool to add your own experience, you can get twice the result with half the effort. The following describes the conversion process.

The code to be converted this time is the IBE client. Ibe provides two external connection methods: 1. Java; 2. com. Java is generally used for Java clients, and COM is generally used for. NET development on Windows platforms. Our platform uses the COM Client. Through the platform performance test, we found that the com version has poor performance. After communicating with colleagues in the IBE group, we said that the com version is also intended to be modified, for example, the internal program structure does not allow repeated function names, and the conversion of managed code and COM components may reduce the efficiency, which can be achieved by imitating the Java call method. net call code, they also support. This article describes how to convert the Java version of The IBE client to the C # client.

In order not to show the IBE source code, this article will try to introduce only the tools and ideas of the conversion process.

 

Tool introduction:

Eclipse plug-in: j2cstranslator is used in the conversion process, the project address is in the http://sourceforge.net/projects/j2cstranslator/, but the most recent plug-in that can be found is in: Release. The plug-in can easily convert the code from eclipse to C # Code. However, to make the converted C # code run normally, the j2cstranslator plug-in's official ing file j2csmapping is also required, it is used to associate some java features with the C # features, compile and run normally. Corresponding to different ing files such as. net2.0, 3.5, and 4.0. The ing file only has one ilog. j2csmapping. dll.

However, the ing provided by the plug-in is used.. Net file, there are still some errors in vs compilation that cannot be compiled. After a lot of attempts, I found another one. net open-source tool: ikvm. net, official address: http://www.ikvm.net /. Ikvm implements Java virtual machine functions written in. net, including command line tools that convert from jar packages to DLL directly, and environment support for mutual calls between Java and. net. Ikvm provides 30 DLL files and 3 EXE files. Some call and conversion methods of ikvm can search for other resources online. I only reference ikvm In the converted project. openjdk. core. the DLL file can be compiled, but other DLL files of ikvm can run normally during the final run.

 

Convert to available production code:

Ilog is referenced in the IBE code conversion process. j2csmapping. DLL and ikvm. openjdk. core. DLL and other ikvm dll will still have some errors, such as the super ,. the base class used in net needs to be manually modified. The socket Processing Method in Java is different from that in. net. You need to manually modify the code.

To use it in production, you must not rely on too many external DLL files. Although ikvm is open-source, there will be some unmanaged C ++ code in it. To use it in production, we must use the external dll as little as possible. net Framework code implementation Java implementation method, which needs to be flexibly handled according to the actual situation. During the IBE conversion process, I found that most of the conversion work is socket and calendar, after continuous debugging and analysis, the code conversion is finally replaced. Of course, according to different Java projects, the objects to be converted and the processing method may be different, and the right remedy is required.

 

Conversion process:

After installing the Eclipse plug-in, open the Java version of The IBE source code, and then select the eclipse file-> export ..., I:

 

Figure 1

On the export page, other has an ilog Java to CSHARP translator option. Select next along the way, and then select the generated directory after conversion. After conversion, there is no csproj project file, all of which are. net source code. CS files.

Create a. Net solution, create a. Net project, and include the converted file in the solution. The project references the ilog. j2csmapping. dll and ikvm. openjdk. Core. DLL files, and then compiles the files. At this time, many errors may occur, because any tool is not omnipotent. In particular, the conversion of programming languages requires manual modification.

Finally, based on different types of features, manually switch the external-dependent API method to the API provided by the. NET Framework. Because the code is confidential, no example is given.

 

Summary:

This article mainly introduces the idea of conversion and involves very specific code conversion. It is better to teach people to fish than to teach them to fish. This article will be of great help to me in my future work.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.