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?
First way
Right click on ProjectA and export as jar file.
Right click on ProjectB, Goto Build path -> Configure build path -> Libraries
Add external jar of ProjectA
Second Way,
Right click on ProjectB, Goto Build path -> Configure build path -> Projects
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
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.
Related
I have two files:
MyProject/src/main/java/foo_package/bar_package/MainClass.java
MyProject/src/gen/java/foo_package/bar_package/OtherClass.java
In both of those classes the very first line is:
package foo_package.bar_package;
If I call:
OtherClass foo = new OtherClass();
It cannot resolve symbol OtherClass. Why is that?
What I've tried:
Rebuild project
Invalidate cache/Restart
Reimport project
Delete .iml files and .idea folder and import everything again
It looks like there is a problem with the path of the last class:
MyProject/src/main/java/foo_package/bar_package/MainClass.java
MyProject/src/gen/java/foo_package/bar_package/OtherClass.java
If your classes have the same package (package starts after ../java/) but they are not part of the java build path, then the IDE won't recognise them as valid.
Try moving your OtherClass.java to the package where the MainClass.java is. Doing this should eventually solve your problem.
PS: be aware about the source folders of your project (most of the time main is the source folder by default and it's enough but there may be other source folders, generally added manually).
Make sure that your IDEA source folder is java, not src (for both java folders inside /gen/ and inside /main).
The sources root is marked as a blue directory in "Project" window (Alt + 1).
I am a total beginner in java and need help. I have file "OOP_Introduction_Examples" and I need to import examples from it in eclipse.
Here is what I have when I open that file:
What excatly I need to do to open it in Eclipse (Which one of these)? Should I just import -> .project? I tried something, but doesn't look good.
I can't comment so I put this as an answer.
If you choose "select root directory" you are gonna import a directory which contain the project (files shown in the picture). So, you choose the directory that contains files and folders from the picture (bin, src, .priject ....).
For exemple, if you have the next project directory/file tree, click on myProject directory and valid your choice.
myProject
|-.settings
|-src
|-bin
|-classpath
|-project
In the 'lib' directory of my Eclipse project, I have a jar file, 'foo.jar' which contains a class file 'Foo.class' in the (default package) of 'foo.jar', which I have added to my build path in Eclipse (using Project->Properties->Java Build Path->Libraries->Add JARs...). Now, 'foo.jar' appears under the 'Referenced Libraries' section in Eclipse's Package Explorer.
In the 'src' directory of my project I've got a file 'bar.java' whose first line is:
import foo.Foo;
In the body of 'bar.java', the code can use the contents of 'Foo.class' and all appears well, except I get exactly one error, on the import statement: "The import foo cannot be resolved", so the program won't run; it's the only error in 'bar.java'.
What's the proper way to take care of this?
(I have cleaned the project and refreshed it.)
import xxx.Foo; where xxx is the package name, not the name of the jar
If it's the default package, just try using Foo? You won't need the import in that case.
Thanks m8 ;)
I am using C#(Unity) to call java function in jar. My class is like:
Class A{
auth(){
}
share(){
StatusesAPI mStatusesAPI;
mStatusesAPI.upload("hey", bitmap, null, null, mListener);
........
}
}
. auth() can be call in c# and works correctly if I comment everything related to StatusesAPI, such like,
//private StatusesAPI mStatusesAPI;
//mStatusesAPI.upload("hey", bitmap, null, null, mListener);
Now, because I need this class(StatusesAPI ) to do something, I have to uncomment it and then import it. This Class is in project B and it depends on other classes in project B. So I add project B in Eclipse JAVA BUILD PATH. NO compile error. I enter
jar -cvf plugin.jar *
in cmd under folder myproject/bin/class
and I move the plugin.jar to unity /bin folder.
But when c# call auth(), error happens.
I doubt StatusesAPI code is not include into my plugin.jar, that makes class A all function don't work. I have tested calling my other class functions in plugin.jar, they works.
Any one know how to deal with this? How to make both auth() and share() works.
why don't you try import the jar file by
right click on your project main folder.
select Build Path then configure build path.
Under libraries choose the required jar file usi(plugin.jar) by Add External Jar's(go to the destination folder).
Click ok and once restart the IDE.
Hope this helps you
I am trying to import some existing projects into Eclipse.
The structures for their packages is:
Project/
/src
/java
/a
/b
/c
Once imported in the package explorer I see:
Project
src/java
--a
--b
--c
- AClass.java
This is ok, since the classes e.g. AClass.java are defined in package: a.b.c
But in one project the structure (once imported) becomes:
Project
src
--java
--a
--b
--c
- AClass.java
And that causes the error that AClass.java is defined to be in package a.b.c but it is actually under java.a.b.c
Why is this happening? Why in this specific project java is not ignored as part of package?
Thanks
How are you creating the Eclipse projects? It sounds like you just need to put "java" as a root on on the source path here, instead of "src". You can do this by editing the build path after the import process, of course.
Remove the existing source folders first. -right click -> menu -> build path -> remove from build path
then
Right click on the source folder. build path -> use as source folder.
Seems like your settings are pointing to the parent of the source folder so src is recognized as package by eclipse.
Wrong package name when using automatically added imports in Eclipse
call the package on the top of your import statements,
like if your class is in java/main/org/goal/Main.java
then the path is package java.main.org.goal;
else do Ctrl +1 and it suggest some quick help
import the necessary package from that
Use this sentence import java.io.*; at the top of java file. Otherwise, you have to create package folder.
Import statements:
In Java if a fully qualified name, which includes the package and the class name, is given then the compiler can easily locate the source code or classes. Import statement is a way of giving the proper location for the compiler to find that particular class.
For example, the following line would ask compiler to load all the classes available in directory java_installation/java/io :
import java.io.*;