The January 2015 Blog
send me a comment

Java Agent HTTP Connection Errors (Thursday, Jan 15)
[ permalink ] [ e-mail me ] [ ]

While working through some demos for my upcoming ConnectED session on Java, I noticed some unusual behavior when making an HTTP connection in a Java agent on an IBM Notes client. We will only be talking about Java in the context of XPages for the session, but I wanted to try everything out in an agent too in case anyone asks. I had one bit of code that worked fine on XPages but which failed on some of my test clients. It looked like this:

URL url = new URL( myAwesomeURLString );
InputStream in = url.openStream();

In the context of an agent, running on a Notes 9 client, those two innocent lines of code gave me this error:

java.lang.NullPointerException
	at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:738)
	at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:665)
	at COM.ibm.JEmpower.applet.http.HttpURLConnection.getInputStream(HttpURLConnection.java:411)
	at COM.ibm.JEmpower.applet.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:703)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:399)
	at JavaAgent.NotesMain(JavaAgent.java:18)
	at lotus.domino.AgentBase.runNotes(Unknown Source)
	at lotus.domino.NotesThread.run(Unknown Source)

The connection worked, but getting the InputStream failed. That was especially weird because (A) I've used that code plenty of times before without error, and (B) it worked just fine on at least one of my other test clients.

Looking this up on Google I found only sparse reports of other people experiencing this: a question on StackOverflow and a thread on the atnotes.de forum. At the bottom of the atnotes thread there was a link to an IBM APAR that mentions this problem in Notes 8.5.3, but it was specific to applets... and it was opened in 2013 and closed about a year later (I have no idea if there was a fix involved, but it is marked as closed).

If this was a widespread problem there would have been a lot more chatter about it on the Internet, so I assumed that there was something specific about my setup that was causing the error.

After many restarts and toggled settings, and much pulling of hair, I was able to find a set of steps that would either cause the problem or prevent it. To make the code work:

  1. Open the Notes client
  2. Run the Java agent
  3. HTTP connection works

To get the error:

  1. Open the Notes client
  2. Open the Java Debug Console
  3. Run the Java agent
  4. HTTP connection fails

There's something about opening the Java Debug Console before running the agent that causes Notes to use a COM.ibm.JEmpower.applet.http.HttpURLConnection to make the connection instead of a sun.net.www.protocol.http.HttpURLConnection, and the JEmpower version of the class is the one that's broken (per experimentation and the APAR mentioned previously).

Oddly, if I run the agent and then open the debug console and then run the agent again, everything is fine. It's opening the debug console before I do anything that causes problems.

My best guess is that this started with Java JVM 1.6.0 SR12, which was included with IBM Notes 8.5.3 FP3 and Notes 9.0.0, but it could have been before that too. In any case, now that I know not to open the debug console first, that'll be my workaround.