You can go both using the canonical URL . For example, in the <head> both /Java.html and / book / Java.html:
<link rel="canonical" href="/book/programming/Java.html" />
With this, Googlebot (and Yahoo / MS) will see the current page as a duplicate of the canonical link and ignore it, without the usual flaws that come with a trick.
Long URLs are good for being descriptive, understandable, and searchable, while short URLs are good for people who can send to friends and any social network in jour - maybe you want both.
Maintaining different URLs and covers will add some work to the server. If this is too much effort, I would go with a long form for users and search. "java.html" might just be a random coffee page, it needs context.
source share