RibsGrowBack
RibsGrowBack

Reputation: 45

Eclipse get linked resource path for file loading

In my Eclipse project I have a "src" folder that's linked from a one drive folder. I have some other text files in the linked folder that I want to load with a FileReader.

How would I get this location, optimally in a way that's agnostic to whether the folder is linked or actually in the project folder. I've tried using

MyClass.class.getResource("");

But it returns me a path to the "bin" folder. I'm probably not using it right. The file I want to get is "src/de/lauch/engine/shaders/primitiveTestShader/vertexShader.vsh"

Thanks in advance!

Upvotes: 0

Views: 673

Answers (2)

RibsGrowBack
RibsGrowBack

Reputation: 45

I solved my particular issue for now but im still open to better solutions :)

public class LinkedResourceLocator {

private static Dictionary<String,String> locations;

public static String getPath(String path) {
    if(locations==null) {
        File projectLocal = new File(LinkedResourceLocator.class.getClassLoader().getResource("").getPath().replaceAll("%20", " ")).getParentFile();
        File dotProject = new File(projectLocal.getAbsolutePath()+"\\.project");

        locations = new Hashtable<String,String>();

        File[] files = projectLocal.listFiles(new FileFilter(){

            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory();
            }

        });
        for (int i = 0; i < files.length; i++) {
            locations.put(files[i].getName(), files[i].getAbsolutePath());
        }
        try {
            BufferedReader br = new BufferedReader(new FileReader(dotProject));
            StringBuilder fileContentBuilder = new StringBuilder();
            String line;
            while((line = br.readLine()) != null) {
                fileContentBuilder.append(line.trim());
            }
            String fileContents = fileContentBuilder.toString();
            Pattern p = Pattern.compile("<link><name>(\\w*)</name><type>\\d*</type><location>([\\w/:]*)</location></link>");
            Matcher m = p.matcher(fileContents);
            while(m.find()) {
                locations.put(m.group(1),m.group(2));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Can't locate .project file");
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Can't read .project file");
        }
    }
    String locator = path.contains("/")?path.substring(0, path.indexOf("/")):path;
    String restPath = path.substring(locator.length());
    return locations.get(locator)+restPath;
}
}

This class gets the linked resource locations from the eclipse .project file and then converts project local paths like "src/de/lauch/engine/shaders/primitiveTestShader/vertexShader.vsh" to these linked locations.

Upvotes: 0

Bheem  Singh
Bheem Singh

Reputation: 707

You can create resources folder like that 'src\main\resources' and put the file after that you can run your same code . hopefully it will work.

Upvotes: 1

Related Questions