1

I have created a plugin. It needs to access 2 files from res folder. Now the trick here is, res folder is not inside plugin. For example, i have published plugin locally, so it now visible under any project you create, inside Tools Menu. What i want is, any new project i create, it will have 2 files facility.json & groups.json inside res folder, now my plugin should be able to access these files. I have tried different approach like as shown below, but it always fails, saying "File not found" exception. If anyone can help me out in this. Its appreciated. Thank you.

private static File getFileFromResource(String fileName) throws URISyntaxException {

    ClassLoader classLoader = Validator.class.getClassLoader();
    URL resource = classLoader.getResource(fileName);
    if (resource == null) {
        throw new IllegalArgumentException("file not found! " + fileName);
    } else {

        // failed if files have whitespaces or special characters
        //return new File(resource.getFile());

        return new File(resource.toURI());
    }

}

private static InputStream getFileFromResourceAsStream(String fileName) {

    // The class loader that loaded the class
    ClassLoader classLoader = Validator.class.getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(fileName);

    // the stream holding the file content
    if (inputStream == null) {
        throw new IllegalArgumentException("file not found! " + fileName);
    } else {
        return inputStream;
    }
}
Ankit Ostwal
  • 1,033
  • 3
  • 14
  • 32

1 Answers1

0

That's the neat part, you don't. you have to add them by yourself each time you make a new project since res folder isn't a plugin, instead res is for resource (assuming you use res as resource)

Try this by manually add each facility.json & groups.json with

InputStream IS = getClass().getResourceAsStream("/res/file name");

or

InputStream IS = getClass().getResourceAsStream("root/dir/file name");

your plugin already published locally so maybe adding address might be help

if still not, try to save your clean project that only have facility.json & groups.json inside res folder as your project template. it depends on your IDE,

Intelij IDEA -Jetbrain : https://www.jetbrains.com/help/idea/saving-project-as-template.html

NetBeans IDE -Apache : https://netbeans.apache.org/tutorials/nbm-filetemplates.html

as my experience, im creating my own netbeans template for new project

SilvaneUX
  • 9
  • 3