Wednesday, January 7, 2009

Proof Of Concept – 4 of 8 : IB's Java Test Client

Next step is to use NetBeans to compile and run IB's Java test client. The process is described in documentation JavaAPIGettingStarted.pdf (Chapter 5) on IB's web site. It's a good idea to read this doc and follow the directions. Running the Java test client basically consists of:
  1. Create a new Java application project in NetBeans
  2. Attach packages in Java\com and Java\TestJavaClient folders (see Proof Of Concept - 2 of 8) to the project
  3. Press F6 to run
  4. Test client magically runs and ready to connect to TWS


Well, this didn't quite work out for me. NetBeans was unable to compile the Java program. Note the red exclamation marks in the upper left corner.



Looking a little deeper at the API code, looks like there's some sort of problem with loading the classes in IB's packages. NetBeans just doesn't like the package names on all the Java class files.


I was stuck at this step for a little while but was able to resolve it (with a little bit of hacking). The issue is that NetBeans is unable to load IB's package names com.ib.client and TestJavaClient defined in Java classes in Java\com and Java\TestJavaClient folders. So some editing of the Java class files are required.


1. In Explorer, copy the two folders in Java\ folder and paste them into a new folder. This is where edited versions will be. I just don't like to lose the original version in case something goes wrong. I named this new folder twsapi\.

2. In NetBeans, for each .java file in ib.client package:
  • At the top of the file, change text package com.ib.client; to package ib.client;
  • Save, and there shouldn't be any errors
3. In Explorer, rename folder hierarchy from twsapi\TestJavaClient to twsapi\x\y\TestJavaClient



now becomes






4. In NetBeans, for each .java file in TestJavaClient package:
  • At the top of the file, change text package TestJavaClient; to package y.TestJavaClient;
  • In the rest of the file, change any occurrence of text import com.ib.client. to import ib.client
  • Save, and there shouldn't be any errors

5. Create a new Java application project.

6. Attach packages in twsapi\com and twsapi\x folders to the project. Note no more red exclamation marks in the upper left corner.



7. Press F6 to run.

8. Test client runs and ready to connect to TWS. Try out its functionalities as specified in documentation JavaAPIGettingStarted.pdf.


No comments: