0

General Information

Windows 10 Home Edition , Java 8 Update 121

Description

I have an simple Java program that renames Files and Folders .

As I read from different articles Maximum filename length is generally 255 for modern versions on operating Systems , like Linux, Windows, Mac Os. So I keep it maximum of 240 just in case.

  1. Maximum filename length in NTFS (Windows XP and Windows Vista)?

  2. https://serverfault.com/questions/9546/filename-length-limits-on-linux

  3. https://apple.stackexchange.com/questions/86611/does-os-x-enforce-a-maximum-filename-length-or-character-restriction

  4. https://support.microsoft.com/en-us/help/2891362/a-file-copy-operation-fails-when-files-or-folders-have-long-paths-in-windows-explorer

Test cases

So I have a Folder in the path C://GOXR3PLUS//..//Folder which contains a very simple sqlite3 database File named dbFile.fb .

Case 1 ❌

I rename the Folder to Folder plus 203 characters , so the folder name is Folderrrrrr.... until 207 characters. Trying to connect to the sqlite3 database I get this exception:

SEVERE: 
java.sql.SQLException: The database has been closed
    at org.sqlite.core.NativeDB.throwex(NativeDB.java:471)
    at org.sqlite.core.NativeDB.errmsg_utf8(Native Method)
    at org.sqlite.core.NativeDB.errmsg(NativeDB.java:137)
    at org.sqlite.core.DB.newSQLException(DB.java:921)
    at org.sqlite.core.DB.throwex(DB.java:886)
    at org.sqlite.core.NativeDB._open_utf8(Native Method)
    at org.sqlite.core.NativeDB._open(NativeDB.java:71)
    at org.sqlite.core.DB.open(DB.java:174)
    at org.sqlite.core.CoreConnection.open(CoreConnection.java:220)
    at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
    at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:26)
    at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:24)
    at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
    at org.sqlite.JDBC.createConnection(JDBC.java:114)
    at org.sqlite.JDBC.connect(JDBC.java:88)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at database.DbManager.<init>(DbManager.java:149)
    at application.Main.lambda$8(Main.java:508)
    at java.lang.Thread.run(Thread.java:745)

Case 2 ✅

I rename the Folder to Folder plus 196 characters so the folder name is Folderrrrrr.... until 201 characters. No exception occurs trying to open the sqlite3 database.

Finally

I am trying to open the dbFile.db with notepad for the first case , and it opens. Eclipse reports file not found , and with the Java application I am getting the error i posted .

Image from Eclipse error enter image description here

My Question is:

Why does this happen, even though I am not even passing 210 characters for Folder Name?

Community
  • 1
  • 1
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 4
    This has nothing to do with java, jdbc **or** sqlite3. This is a limitation of **Windows**. – Elliott Frisch May 12 '17 at 16:19
  • 1
    Possible duplicate of [Why does the 260 character path length limit exist in Windows?](http://stackoverflow.com/q/1880321/5221149) – Andreas May 12 '17 at 16:24
  • @Elliott Frisch Oou i see from the answers , so i should check the path+filename during renaming so it doesn't extend 255 characters . – GOXR3PLUS May 12 '17 at 16:38

1 Answers1

4

There is a limit on path length, not only file name length. You are probably exceeding path length.

From Windows docs:

Maximum Path Length Limitation

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

  • Note File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections.

The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters. This type of path is composed of components separated by backslashes, each up to the value returned in the lpMaximumComponentLength parameter of the GetVolumeInformation function (this value is commonly 255 characters). To specify an extended-length path, use the "\?\" prefix. For example, "\?\D:\very long path".

  • Note The maximum path of 32,767 characters is approximate, because the "\?\" prefix may be expanded to a longer string by the system at run time, and this expansion applies to the total length.

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
whbogado
  • 929
  • 5
  • 15
  • Thanks for this detailed answer ! I want the app to be platform independed , so should i check the path + filename length before allowing user to rename the file? – GOXR3PLUS May 12 '17 at 16:35
  • 1
    @GOXR3PLUS As you can see, even for one OS (Windows), maximum file/path length varies. If you establish a hard limit you will be arbitrarily limiting your application capabilities. The recommended way to verify the file name is to try the operation, if you get an exception you know that something went wrong. Sometimes it is difficult to determine the exact cause but at least you gain maximum platform independency since you are not embedding any OS specific feature in your code. – whbogado May 12 '17 at 17:06