---
title: Java will Bite You
teaser: A way to copy running files in Window using Java.
tags: java
author: Jon Yurek
published_on: 2006-01-20
---

You know that burning flushed feeling you get when you just got a really good
zinger tossed in your face, possibly by your best friend, possibly by your worst
enemy. Doesn't matter. Anyway, on the "worst enemy" part, Java just threw one of
those my way, except it wasn't a zinger, it was the stinging burn of a day and a
half of wasted effort.

In Windows, a `File` object will report false to the functions `delete()` and
`renameTo()` if the file is currently in use. This does not happen on Linux,
where I had originally tested it. I was trying to be all nice about it and use
those functions because I figured they were the "right" way of doing it.

Java only responds to authority, it seems.

Changing to a flat out frontal assault (that is, `FileOutputStream`) on the file
in question resulted in it being properly overwritten. What annoys me about this
is that when I was originally testing yesterday whether or not I could copy over
running files in Windows, I was using the copy command. This, like
`File.renameTo()`, politely says the file is in use and to come back when the
other program is finished, thank you and have a nice day. Later, I was messing
around with xcopy because I was desperately seeking another venue for copying
the damned files, and it overwrote them no problem. I guess, being xcopy, was
more eXtreme than copy and the filesystem's culture shock allowed xcopy to slip
by. As such, I emulated its barbarian tactics, broke the door down, and I was
in.

The moral of the story is, no more Mr. Nice Java for me. [Forcibly inserting a
<abbr title="Uniform Resource Locator">URL</abbr> into the System Classloader at
runtime][force-url]?

[force-url]: https://web.archive.org/web/20060406044726/http://www.0xcafebabe.com/?p=5

Yes, ma'am. I'll do all that and more. I'm a bad boy now.
