5

I'm using the same java class in one of my main application, and in another project. This class is still in development and I therefor cannot compile it always as a jar and add it as a library.

How can I link this *.java class in another project to that it is used as if it belongs directly to that project?

I tried going to project properties > java build path > link source. I can make reference to a source folder of another project. Fine, BUT always eclipse complains about wrong package path. The strange thing is, that the shared java class in the new project is placed unter (default package) whereas I would it expect to just be displayed with the same path as in the main project. And Eclipse claims to remove the package path. If I do so, of course, the main project will complain about a missing path there.

What am I doing wrong? How can I synchronise this?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

6 Answers6

12

Assuming these two projects are in the same workspace, go to the properties of the project where you want to refer to the java class and do the following:

Project Properties => Java Build Path => Projects => Add => Check the box against the project that contains the Java file.
Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • there is one problem with this solution: if I export the second project, then ALL classes from the first project are taken into this as well! I only want to link 2 classes of about 20... – membersound Oct 01 '12 at 12:31
  • Why do you want to export? Just add a reference. While packaging, you can choose to only package the required files into a jar and include that jar in the classpath while running the depending app. – Vikdor Oct 01 '12 at 12:41
9

I'm a bit late with the answer, still it may be helpful to someone:

In the Eclipse:
1) right-click the destination folder -> New -> File
2) click "Advanced>>" and set "Link to file in the file system"
3) Browse, select the source file and click "Finish"
Here you are!

Still if you just share source files from neighbor-project in the workspace you may simply import them like you import any other classes:

import other.project.class;

The latter works for me, if it doesn't for you - I may look for settings.

sberezin
  • 3,266
  • 23
  • 28
3

I was facing the same problem, and I just renamed the package to the same as the other project package.

  • Right click src folder -> New -> Folder
  • Click Advanced and check "Link to alternate location(Linked folder)"
  • Browse, select the the folder you want to link

Note that the folder you are creating and the one you want to link have to be named the same (or atleast thats the only way I could find )

centenond
  • 1,170
  • 1
  • 8
  • 23
2

Alternative solution for using the other project can be done

In eclipse export the project into jar and import jar as library to any other projects it should work fine.

Correct me if 'm wrong

HHH
  • 37
  • 5
0

i also having the same problem, when i include sources from other project as link sources at specific folder, it will mess with the package name since I'm not linking it at the root folder /src but let say /src/com

my only solution is just create a folder (no need to create project), and put your library there, then you link sources from there (so the package doesn't have to follow the rules of /src folder)

0

Importing using import statements works best for me; otherwise you run into the package folder hierarchy/package naming problems mentioned above. When importing, do not use the project name in the package path. For example, if you have an Eclipse project named "WorldPeace" with a package called toolsof.loveliness, and you want to use the package toolsof.loveliness in another project called "FamilyCounseling", try using the following import statement in your classes:

import toolsof.loveliness.*;

without regard to the fact that toolsof.loveliness is in another project.