Eclipse - link file in another project

Edit: My decision is at the bottom of the post.

I have 2 projects that share the same XML / XSD data in an eclipse. I tried a lot of different things to break the data into a separate project and just refer to them in various ways, but I was out of luck. My goal is so that I can click Run in Eclipse for each project individually, and they are both loaded from the same data.

When the project is finally assembled, there will be 3 flasks (two projects + library), which will be in the same directory as the xml and xsd folders. Thus, any file references in XSD / XML currently have the following format:

File file = new File("xml/pixtilemaps.xml"); 

This does not work in Eclipse, but it works during export because the file structure is healthy. I tried to use virtual folders in each of the projects, but it doesn’t look like runtime links to relative files use virtual folders as I expected. I tried to add each of them as a related resource, added the project to the build path, etc., but no matter what I do, it looks like the xml folder is not considered as a relative folder. I get an error, for example: file: / F: /workspace/com.myoid.editor/xsd/pixtilemaps.xsd does not exist. Because it refers to the project. I also tried to find a solution for the launch configuration, but obviously I don’t understand something in order to make it work the way I want.

So to summarize: I have the xsd folder, the xml folder in the third Eclipse project, and I want 1 and 2 to have access to them through relative file paths, such as

 File file = new File("xml/pixtilemaps.xml"); 

How I fixed it:

Ok ... so ... maybe there is a better solution, but this is what I came up with ... In Eclipse, under the launch configurations, you can specify the working directory from which the launch will be launched. So I put all xml / xsd for both projects in the same "working directory". In the launch configuration for each project, I point the working directory to the third project, and the alt works as I expected.

+4
source share
2 answers

Add the third project n to the build path of the first two projects as a project reference .

Use ClassLoader.getResourceAsStream instead of File in the first two projects, as shown below:

  this.getClass().getClassLoader().getResourceAsStream("/xml/pixtilemaps.xml"); 
0
source

Have you tried to link to project 3 in projects 1 and 2 through the project menu β†’ properties β†’ java build path β†’ add project?

0
source

Source: https://habr.com/ru/post/1439685/


All Articles