3

I have a weird issue that I'm experiencing on my laptop;

Laptop: OSX Version: 10.11.6 - El Capitan

Java: java version "1.8.0_91" Java(TM) SE Runtime Environment (build 1.8.0_91-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

I'm trying to use ClassLoader#getResource to open a filename of the following format app-2016-08-13T22:55:00Z.1.log.gz.

I've placed the file under /src/main/test/resources however I am always getting back null.

If I rename the file to be anything without the colons the code works. What's going on?

@Test
public void testColonFile() throws Exception {
    String resourceFilename = "app-2016-08-13T22:55:00Z.1.log.gz";
    InputStream is = TestClass.class.getClassLoader().getResourceAsStream(resourceFilename);
    //InputStream is null here!
 }
Setheron
  • 3,520
  • 3
  • 34
  • 52
  • I suggest removing `toURI()`. – Code-Apprentice Aug 16 '16 at 01:09
  • There are a lot of discussions about HFS+ and colons. The thing is the `:` was used as a filepath delimiter. So you should avoid to use `:` in filenames. So, the **system just can't find specified file** See also: here is the [wikipage](https://en.wikipedia.org/wiki/Filename#Comparison_of_filename_limitations) with comparison of filesystems and their limitations. – maxkoryukov Aug 16 '16 at 01:17
  • 1
    Your code is invalid. Resources are not files. They can be in a JAR or WAR file, not in the file system. You should use the URI directly, or `getResourceAsStream()`. – user207421 Aug 16 '16 at 01:26
  • if I use just `new File(absolutePath)` it works fine. So it can clearly find it. – Setheron Aug 16 '16 at 16:36

1 Answers1

0

My guess is toURI is URL-encoding the colon, and the constructor to File is not expecting that.

Please add a line like

System.err.println(Resources.getResource(resourceFilename).toURI());

to see.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • It is failing before the `toURI` . `InputStream is = BlingPublisherTest.class.getClassLoader().getResourceAsStream(resourceFilename);` is also returning null for an inputstream – Setheron Aug 16 '16 at 16:36