JDK-6385390 : Exclusive locked file by java results in Microsoft Office reporting confusing error
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-02-14
  • Updated: 2011-02-16
  • Resolved: 2006-02-14
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

EXTRA RELEVANT SYSTEM CONFIGURATION :
Service Pack 2
CPU: Pentium 4  3.00GHz
RAM: 1G

A DESCRIPTION OF THE PROBLEM :
If we open exclusive locked doc(MS-WORD) file with microsoft word and then word doesn't  display proper lock message.
For several tens of seconds microsoft word is waiting and then issue error message(not lock message).

But doc file is locked by another native application and then microsoft word issue proper lock message.

This problem happens to all microsoft office application(excel,powerpoint,word).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create microsoft office word document.
2. Do exclusive Lock on a doc file
3. Open locked doc file with microsoft word

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Microsoft word issue proper lock message immediately
ACTUAL -
For several tens of seconds microsoft word is waiting and then issue error message(not lock message).

ERROR MESSAGES/STACK TRACES THAT OCCUR :
no error message

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class TestLock {
	
	public TestLock(){
	}
	
	public void lock(String path){
		try{
			File file = new File(path);
			RandomAccessFile afile = new RandomAccessFile(file,"rw");
			FileChannel channel = afile.getChannel();
			FileLock lock = channel.tryLock();
			
			try {
				if(lock !=null){
					// waiting until user input
					System.in.read(new byte[10]);
				}
			}
			finally {
				if(lock !=null){
					lock.release();
				}
			}
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		TestLock test = new TestLock();
		test.lock(args[0]);
	}
}

---------- END SOURCE ----------

Comments
EVALUATION I repeated the experimental described in the description: 1. java TestLock foo.doc 2. In Microsoft Word (2003 SP1) attempt to open "foo.doc" 3. Word hangs for a couple of second and then a dialog pops up: "Word experienced an error trying to open the file." The dialog suggests checking the file permissions and other things. So clearly there isn't a bug in FileChannel here - the file is locked exclusively and Word is unable to open it (which is the expected behaviour). The bug description is not very clear but I believe this is a request for the locking to also work with the proprietary lock notification mechanism used by Microsoft Office. This mechanism allows Office applications to report more detailed errors such as "File foo.doc is locked for editing by Joe". Sorry, this mechanism appears to be specific to Microsoft applications and it would be more appropriate if Office were updated to print a clearer message when it attempts to open a locked file.
14-02-2006