Java: FileOutputStream("NUL:") not working after Java upgrade
Asked Answered
C

1

8

On Windows, NUL is the null output device similar to /dev/null on Linux.

With Oracle Java 8 Update 331, trying to get a new FileOutputStream("NUL:") throws an exception. Previously (Java 8u321) it worked fine.

The problem seems to be the colon:

  • new FileOutputStream("NUL") - OK
  • new FileOutputStream("NUL:") - exception

Can anyone point me to docs or JDK sources regarding this change? I can't change the code itself because it is in a 3rd party lib (xnio-api).

try
{
  new FileOutputStream("NUL:");
  System.out.println("OK");
}
catch (FileNotFoundException e)
{
  System.out.println(e);
}
Coldiron answered 22/4, 2022 at 8:57 Comment(4)
Just for reference: #313611Grundy
Relevant entry in Oracle Bug Database: bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8285445Coldiron
Fixed in Oracle Java 8u333Coldiron
And for completeness, it's also fixed in the library; NUL: has been replaced by NUL in xnio-api 3.8.7.Final via ticket XNIO-404Pavlish
H
13

I suspect this is the offending change.

Apparently it tries to avoid accessing ADS (alternate data streams), but seems to "accidentally" also prevent access to device-files like this.

If that's correct, then you can try setting the system property jdk.io.File.enableADS to true to re-enable the old behaviour.

Hallucinatory answered 22/4, 2022 at 9:3 Comment(3)
Thank you, it works. Now I need to evaluate the implications of having ADS enabled... :)Coldiron
Maybe it helps. I used the workaround in my wildfly environment, but was not working. E.g: I started wildfly like "standalone.bat -Djdk.io.File.enableADS=true --server-config=my.xml" But I found, the system property was for the application and not for the JVM. Adding the system property to standalone.conf.bat like: set "JAVA_OPTS=%JAVA_OPTS% -Djdk.io.File.enableADS=true" This was passed to the JVM. Standalone.conf looks is passed to the application level (based on order) (I'm not a wildfly expert, maybe the are more elegant ways.)Roderica
-Djdk.io.File.enableADS=true works for me ,jdk11 on windows11 , thank youWordage

© 2022 - 2024 — McMap. All rights reserved.