2

I'm trying to get file (readme.txt) from my project folder. Don't know how to get location of project. When I say project, I mean location where my application code is written and not runtime application. I've tried getting absolute path, relative path... and it always gives me folder of runtime application. Also tried something like this.getClass() and tried to extract path or System.getProperty("user.dir"). These two also gives me path of my eclipse.../.../...runtime app. I'm making eclipse plugin, and this file is suppose to be part of my plugin, so that when user click's on button, this file opens (it's some help txt file). This is my code for opening file, problem is path.

/**
 * Help button listener. If button is pressed, help file is opened.
 */
private void listenButtonHelp() {
    buttonHelp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (Desktop.isDesktopSupported()) {
                File helpFile = new File("\\readme.txt");
                helpFile.setReadOnly();
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.open(helpFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
Squlerr1
  • 33
  • 6
  • have you tried using the (System)properties? – Stultuske May 28 '18 at 08:11
  • You mean this System.getProperty("user.dir") ?? – Squlerr1 May 28 '18 at 08:12
  • The runtime has no concept of a "project folder", if you need a file in your project, you have to put it somewhere along with the compilation result. How you do that depends on your setup, eg are you using ant, Maven, Eclipse only etc. – daniu May 28 '18 at 08:13
  • I use Eclipse only, when you say compilation result, you mean .class file's or what? – Squlerr1 May 28 '18 at 08:14
  • Use classloader's getResource() method. Refer : https://stackoverflow.com/a/1119776/4358787 – Gaurav May 28 '18 at 08:20
  • When I use class loader like this InputStream in = this.getClass().getClassLoader().getResourceAsStream("readme.txt"); output is: D:\\eclipse-rcp-oxygen\\eclipse\\..\\..\\..\\eclipse_oxygen_workspace\\TestProject\\readme.txt. In this output my first part (D:\\eclipse-rcp-oxygen\\eclipse) is path to the eclipse and second part (\\eclipse_oxygen_workspace\\TestProject\\readme.txt) is path to the project but if added (D:) in front of it. Ok I can do it like this but then I have to parse path. I'll add answer when I make it. – Squlerr1 May 28 '18 at 08:26

2 Answers2

1

It depends on where exactly the file is in your project. A clean point to put it might be ${project.root}/resources, so create a folder and put the file there. Mark it as a "source folder" in Eclipse (project properties -> build path -> source folders). Your current setup isn't a good idea because the file will not be included in your distribution by Eclipse's compile.

Now, when you compile the code, this gets copied into the target directors (bin per default); you can check by opening it in your file browser.

So to check the file is there, you can do

Path filePath = Paths.get("resources", "readme.txt");
System.out.println(Files.exists(filePath));

If you need it as a File, you can do

File readmeFile = filePath.toFile();

This reads the file from the source project folder, so it won't be much use after you run the program somewhere else.

For that, you can use the ClassLoader:

URL readmeUrl = ClassLoader.getSystemClassLoader().getResource("resources/readme.txt"));
File readmeFile = new File(readmeUrl.getFile());
daniu
  • 14,137
  • 4
  • 32
  • 53
  • I get null for readmeUrl, tried "resources/readme.txt", "/resources/readme.txt", "resources\\readme.txt", "\\resources\\readme.txt", still the same – Squlerr1 May 28 '18 at 08:51
  • When I try like this : URL readmeUrl = this.getClass().getClassLoader().getResource("resources/readme.txt"); output is: bundleresource://903.fwk984832924:1/resources/readme.txt and it can not be opened as file. – Squlerr1 May 28 '18 at 09:19
0

I found answer, this works for me:

/**
 * Help button listener. If button is pressed, help file is opened.
 */
private void listenButtonHelp() {
    buttonHelp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (Desktop.isDesktopSupported()) {
                File file = null;
                Bundle bundle = Platform.getBundle("TestProject");
                IPath path = new Path("resources/readme.txt");
                URL url = FileLocator.find(bundle, path, null);
                /*
                 * After FileLocator, I get also this, like I commented before:
                 * D:\\eclipse-rcp-oxygen\\eclipse\\..\\..\\..\\eclipse_oxygen_workspace\\
                 * TestProject\\resources\\readme.txt and before it didn't work but if 
                 * you add these lines:
                 * url = FileLocator.toFileURL(url);
                 * file = URIUtil.toFile(URIUtil.toURI(url));

                 * Like in my try bracket, it works. I guess it needs to be 
                 * converted using URIUtil.
                 * Now it finds file, and it can be opened, also works for .html files.
                */
                Desktop desktop = Desktop.getDesktop();
                try {
                    url = FileLocator.toFileURL(url);
                    file = URIUtil.toFile(URIUtil.toURI(url));
                    // file.setReadOnly();
                    desktop.open(file);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    });
}
Squlerr1
  • 33
  • 6