donderdag 17 december 2009

Proper handling of linked resources

In the Eclipse workbench you can import resources, meaning copying them into the workspace, or you can set up a folder to link to them, and they files stay where they were. For large datasets, linking has obvious advantages. There is even a Decorator to show linked resources.

When programming in Eclipse you sometimes must use plain Java based libraries. These libraries handle files using java.io.File. When you work in Eclipse you reference files in the workspace using org.eclipse.core.resources.IFile. So in this case I needed to convert between the two formats.

First attempt, WRONG!

My first attempt was to use f.getFullPath().toFile() and that worked! Because I tested all my code with small data sets in the workspace, it wasn't until later that I found out, this does not work with linked resources:
java.io.FileNotFoundException: /Remote-Data/eulumdat/data.file (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:106)
at java.io.FileReader.(FileReader.java:55)
at xxxx

Second attempt: the proper way

My went back to the offending code and used CTRL+SPACE again. Now I selected f.getLocation().toFile() and that did it!

Conclusion

In order to get your code working in all circumstances, you need to test with both resources in the workspace and outside.
I'm i good company though, as there are 64 open bugs in the Eclipse bugzilla about linked resources.
Also I suspect code that doesn't work with linked resources, will not work with the remote system explorer, like this one.