Problem in packages in importing in Eclipse - java

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

Related

How to import a class that i wrote in java

So there is a folder in which there is the main.java file and a folder in which is a class,
how to import the class in main from the folder?
Direct folder import can't be achieved in java.
Following can be done
Package the other java class as jar
Include that jar in classpath of main program
Import the other java class in main program
Given your question, I'm assuming that you're a beginner. Here are the steps to follow for Eclipse IDE:
Under the class (which you want to import) go to: File -> Export -> Java -> Jar file.
Choose the resources to export and select a destination for the jar file.
In the project where you want to import the file, go to: Java Build Path -> Libraries -> Add External Jar -> Choose the jar file.
In the class where you need to use the file, write: import com.xxx.yyyy; (Package name).
Suppose we have the following:
LibA.Package1
SomeClass.class
LibA.Package2
Main.java
In your main file, you will need to use the full import path in the Main.java
import LibA.Folder1.SomeClass
See the following question for reference

Java classes are in the same package (different directories) but they can't access each other

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

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 importing package in Java

I am trying to sort my classes into packages but i can't import them.
My files are in the following folders:
- .java files are in C:\Java\Code\src\my\app\Timer
- .class files are in C:\Java\Code\compiled\my\app\Timer
In my class (timer) i've added package my.app;
Also, I have setted the CLASSPATH to look in both src and compiled folders.
Then, I have another folder where I put my "bigger" projects in:
- C:\Java\Projects\myProject
The problem is that when I try to import the class Timer into MyProject using import my.app.*; all I get is:
Error: package my.app does not exist
Culd you please give me a hand?
PS. My IDE is Dr.Java
I have found the problem.
It appears that Dr.Java ignores complitely the CLASSPATH variable. It is necessary to set in preferences where are the .class files.

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

Categories

Resources