Link to java source from another project?

I use the same java class in one of my main applications and in another project. This class is still under development, and therefore I cannot always compile it as a jar and add it as a library.

How to connect this * .java class in another project with the fact that it is used as if it belongs directly to this project?

I tried to go to project properties> Java build path> link source. I can refer to the source folder of another project. Great, but the eclipse always complains about the wrong package path. It is strange that the generic java class in the new project is hosted by unter (default package) , while I expect it to be displayed only in the same way as in the main project. And Eclipse claims to remove the package path. If I do this, of course, the main project will complain about the missing path there.

What am I doing wrong? How can i sync this?

+4
source share
6 answers

Assuming these two projects are in the same workspace, go to the project properties that you want to access 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. 
+9
source

I was a bit late with the answer, but this might be useful to someone:

In 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
There you are!

However, if you just exchange source files from a neighboring project in the workspace, you can simply import them as you import any other classes:

 import other.project.class; 

The latter works for me, if it is not for you - I can search for settings.

+5
source

An alternative solution to use another project can be implemented

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

Correct me if wrong

+2
source

I had the same problem and just renamed the package in the same way as the other project package.

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

Note that the folder you create and the one you want to link should be the same (or at least this is the only way to find)

+1
source

i also has the same problem, when I include sources from another project as link sources in a specific folder, it will bind to the package name because I do not bind it to the root folder / src, but let say / src / com

My only solution is to simply create a folder (there is no need to create a project) and place your library there, then you link the sources from there (so the package should not follow the rules of the / src folder)

0
source

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

 import toolsof.loveliness.*; 

no matter what toolsof.loveliness is in another project.

0
source

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


All Articles