Issue
I want to createNewFile
with a path but I got an IOException. The question is, the detailed message cannot be interpreted, I can only see a bunch of question marks.
src="https://i.stack.imgur.com/VyuwJ.png" alt="enter image description here" />
I am with Windows 10 originally in Spanish, but with Chinese language pack installed. The java language already set to en
and file encoding UTF-8
:
java -version
Picked up _JAVA_OPTIONS: -Duser.country=US -Duser.language=en -Dfile.encoding=UTF-8
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
Why? Only this exception cannot be read.
EDIT: tried to define the language as zh
, but does not work.
Use this main class to reproduce it:
public class PromotionTargetFileHandlerMain {
public static final String uploadingDir = String.join(File.separator,
System.getProperty("user.dir"), "targets_csv");
private static final File destDir = new File(uploadingDir);
public static void main(String[] args) {
createFileDestination("target.csv");
}
public static void createFileDestination(String filename) {
if (!destDir.exists()) {
try {
Files.createDirectory(Path.of(uploadingDir));
} catch (FileAlreadyExistsException e) {
log.trace("File dir already exists: {}", uploadingDir);
} catch (IOException e) {
log.error("Cannot create temp file dir {}", uploadingDir, e);
throw new RuntimeException(e);
}
}
String saveLocation = String.join(File.separator,
uploadingDir, filename
);
File saveFile = new File(saveLocation);
if (saveFile.exists()) saveFile.delete();
try {
saveFile.createNewFile(); // <--------------- here IOException
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Solution
I may found one solution: change locale in Windows 10 for non-Unicode applications in Control Panel. It was Spanish, but now I changed to Chinese, then I can see some localized message of other IO errors(cannot reproduce the same error in the question):
I found this:
...and .NET exception messages will be localized regardless of the intended recipient of the message.
So it leads me to believe that the process is like this:
- native OS level error message is always in Chinese
- and, due to OS locale setting for non-Unicode(why Java is considered non-Unicode application, I don't know), it wants to interprete it as Spanish, but failed
- so I can only see ??????
In Windows 10, you can type "Control Panel" in the search bar, and go to: Region -> Management(tab) -> non-Unicode application language, set to Chinese(or others as in your local language pack), and restart.
Of course, it affects other non-Unicode application, too.
Answered By - WesternGun
Answer Checked By - Timothy Miller (JavaFixing Admin)