package declaration error depending on the IDE used - java

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:

Related

Declaring packages in eclipse

I'm new to java and eclipse and was wondering if someone could help me.
In my comp class we have a lab each week so in eclipse I created a folder called "compClass" in this file is the "src" folder and in that folder are 3 packages. lab1, lab2, lab3, one for each week we have an assignment. We're currently on assignment 3 so I'm using "lab3" package. In this package I have 2 files, "Test.java" and "MyInteger.java". These files were given to the students and we are supposed to modify them, however that's not my issue.
My issue is on those files I keep getting a compiler error that says: The declared package "" does not match the expected package "lab3" I'm not sure why this is happening as I didn't import anything. I created new java files and copy pasted the code in from a text file. I even tried typing it out by hand and got the same error. I tried dragging and dropping the "Test" and "MyInteger" files (as java files not text files) from a folder on my desktop to the "lab3" package and got the same error. How can I fix this?
P.S. On this post: https://stackoverflow.com/questions/7628686/eclipse-the-declared-package-does-not-match-the-expected-package/13444301#:~:text=java%20files%20to%20%22package%20path,All%20should%20be%20ok.&text=If%20you%20have%20imported%20an,the%20error%20till%20you%20restart.
The top answer said to find the src/prefix1 directory and right click on it. Then build path and use as source folder. If that is the answer then could someone please help me find the "src" directory I don't even really know what that is or where to find it.
Thank you!
Every Java project has a project root which can be configured using your IDE. In your case, the root is src/. If your code is inside a subfolder in the root, this folder represents the package your code belongs to, so in this case it's a package of lab3. If you want to not include any package declarations, then either place the code directly inside src or set lab3 as the root.

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

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

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