?
Solved

FileChannel.tryLock : IOException: Invalid argument

Posted on 2009-04-08
9
Medium Priority
?
1,288 Views
Last Modified: 2012-05-06
I've run into a baffling problem porting a Java program over to a FreeBSD system from Trustix.  When I try to obtain a file lock on a RandomAccessFile I get an "Invalid Argument" exception.

When searching for a fix I found reference to 64-bit systems having problems with this, but that has been fixed in java 1.4.3 (IIRC).  I did try passing in a block size to tryLock() (ie: tryLock(0L, 1024L, false) ) but that did not change the result.

See code snippet for code and exception.

System:
FreeBSD 7.1, 64bit
Filesystem: UFS
java: jdk 1.6.0

Anyone have any input on this?
// code
      FileChannel channel = new RandomAccessFile("fileLockTest","rw").getChannel();
      FileLock lock = channel.tryLock();
 
// exception
java.io.IOException: Invalid argument
        at sun.nio.ch.FileChannelImpl.lock0(Native Method)
        at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:924)
        at java.nio.channels.FileChannel.tryLock(FileChannel.java:978)
        at FileLockTest.obtainRefreshLock(FileLockTest.java:47)

Open in new window

0
Comment
Question by:malklavious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24102518
first create the lock

      FileChannel channel = new RandomAccessFile("fileLockTest","rw").getChannel();
        FileLock lock = channel.lock();
      lock = channel.tryLock();

0
 
LVL 2

Author Comment

by:malklavious
ID: 24102892
I was doing that at first, moved to the tryLock as I was trying to figure out a way to get it to work.

Tried it again, still getting the exception, now on the "channel.lock()" line.
// code
      FileChannel channel = new RandomAccessFile("fileLockTest","rws").getChannel();  
      FileLock lock = channel.lock();  // <-- exception thrown here
 
// exception
java.io.IOException: Invalid argument
        at sun.nio.ch.FileChannelImpl.lock0(Native Method)
        at sun.nio.ch.FileChannelImpl.lock(FileChannelImpl.java:887)
        at java.nio.channels.FileChannel.lock(FileChannel.java:876)
        at FileLockTest.obtainRefreshLock(FileLockTest.java:66)

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24103569
> I did try passing in a block size to tryLock() (ie: tryLock(0L, 1024L, false)

that should have worked according to:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6371627
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 2

Author Comment

by:malklavious
ID: 24108006
That's exactly why I'm so baffled!  I read that bug and a couple of other bugs related to using Long.MAX_VALUE on 64-bit systems could cause problems.

I just ran a couple more tests, using start/end pairs of:

0, 1024
1, 1024
0, 0

All with the same result as above...
0
 
LVL 2

Author Comment

by:malklavious
ID: 24117318
One side note, looks like the system has OpenJDK 1.6.0, so it may be an OpenJDK issue.  I'm going to try and do a test on the same OS/Filesystem with regular Java to see what happens.
0
 
LVL 1

Expert Comment

by:nj_glenn
ID: 24117479
Try to run java -version from the command line.  If it doesn't report correctly, you may have a configuration error.  

Expected output:

[/usr/local/etc]$ java -version
openjdk version "1.6.0-internal"
OpenJDK Runtime Environment (build 1.6.0-internal-root_17_mar_2009_11_47-b00)
OpenJDK 64-Bit Server VM (build 11.0-b17, mixed mode)
0
 
LVL 2

Author Comment

by:malklavious
ID: 24117522
Doing some more testing with different systems found that it is not isolated to OpenJDK, on a different system that is also running OpenJDK I was able to get the FileLock to work.  Seems like it is a configuration issue on the system I was doing my initial tests on.  Having my SysAdmins take a look to see what's different (they should be identical).
0
 
LVL 1

Accepted Solution

by:
nj_glenn earned 1500 total points
ID: 24117555
Interesting. Have your sys-admins be sure both versions of the JDK are the same on each machine.
0
 
LVL 2

Author Comment

by:malklavious
ID: 24117619
Bingo, looks like that is the problem.  The OpenJDK versions are different.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month9 days, 16 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question