Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Migrating Java applications to .Net

How to convert existing Java or Visual J++ applications to J# or C#

  • Print
  • Feedback

Two paths are available for migrating Java applications to .Net: upgrade them to Visual J# .Net or convert them to Visual C# .Net. Upgrading Java applications to J# is the quickest and easiest way to port Java applications to the .Net Framework. Java developers are instantly productive on Visual J# .Net because they are already familiar with the syntax and set of class libraries.

Java applications can convert to C# using the Microsoft Java Language Conversion Assistant (JLCA). Code that calls Java APIs can convert to comparable C# code that uses the .Net Framework. The JLCA converts 90 percent of JDK-level 1.1.4 calls and emits issues in code for the other 10 percent. Each issue is linked to a topic with guidelines for modifications needed to finish the conversion.

Though converting to C# is slower than upgrading to J#, it offers more opportunities because converted applications use native .Net Framework APIs.

Download the source code that accompanies this article from Resources.

Sample Visual J++ application

As a demonstration, we have developed a sample Visual J++ (VJ++) application named CustomerDemo to port to .Net. The CustomerDemo application is modeled after a typical Java business application and uses some Java SDK APIs as well as Microsoft Java packages. CustomerDemo employs standard Java Database Connectivity (JDBC) APIs to access the SQL server to retrieve customer data. This data then transforms into hierarchical and flat views using the Java Collections API. The views are constructed by employing Microsoft Windows Foundation Classes (WFC). The following CustomerDemo parts will be detailed:

  • User interface
  • Database access
  • Common Java APIs used

User interface

CustomerDemo is a Windows application that has a Windows Form, ViewCustomer, shown in Figure 1:

Figure 1. CustomerDemo application

ViewCustomer is a com.ms.wfc.ui.Form hosting a typical set of Windows controls such as Panel, TreeView, Splitter, Edit, and Labels. It shows customers in a left-pane, hierarchical view. Countries are shown at the first level and customers at the leaf level. Clicking on each customer retrieves the detailed record of that customer data. These details include Contact and Address, shown in respective edit controls in the right pane.

Commercial applications generally have resources like icons and bitmaps stored in resource files. So, we also have an icon resource embedded in ViewCustomer Form and stored in the associated resource file ViewCustomer.resource.

Database access

We have provided the SQLData class that retrieves customer data from the Northwind database (sample database that comes with MS SQL server). SQLData accesses the database server by employing standard JDBC APIs. It connects to the database using the following code:

dbCon_ = DriverManager.getConnection(conInfo_,"","");


Country and customer data is retrieved using the following code:

stmt = dbCon_.createStatement();
rs = stmt.executeQuery(sqlQuery);
while(rs.next()){
      String countryId = rs.getString("Country");
      String companyName = rs.getString("CompanyName");
      //Package data into a Hashtable
      ...
}


Common Java APIs used

The CustomerDemo application uses common Java classes from java.util and java.lang packages. For example, it uses java.util.Hashtable to store the relationship between customers and countries. The java.util.Hashtable key is country name, and values are customers stored in a java.util.Vector. We use java.util.Enumerator to iterate the country keys and then get the associated list of customers. The relevant code is below:

  • Print
  • Feedback

Resources