-
Bug
-
Resolution: Not an Issue
-
P5
-
None
-
11, 25
-
generic
-
generic
ADDITIONAL SYSTEM INFORMATION :
Windows 11 Home, JDK 23
A DESCRIPTION OF THE PROBLEM :
With LongPathsEnabled on Windows, if we create a Java main class that creates a file with a long path, it fails.
On the Internet I couldn't find any Java side way to solve this issue, so if there's a Java solution to this, please provide as an answer to this Ticket.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. With LongPathsEnabled on Windows:
regedit
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
LongPathsEnabled set to 1
Restart the Computer
2. Create a random directory and file in windows C: longer than 300 characters, it should work.
3. Now create a Java Main Class Program to create a File using a very long path(see code below)
4. The program fails with an exception Caused by: java.nio.file.FileSystemException.
If the line stringBuilder.append("a".repeat(4100)); has the number changed to 200, it works, confirming it's a long path error with the JDK.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The long path file is successfully created.
ACTUAL -
The program fails with exception Caused by: java.nio.file.FileSystemException.
---------- BEGIN SOURCE ----------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class LongPathTestMain {
private static final String LONG_PATH_PREFIX = "\\\\?\\";
public static void main(String[] args) {
File file;
BufferedWriter bufferedWriter = null;
try {
// String filePath = LONG_PATH_PREFIX.concat("C:/node/test/123456789123456789123456789/");
String filePath = "C:/node/test/123456789123456789123456789/";
System.out.println(filePath);
file = new File(filePath);
Files.createDirectories(Paths.get(filePath));
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("a".repeat(4100));
filePath = filePath.concat(stringBuilder.toString());
file = new File(filePath);
Files.createFile(file.toPath());
/*boolean isSuccessful = file.createNewFile();
if (!isSuccessful) {
System.out.println("Failed");
}*/
System.out.println("Full path: " + file.getAbsolutePath());
System.out.println("Full path length: " + file.getAbsolutePath().length());
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(stringBuilder.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
---------- END SOURCE ----------
Windows 11 Home, JDK 23
A DESCRIPTION OF THE PROBLEM :
With LongPathsEnabled on Windows, if we create a Java main class that creates a file with a long path, it fails.
On the Internet I couldn't find any Java side way to solve this issue, so if there's a Java solution to this, please provide as an answer to this Ticket.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. With LongPathsEnabled on Windows:
regedit
Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
LongPathsEnabled set to 1
Restart the Computer
2. Create a random directory and file in windows C: longer than 300 characters, it should work.
3. Now create a Java Main Class Program to create a File using a very long path(see code below)
4. The program fails with an exception Caused by: java.nio.file.FileSystemException.
If the line stringBuilder.append("a".repeat(4100)); has the number changed to 200, it works, confirming it's a long path error with the JDK.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The long path file is successfully created.
ACTUAL -
The program fails with exception Caused by: java.nio.file.FileSystemException.
---------- BEGIN SOURCE ----------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class LongPathTestMain {
private static final String LONG_PATH_PREFIX = "\\\\?\\";
public static void main(String[] args) {
File file;
BufferedWriter bufferedWriter = null;
try {
// String filePath = LONG_PATH_PREFIX.concat("C:/node/test/123456789123456789123456789/");
String filePath = "C:/node/test/123456789123456789123456789/";
System.out.println(filePath);
file = new File(filePath);
Files.createDirectories(Paths.get(filePath));
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("a".repeat(4100));
filePath = filePath.concat(stringBuilder.toString());
file = new File(filePath);
Files.createFile(file.toPath());
/*boolean isSuccessful = file.createNewFile();
if (!isSuccessful) {
System.out.println("Failed");
}*/
System.out.println("Full path: " + file.getAbsolutePath());
System.out.println("Full path length: " + file.getAbsolutePath().length());
bufferedWriter = new BufferedWriter(new FileWriter(file));
bufferedWriter.write(stringBuilder.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
---------- END SOURCE ----------