Robot takes shortest path rather than avoiding joint limit

It sounds like your issue is the same as the problem described here:

There are some suggested workarounds there you can take a look at.