Maciaz
Maciaz

Reputation: 1234

Spring cannot find the file via specified path

My files hierarchy:

>resources:
    >static:
        >css:
        >json:
            >networks:
                >network-list.json
        >js:
        >img:

I've tried to create a new file via:

File jsonNetworkDetailsFile = new File("/json/networks/network-list.json");
File jsonNetworkDetailsFile = new File("static/json/networks/network-list.json");
File jsonNetworkDetailsFile = new File("../json/networks/network-list.json");
File jsonNetworkDetailsFile = new File("../../json/networks/network-list.json");
File jsonNetworkDetailsFile = new File("/json/networks/network-list.json");

...and some more. None of it works.

I'm still getting the

java.io.FileNotFoundException: the system cannot find the path specified

What's the proper way?

EDIT

Found a solution. Had to include full path to the file like:

 File jsonNetworkDetailsFile = new File("src/main/resources/static/json/networks/Turtlecoin/turtlecoin-pools.json");

EDIT2

As TwiN stated - it's impossible to reference a file through File object as soon as the app is packed into .jar. A proper solution would include:

InputStream jsonNetworkDetailsFile = new ClassPathResource("/static/json/networks/network-list.json").getInputStream();

Upvotes: 1

Views: 9718

Answers (4)

TwiN
TwiN

Reputation: 3834

InputStream is = new ClassPathResource("/someFile.txt").getInputStream();

where /someFile.txt is in your resources folder.

As mentioned in the documentation for ClassPathResource:

Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

In other words, you'll want to use the getInputStream() method for your case:

InputStream is = new ClassPathResource("/someFile.txt").getInputStream();
try {
    String contents = new String(FileCopyUtils.copyToByteArray(is), StandardCharsets.UTF_8);
    System.out.println(contents); // do something with the content here
    is.close();
} catch (IOException e) {
    e.printStackTrace();
}

I'm mentioning this because ClassPathResource also has a getFile() method.

For more details, see reference

Upvotes: 3

Oreste Viron
Oreste Viron

Reputation: 3805

Try something like this :

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("classpath:static/json/networks/network-list.json").getFile());

You could also use :

@Value(value = "static/json/networks/network-list.json")
private Resource myFile;

And then :

myFile.getInputStream()

(will only work on a class annoted with @Component, @Service... etc)

Upvotes: 1

Oguz
Oguz

Reputation: 1926

I think you should give the exact location to File object. Another solution:

File currDir = new File(".");
String path = currDir.getAbsolutePath();
// String path = "C:\\ExternalFiles\\"; // Or you can give staticly
File jsonNetworkDetailsFile = new File(path);

Hope it helps.

Upvotes: 0

Elarbi Mohamed Aymen
Elarbi Mohamed Aymen

Reputation: 1710

you can try this to load files from ressources :

ClassLoader loader = Thread.currentThread().
getContextClassLoader();
InputStream configStream = loader.getResourceAsStream("/static/json/networks/network-list.json");

Upvotes: 0

Related Questions