1

I know this question must have been asked before and i have gone through the solution but that didnt seem to solve my question. Suppose I have 2 projects in eclipse .ProjectA and ProjectB and ProjectA has file named FileA in package PackageA and ProjectB has a file named FileB in PackageB.Suppose i want to use a function of FileA in projectB.Is there any import statement which will allow me to do so.I dont want to copy paste the entire fileA in projectB.I just want something like

import ProjectA.packageA.fileA
class fileB
{
//calling function from fileA
int somethingsomething = fileA.somefunction();
}

I have done things like go to project right click and click on import and import the file.But i dont want the file to be present in my filelist of ProjectB.Please ,Can someone help me with this problem.Is this even possible?

2 Answers2

3

First way

  1. Right click on ProjectA and export as jar file.
  2. Right click on ProjectB, Goto Build path -> Configure build path -> Libraries
  3. Add external jar of ProjectA

Second Way,

  1. Right click on ProjectB, Goto Build path -> Configure build path -> Projects
  2. Click on Add, select ProjectA from list.

After this, you will be able to import classes in ProjectB which are defined in ProjectA by,

import packageA.fileA

Read more at : Java build path

Not a bug
  • 4,286
  • 2
  • 40
  • 80
  • The second way is great. Looked for such thing a long time. Why did i think that one could not just include a .java source file? So i tried to import a .java activity source file which i use in many projects but always moved from project to project. This works all fine. If i introduce an error in the source it won't compile. So it is definitly used. In the manifest i changed the package name for the activity. It compiles and links ok but at runtime i get a `java.lang.ClassNotFoundException` mentioning `packageA.fileA`. Is there something more to it? – greenapps Sep 22 '14 at 07:48
  • `you will be able to import classes written in ProjectB which are defined in ProjectA`. Now that i look more to that i do not understand. "written in projectB" ? What would that be? Didn't you mean: "you will be able to import and use classes in ProjectB which are defined in ProjectA" ? – greenapps Sep 22 '14 at 07:51
  • It was a typo, corrected now !! I exactly mean that what you said. – Not a bug Sep 22 '14 at 09:48
  • I am not familiar with android but you can get help for `ClassNotFoundException` from [this question](http://stackoverflow.com/q/17408769/1686291). – Not a bug Sep 22 '14 at 12:04
  • Mmmm was not successfull so tried 'First way'. That gives me a `NoClassDefFoundError`. Googling around it looks as if i'm not the first seeing such error. I know now your advices don't work for me out of the box. That is a pitty. – greenapps Sep 22 '14 at 13:33
0

Well your Project B needs the code of File A to execute it. You can either copy the file, or a .jar, or you copy the relevant code into the new project. You cannot just reference a piece of code but not add it to your Project in some way. When building your project to a .jar for example, this piece of information must be present and accessible for the program.

PKlumpp
  • 4,913
  • 8
  • 36
  • 64