简体   繁体   中英

Can I refer to a Java source path using a variable in Eclipse

Background

I'm using an Eclipse 4.2 (Juno) release to build a plugin for a Java application. The source code and classes for my plugin all reside within the project workspace. The application jar and its source code are, for various reasons maintained outside of the eclipse workspace and the application jar is produced by another build mechanism.

This isn't a problem as I have referenced the application jar file in my project using drag and drop and the ' Link to files ' option specifying it ' Create link locations relative to : MYDEV ', where MYDEV is a Linked Resource Variable I have created for each Eclipse installation. This lets me use the same eclipse project on multiple machines where the path to the application jar varies, but is always the same relative to MYDEV.

However, I cannot find a way to associate the application source code with this jar unless I use an absolute path. I only want access to the application source for debugging purposes.

Question

In Eclipse, how do I attach java source to a referenced jar in a way that allows a project to be used on multiple installations where the referenced source code has been relocated?

If the source is outside the proyect, then there's no way to access it without an absolute path.

The best solution is using a symlink inside the eclipse project directory to the actual source folder; eclipse won't notice the source is outside the project directory and everything will work fine, without having to relocate the source.

This is the only way I was able to do what you are suggesting (do not know if there are better ways).

Package a JAR file that contains the source code of the JAR file. Place it inside your project in a folder (you do not need to add it to your class path)

In the eclipse project right click on the JAR file and go to "properties" and then to "Java Source Attachment" From there select "Workspace" button and pick the jar file with the source code we added from the above step.

So the source attachment path will always be relative to the project. That way if you share the project via SVN, GIT or whatever, the source will always be available and it'll work if the user is running eclipse on Windows or Linux.

I've had a similar problem to you where absolute paths were a problem for people using different OS and not even using mapped drives via Samba helped that much.

Hope this helps.

The way I have solved this problem is to use a use defined library in eclipse. Here the steps that you can use to solver this problem.

  1. Create a simple project in eclipse in the directory that contains the jars and the source code files.

  2. Define a java user library add the jars to the user library and for each jar in the library specify the location of the source. If the source files are in a project that is in the workspace then the path will be relative to the workspace folder.

  3. Export the user library as an .xml file

When another use wants to setup an eclipse workspace with the same setting as yours they will do two things.

  1. Import the project that contains the jars and the sources into their workspace.

  2. Import the user library into their workpsace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM