Friday, 2 July 2010

JNI/Win32:java.lang.UnsatisfiedLinkError

I spent almost two whole days trying to solve this issue. I did everything possible from altering my c++ code (changing the return types) to compiling from command line rather than the IDE. Also, I tried a number of other steps like changing the classpaths, refactoring package names to changing names of the generated header files. Finally, I found the following resolution.

Whenever the C++ compiler (GCC) generates a DLL, it is exported in the following form:
Java_somepackage_SomeClass_someNativeMethod@8

Where the integer suffix suggests the byte space required by the arguments. This sort of function call makes no sense to the JVM (while invoking someNativeMethod) and hence it leads to java.lang.UnsatisfiedLinkError. The resolution is to add the following flags while linking to generate unmangled names:
-Wl,--add-stdcall-alias

This will create an alias name (pure method call name) to the generated method. This allows the JVM to invoke the right method via JNI.

I almost gave up on this issue, before I finally found this resolution. The point to note here is that this happened to me only on Win32 - I had no issues running on the UNIX platform.

Friday, 28 May 2010

Clash of the Titans, C++ vs Java!

As a software engineer, the mind is trained to seek optimizations in every aspect of development, and ooze out every bit of available cpu resource to deliver a performing application. This begins not only in designing the algorithm or coming out with efficient and robust architecture, but right onto the choice of programming language. Most of us, as we spend years in our jobs - tend to be proficient in at least one of these.

Recently, I spent some time to check on the performance (not a very detailed study) of the various programming languages. One, by researching on the internet; Two, by developing small programs and benchmarking. The legacy languages - be it ASM or C still rule in terms of performance. But these are definitely ruled out for enterprise applications due to the complexity in development, maintainability, need for object orientation and interoperability. They still will win for mission critical or real-time systems, which need performance over these parameters. There were languages i briefly read about, including other performance comparisons on the internet. These include Python, PHP, Perl and Ruby.

Considering all aspects and needs of current enterprise development, it is C++ and Java which outscore the other in terms of speed. According to other comparisons [Google for 'Performance of Programming Languages'] spread over the net, they clearly outshine others in all speed benchmarks. So much for my blog title :-)

So when these titans are pit against each other in real time, considering all aspects of memory and execution time - Java is floored. Though I have spent last 7 years of my life coding and perfecting my Java and J2EE skill - I suddenly feel... Ahem, Slow! One of the problem statements to verify this is given below (alongwith the associated code) and the associated execution parameters.


[Disclaimer: Problem Statement given below is the property of www.codechef.com]
In Byteland they have a very strange monetary system. Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. But these numbers are all rounded down (the banks have to make a profit). You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. You have one gold coin. What is the maximum amount of American dollars you can get for it?

Input
The input will contain several test cases (not more than 10). Each testcase is a single line with a number n, 0 <= n <= 1 000 000 000. It is the number written on your coin.


JAVA SOLUTION

C++ SOLUTION

RESULTS

TIME

MEM

LANG

0.00

2.8M

C++ 4.3.2

0.26

218.3M

JAVA



I am sure these results will continue to be remain in the same ratio (with slight variations for other or benchmark problems) - even with the most optimized java code.



Saturday, 31 January 2009

JAR Searcher Developer Tool

As a Java/J2EE developer, I know that the most commonly faced issue in development, which has also been reported as the primary reason for developers losing hair - missing classpath files or not being able to trace a JAR which contains the file - when you know you have all JARs that could possibly exist in this world on your local file system.

Just wrote this simple tool, JarSearcher v1.0, which accepts the name of class file you are looking for and returns all the JARs on your system which contain this file. Also, for Windows developers, I have added a .bat file (Unix folks, kindly replicate .sh) which takes the following form for execution:

js [class-name] [start-in] -c
[class-name] The name of the class file to search (without .class)
[start-in] The directory in which to start looking for the files
-c This switch will toggle display of processing information

or you can directly invoke the program from the bundled JAR file:

java -jar jar-searcher.jar [class-name] [start-in] -c


I have included the source, feel free to modify it as per your convenience.

Friday, 23 January 2009

Spring - DWR - Ext JS Chat Application

I was curious to explore the capabilities of Reverse Ajax. That's when I created this simple chat application using Spring/DWR/Ext JS.

From my experience, I can easily say that DWR is easy to learn and configure, especially when you are planning to integrate with Spring on the application tier. DWR has a powerful API to perform all relevant operations, right from accessing page script sessions to util classes for sending updates to the client.

I used Ext JS for creating the user interface, which renders stunning display for elements like forms, buttons, etc. Ext JS has a very steep learning curve and each operation requires a lot of configuration and reference. Also, I found that the event handling mechanism, though complete, is very complex to use. I relied on external Javascript coding for handling events. On the upside, the documentation and support is really good for this framework. Inspite, I would instantly recommend use of Ext JS for large sized customer facing web-based applications, especially for the internet. For medium scale projects or enterprise based projects, I would think twice.


Login Screen


Chat Screen


Simply, drop the .war file in your web/app server. All dependencies are available in the archive itself. The only external reference is 'servlet-api.jar', which is part of the default lib in all latest containers. The source is packaged separately. Once deployed, the url to access should be: http://[server_name]:[server_port]/kabootar/login.kabootar

If you are trying to figure out what 'Kabootar' means, it's hindi for 'Pigeon'...

Friday, 16 January 2009

JBoss Cache XSD Config in Eclipse

I was facing this issue in Eclipse Europa where the Spring configuration XML was not getting validated, when i included JBoss Cache related configuration in it. This is primarily because the Namespace and the Schema URI,
are not available at the respective locations, hence i guess these were not bundled in Eclipse Europa. I spent about 4-5 hours breaking my head on this issue before I found the solution in another blog.

The way to get your XML to be reported as validated is:
1. In Eclipse, Click on Window > Preferences.. > Web and XML > XML Catalog
2. Under XML Catalog, Click on User Specified Entries and then click on Add...
3. Create the following Entries/Locations

>>>>> [Replace pathto with the actual location of the JAR on your local machine.]
>>>>>> jar:file:pathto/spring-modules-cache.jar!/org/springmodules/cache/config/springmodules-cache.xsd
>>>>>> KEY TYPE Schema Location

>>>>> jar:file:pathto/spring-modules-cache.jar!/org/springmodules/cache/config/jboss/springmodules-jboss.xsd
>>>>> KEY TYPE Schema Location
>>>>> KEY http://www.springmodules.org/schema/cache/springmodules-jboss.xsd


4. Refresh the XML file in the Project Explorer and... all XML validation errors have gone!

Thursday, 15 January 2009

Spring, Hibernate, EhCache Recipe

A simple scenario explaining the usage and performance, when using EhCache along with Hibernate in a Spring environment. The performance results are taken using mySQL as the database. I started off my experiment with JBossCache, but had one hell of a time with it's configuration within Spring. [Download Sample Code]

In this example, I need to retrieve close to 5,000 records in a single fetch and then cache this information. As usual, setup Spring contexts in your Spring configuration file. I have just one bean, which is a Hibernate DAO, HibernateDoctorDAO.java. The dependency injection hierarchy is dataSource > sessionFactory > hibernateTemplate. hibernateTemplate is then injected into the HibernateDoctorDAO.java at runtime by the Spring Framework.

The implementations of each of these are:
dataSource > org.apache.commons.dbcp.BasicDataSource
sessionFactory > org.springframework.orm.hibernate3.LocalSessionFactoryBean
hibernateTemplate > org.springframework.orm.hibernate3.HibernateTemplate

The results clearly shows the difference in performance with EhCache enabled, even in this simple example:
PERFORMANCE COMPARISON (in seconds)
==================================
QUERY FETCH TIME (INITIAL): 0.599
QUERY FETCH TIME (HIBERNATE CACHE): 0.212
QUERY FETCH TIME (2ND LEVEL CACHE): 0.091

The main program to run the sample is SpringOrmIntegration.java

Version Reference > Spring-2.5, Hibernate-3.0, EhCache-1.4, mySQL-5.0

Wednesday, 7 January 2009

Thoughtworks - A Different Company!

It might be an inappropriate entry for this blog - sharing an interview experience. But one that might help people the most [ :-) ] especially when it is about... ThoughtWorks!

The company has a very interesting interview process, different than most i have come across, which speaks a lot of the company culture and the work that you might do there. I write this from the perspecive of an interviewee who participated in the interview process there, twice!

1. It all starts with an online application [Unless you are the lucky one whose CV has been sourced!]
2. Once shortlisted, you get a call from the HR who checks your comfort submitting code and explains the interview process.
3. You get two problem statements by mail, and have the choice of submitting solution for either of them - focus is on object oriented design [Mars Rovers problem should be famous by now]
4. Next I had a Phone screen, in which i was presented with a problem again for which one has to design and implement a solution - again object oriented skills are the focus. During the course, you might be presented with a change in requirement and have to explain how easily your design can adapt to these changes.
5. When i was interviewed the first time, the next round was in-office and I had logical and reasoning ability tests - which are really a breeze.
6. The first time I did not want to proceed beyond this point due to some personal reasons... The second time around I wasn't selected [ :-) ], But what I know is that the subsequent interviews focus on how well you can code pair with other Thoughtworkers and a series of technical interviews on your primary technology skill.
7. The offer, I am sure once in you would never want to be out - working with the best technical minds....

What I love about this process, as a developer, is that unlike other interview it tests and validates all your abilities in real-time [technology knowledge, problem solving, design skills, implementation skills and most of all that you can code - no matter whether you are an architect, tech lead or a senior developer]. The other important aspects that I like [and have read or heard] about the company is the flat hierarchy, enterpreneurial culture, agile process and that it values technology.