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(); } } } }); } 
6
  • have you tried using the (System)properties? Commented May 28, 2018 at 8:11
  • You mean this System.getProperty("user.dir") ?? Commented May 28, 2018 at 8: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. Commented May 28, 2018 at 8:13
  • I use Eclipse only, when you say compilation result, you mean .class file's or what? Commented May 28, 2018 at 8:14
  • Use classloader's getResource() method. Refer : stackoverflow.com/a/1119776/4358787 Commented May 28, 2018 at 8:20

2 Answers 2

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()); 
Sign up to request clarification or add additional context in comments.

2 Comments

I get null for readmeUrl, tried "resources/readme.txt", "/resources/readme.txt", "resources\\readme.txt", "\\resources\\readme.txt", still the same
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.
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(); } } } }); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.