4

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?

2
  • So, the problem is eclipse won't let you specify relative path to source? Have you tried changing it manually in .classpath file? Commented Jun 25, 2012 at 14:34
  • Yep. When I refresh the project it just complains that it isn't a valid path. Tried every combination of ${MYDEV} and $MYDEV I could thnk of too. Interestingly the help section on 'Source Attachment Properties' suggests a variables option would be available, but there isn't when using 'Java Source Attachment'. Commented Jun 25, 2012 at 15:00

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. However, you still appear to need to use an absolute path to reference the source, even if it now points to a symbolic link. Thus the same path won't work across linux and windows. Also in windows you have to use the 'mklink' command for the link to be useable (shortcuts don't work) which typically requires administrator access.
The symlinked directory is relative to the eclipse project, so he can reference it relatively, instead of absolutely. If mklink requires administator access on windows (and if he's using windows), he'll have to sudo or whatever the equivalent is. I imagine as a developer, he's probably got root access to his own computer.
Sorry, yes this does work if you remember to refresh the project! The workaround is therefore to create a link in the project area and then to link it via Workspace Location. Many thanks.
0

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.

3 Comments

OK that might be workable, thanks. I still don't understand why it appears to treat these things differently i.e. why I can use a variable to reference the jar but not it's source.
Maybe the devs didn't think of this use case before. This is probably worth posting on bugs.eclipse.org/bugs
Good idea. I've sent a bug report, progress can be followed here 383491.
0

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.

1 Comment

Yes I can see that working, but it does appear to be working around a potential defect in Eclipse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.