Making Your ColdFusion and Java Applications More Platform Independent (Part III)

Making Your ColdFusion and Java Applications More Platform Independent (Part III)

Two years ago (has it really been that long?!), I made a couple of posts about how to make your ColdFusion and Java applications more platform independent. The first post covered the importance of case consistency, and the second talked about not hard-coding path separators since they are different on different platforms. Well, all this time later, I actually have a third piece of advice: don’t hard-code new line characters.

I’m switching my development environment over from OS X to Windows (I haven’t used Windows consistently in many years, so I figured it was time to give it a try again), and while trying to get MXNA running locally, I came across a bug that I’d never seen in development (on OS X) or on production (Linux). The problem was this line of code:

<cfset blArray = listToArray(blacklist, chr(10))/>

The code is trying to convert a line-separated list into an array. On OS X and Linux, it works fine since the new line character is "line feed", or chr(10), or \n, but on Windows, the code didn’t work because a new line is "line feed" and "carriage return", or chr(10) & chr(13), or \n and \r. So rather than ever having to worry about this again, I used a little Java trick to make the code run in any environment. Now it looks like this:

<cfset blArray = listToArray(blacklist, createObject("java", "java.lang.System").getProperty("line.separator"))/>

Even with a language like Java (and hence, ColdFusion), if you want your code to be truly platform independent, it takes a little work. The good news is that once that was fixed, MXNA was up and running in my development environment perfectly.

8 Responses to Making Your ColdFusion and Java Applications More Platform Independent (Part III)

  1. Stephanie says:

    I’m switching my development environment over from OS X to Windows (I haven’t used Windows consistently in many years, so I figured it was time to give it a try again” — Christian! Surely you’re not deserting us… 😉

  2. Not deserting. Just exploring. I still have a Mac close by at all times.

  3. JediHomer says:

    Ok, maybe a silly question, but if a CRLF in OSX and Linux is Chr(10), and a CRLF in Windows is Chr(13) & Chr(10), why would ListToArray(foo, Chr(10)) fail because all the OS’s listed above contain Chr(10).

  4. Murat says:

    If you use two characters for CF list functions, they can use one of them to determine the list items. This means you can safely use , this should work on any platform.

  5. Brice Laurencin says:

    I have kinda of the problem you describe, but the difference is that the server is linux, and the browser is on Windows, so, the System.getProperty(“line.separator”) trick won’t make it there.any idea ?

  6. Brice, this will still work fine for your ColdFusion code which is running on the server.Christian

  7. Ben Nadel says:

    How about CFIMPORT library tags? They cannot be dynamic in path values, so wouldn’t you have to hard code the path stuff? I would love to make that dynamic as i could code in the WebRoot (../../) and my the folder ultra portable without code touch ups.

  8. Tjarko says:

    Can’t you use #expandpath(‘your relative path’)# to have the path dynamicly?? I’m not really familiar with the CFIMPORT tag.. but it works for CFFILE and CFDIRECTORY tags.