问题
Edit: My solution at bottom of post.
I have 2 projects that share the same XML/XSD data in eclipse. I've tried many different things to break the data into a separate project and just reference that instead through various methods, with not much luck. My goal is so I can click run in eclipse for each project separately and they both load from the same data.
When the project is finally built, there will be 3 jars (the two projects,+ a lib), which will be in the same directory with the xml and xsd folders. So any file references to the XSD/XMLs are currently in the format like this:
File file = new File("xml/pixtilemaps.xml");
This doesn't work in eclipse, but works during export because of the file structure is sound. I tried using virtual folders in each of the projects, but it doesn't look like runtime references to relative files use the virtual folders in the way I was expecting. I've tried adding each as a linked resource, added the project on the build path, etc, but whatever I do, it doesn't seem to treat the xml folder as a relative folder. I get an error like : file:/F:/workspace/com.myoid.editor/xsd/pixtilemaps.xsd does not exist. Because it takes it relative to the project. I've also tried to find a run configuration solution to this, but there is obviously something I'm not understanding to get this to work the way I want.
So in summary, have an xsd folder, xml folder in a 3rd eclipse project, that I want 1 and 2 to be able to access through relative file paths like
File file = new File("xml/pixtilemaps.xml");
How I fixed it:
Okay.. so.. maybe there is a better solution but this is what I came up with... In Eclipse under run configurations you can specify the working directory that the launch will run from. So I put all xml/xsd in for both projects in the same "working directory". In the run configuration for each project I point the working directory to the 3rd project, and viola, it works like I had expected.
回答1:
Did you try to reference project 3 in project 1 and 2 via project menu -> properties -> java build path -> add project?
回答2:
Add third project n the build path of first two projects as project reference
.
Use ClassLoader.getResourceAsStream in place of File
in first two projects as below:
this.getClass().getClassLoader().getResourceAsStream("/xml/pixtilemaps.xml");
来源:https://stackoverflow.com/questions/12885950/eclipse-reference-file-in-another-project