How do I import java project in eclipse? - java

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

Related

package declaration error depending on the IDE used

I have created a program with Dr.Java with the following package declaration:
dir \program -> package program:
dir \program\model -> package program.model;
dir \program\view -> package programa.view;
dir \program\controller -> package program.controller;
Now, I've tried to import this project in Eclipse (using the same folder where the .class files are as root folder for the project) but it gives me errors like this one:
The declared package "programa.model" does not match the expected package "model"
If I remove "program." from the package declaration, it works in Eclipse but not in Dr.Java.
The reason why i want to use two IDEs is beacause i am working on two different computers. An old laptop where I use Dr.Java and another newer computer where i can use an IDE with more features (like Eclipse).
Could somebody tell what am I doing wrong?
Seems like you are using program folder as root folder. It should be the parent folder of program folder.
In following image of folder structure, you should use JavaWorkspace folder as root folder:

Error when importing jar file in Eclipse (even after adding it to the build path)

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 ;)

how to import file from one eclipse project to another

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.

How to generate package structure from standalone *.java source files?

I've been searching for answer for my problem but I can't find some relevant information, so I'm asking. I've directory which contains thousands of Java classes with source code (*.java files). Each of those files contains information to what package the file belongs, its classnames + code itself of course. I need to find some function of the Eclipse IDE (or maybe of another IDE) which is able to reconstruct packages under the 'src' directory based on the information in the class files and bring to me a good Java project structure so the restored packages and its classes can be easily imported into a new Java project then. Creating the structure of packages manually would take me (maybe) lot of days...
For clarification:
I have:
directory which contains: 1.java, 2.java, 3. java, n.java...
I need:
directory which will contain:
[src] -> [package_1] -> [1.java, 2.java, etc...]
...
[src] -> [package_m] -> [3.java, n.java]
I think this must be possible somehow as the each class file contains information to which package it belongs actually.
Just if you find no better solution: At least you can import all sources into a project in Eclipse and have Eclipse move each file to the right package by means of using the quick fix for each problem. You still need to press 3 keys per wrong package declaration, but it saves you from fiddling with files and folders.

Problem in packages in importing in Eclipse

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.*;

Categories

Resources