Imports in a source folder: The import cannot be resolved - java

I'm trying to import a package to a class to make some tests creating a particular class and to instance some objects.
So, I create a source folder (asdsad) and put a class in there, after I tried to import Produtos.GestaoDeContrato.Mapeamentos.Telas.* but doesn't work.
Is it possible to do this?
UPDATE 01
Thanks for the answers, I tried to do this, but I don't know if I did what you said, that is correct? Because doesn't works
Please, pardon my english.

If the class you are trying to instantiate is in a separate project as I can see, then you need to to add this class to your current's project class path.Or you can place the class in the same project but in a different package !

You need to place all the java classes in the src folder of the project in eclipse unless you are trying to import from the jar file
but if you have multiple projects you can go to properties>build
path> Libraries > Add Class Folder > The select the required java
package of the proj you need.

Related

How to refer to class from inside package in Java

I have maven project which src folder contains 2 packages and 1 class outside this packages.
Inside MainWindow i want to create ClientConnection instance but it cant resolve symbol 'ClientConnection'. Is this possible to create this instance without moving file inside 'GUI' package ?
Create a new package (e.g. connection) in java folder and move ClientConnection to package conenction. In your MainWindow add
import connection.ClientConnection;
Classes in the default package cannot be imported by classes in packages.
You need to create a package for all your classes. It's best practices.

How to import a java package in Android Studio

I'm new in android, and I want to import the jfftpack to my project in android studio anda i don't know how to import it.
The original code was import ca.uol.aig.realdoublefft and I'don't know where to put the java files. I've tried to put them to a libs folder.
here's the jfftpack source code:
If it's an existing code library, I'd recommend adding a module for it. Probably the easiest way is to use the File menu command to add a new module, let it create a plain Java (non-Android) module for you, remove the sample class it puts into the module, and then copy your files over into it and tweak it. The module wizard will take care of setting up the directories and build files for you, and you can look at it later and see what did.
Without pressing Alt + enter to import the Packages?
There is some steps like
1)File --->Settings --from left pane we find editor---->General--->Auto Import
There
Insert imports on paste as ALL not ask
And put check mark for all the Above three Fields
1)Show import popup
2)Optimize the imports on fly
3)Add unambiguous imports on the fly
Click Apply and ok
Happy Coding......
If you have the sources import them in the src folder. Is the simple thing that you can do.
I suggest you to use the command line rather than Android Studio interface.
If you work on Linux or MacOS, try to copy the package to your working direcotry like:
cp -r PACKAGE_NAME PATH_TO/AndroidStudioProjects/PROJECT_NAME/app/src/main/java/
I am also new and had the same problem. I didn't have the package instruction at the top of the java file package com.example.myapp

Configuring IntelliJ project

My problem is very simple to describe.
I've made a Class named Car and I let IntelliJ build the Car.class file.
After that I've made another Project which has a dummy class called Main which wrapps this Car object.
In the project structure I've added as an external library the folder which contains Car.class.
So basicly after declaring the Car member variable in my Main dummy class, IntellJ normaly suggests me to import Car which is shown in the picture below.
Picture1
When I choose to import Car class it directly writes down the package before the Class name (not the usual import statement) and the import fails as shown on this picture.
Picture2
(note: the reason it states org.dino.test.pojos is because Car class package is org.dino.test.pojos)
The project setup is shown below.
Picture 3
Does someone know how to setup this project structure so such things work?
The fact that IntelliJ has marked the org directory as the library home is suspicious- I suspect that you have defined the classes in the library as the org subdirectory rather than the JAR or classes directory itself.
You also have a folder structure that is inconsistent with your package structure.
When you define the library, select the testLibrary directory/JAR rather than one of the directories inside it. Also, ensure that the folder structure inside your library matches the Java package of the class within it.

How to use classes from the .jar lib added to netbeans project?

Can anybody help me out in this?
what I have is a abc.jar file with me. It contains ABC.class file inside it. I added the jar file to netbeans project Libraries. But I am getting an error when I write
ABC a=new ABC();
Error :
"can not find symbol class ABC"
any help?
Edited :
also I am able to see the structure of ABC class when I click on the ABC.class file inside the library.
Remember to import it with an import statement. You should probably read the documentation that follows the jar file. Hopefully there's an example of usage - mostly there is.
The answer :
I found out that if the jar has only default package you won't be able to import classes inside it. You need to have packages other than default inside the jar to import it. You can only import non default packages.
the thing with default packages works fins in eclipse, but not in netbeans.

Java Package Vs Folder-Structure? what is the difference

I would like to know What are the difference between folder-structure and package used in Eclipse IDE for Java EE development.
When do we use which one and why?.
Whats should be the practice
create a folder structure like src/com/utils and then create a class inside it
create a package like src.com.util and then create a class inside it
which option would be better and easy to deploy if i have to write a ant script later for deployment ?
if i go for the folder-structure will the deployment is as easy as copying files from development to deployment target ?
If you configured stuffs correctly. Adding a folder inside src, is same as adding a package from File > New Package.
So, it's up to you, whatever feels comfortable to you -- add a folder or create a package. Also, when you put stuffs under src the package name starts from subfolder. So, src/com/naishe/test will be package com.naishe.test.
Basically there is no difference, both are the same.
In both the cases, the folder structure will be src/com/utils.
and in both the cases, you will need to mention
package com.utils;
as first line in the class
Since it doesn't have any difference practically, it won't make any difference to ant script.
"Packaging helps us to avoid class name collision when we use the same class name as that of others. For example, if we have a class name called "Vector", its name would crash with the Vector class from JDK. However, this never happens because JDK use java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of package will also help us manage and use files stored in jar files in more efficient ways."
check out http://www.jarticles.com/package/package_eng.html for more information on packages
create a package like 'src.com.util'
That sounds like a mistake. The package name should be 'com.util', and 'src' is the name of the source folder.
Other than that, I fail to see what the difference is between your two choices. The result is the same, right? Just different steps in the GUI to arrive at it. The wizard to create a new package in Eclipse is just a wrapper around creating the appropriate folder hierarchy within a source folder.
You don't need to create empty packages at all, you can directly create classes (the package will be created automatically if it does not already exist).
A package is automatically "source folder" where folder is just a normal folder.
When you compile an Eclipse project, all files in source folders are compiled but not in regular folders (unless those regular folders a)
folder structure or to be specific source folder in eclipse is meant just for eclipse but package is universal irrespective of any editor..

Categories

Resources