0

This problem is driving me crazy. I have a file I would like to reach in my src/main/resources folder and I am trying to obtain the path via:

FileSystem fileSystem = FileSystems.getDefault();
Path path = fileSystem.getPath(AnalysisEngine.class.getResource("/models/10_NB_7dev_2.model").getFile());

However, I keep getting the following error:

Illegal char <:> at index 2: /C:/Users/...(the path is here)/models/10_NB_7dev_2.model

As you can see, the path returned has '/' before C:, which ruins everything. What is the reason and how could this be fixed? Is there an alternative with java.io package?

I am using Windows 8 - 64 bit OS, if it helps.

3 Answers 3

2

The URL returned by Class#getResource(String) contains a preceding /.

/C:/Users/...(the path is here)/models/10_NB_7dev_2.model

That's just how URLs work. Then the FileSystem tries to parse that, but it makes no sense to it that there is a : character in the mix, so it throws an exception. In other words, getPath() is trying to create a path, not a url. You cannot have a : character in a Windows (possibly linux as well) path, unless it is directly following the Drive name as the first two characters of the path string.

The solution here is not to use the path of a classpath resource. A classpath resource might not come from the filesystem directly, it might be inside a jar.

Sign up to request clarification or add additional context in comments.

1 Comment

@hiro2k Let me rephrase that. You can't have : anywhere but in a Windows file path after the drive name.
0

...(the path is here)/models/10_NB_7dev.model

in your code you put:

("/models/10_NB_7dev_2.model").

Are you meaning to put a _2.?

1 Comment

No, that was a typo. The error is because of backslash appearing before C:.
0

If you are not worried about using the default filesystem (e.g. if you aren't using an in-memory filesystem for testing) then you can do:

URI uri = AnalysisEngine.class.getResource("/models/10_NB_7dev_2.model").toURI();
Path path = Paths.get(uri);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.