Configuring IntelliJ project - java

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.

Related

Issues importing packages

This may be a duplicate, but I can't find any solutions that are similar to mine. (I probably just suck at searching)
I'm currently new and this is the first time actually stepping up to ask a dumb question, but I'm currently trying to important packages but it doesn't recognize my packages. I've created a com folder in my C: drive and created two folders named school and home. In the school folder, I have two folders named student and teacher and in the home folder, with multiple classes in each folder. In the home folder, I have a main class that imports the packages from the student and teacher folder, but it won't recognize my packages.
home folder -
hi.java (import com.school.student.; and import com.school.teacher.; )
student folder -
multiple class files with (package com.school.student;) at the top
teacher folder -
one class file with (package com.school.teacher;) and (import com.school.students.*;)
I can't tell what I'm doing wrong as it's not recognizing com.school.student.; and com.school.teacher.; in the hi.java method.
You have to remove period(.) from the end of import statements.
Instead of
com.school.student.;
com.school.teacher.;
use
com.school.student.*;
com.school.teacher.*;
This will import all the classes from com.school.student and com.school.teacher packages. It is better to import only those classes which will be used by your class.
You can use any IDE like Eclipse or NetBeans to code. Both of them are free.

Imports in a source folder: The import cannot be resolved

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.

Java package in package?

I'm using eclipse 3.8 indigo and I don't know why, when I'm creating a new package the destination of the new package goes outside. I want to create new packages within a package: package in package. I tried to copy / paste the newly created package or to move, but it just copies.
For example, I want to create different packages: dialogs, views, tables, etc. in my main source package. For example:
com.new.application // this is the the main package created by wizard
com.new.application.view // package that contains all views.
In reality there is no such thing as a sub-package in Java - each package is a completely separate entity, with the names being seemingly hierarchical only for convenience. For example, items with default visibility are not visible in sub-packages, despite what one might expect.
If your problem has more to do with presentation and aesthetics than substance, then perhaps what you are looking for is the hierarchical package presentation setting in the Eclipse Package Explorer: click on the little downward triangle/arrow at the top right of the package explorer and select "Hierarchical" in the "Package presentation" submenu:
This is a global setting and will affect all your opened/un-opened projects.
There is no concept of package with in package. Each package is separate namespace. I think if you go to folder view instead of package view, there you may see one under another.
The eclipse package explorer has two view options: flat or hierarchical. You are probably in the flat view (which is the default, who knows why). Change the view to hierarchical by clicking on the small triangle on the top right corner of the package view and then changing the package representation.
Firstly, you should select hierarchical representation.
Secondly, you should entitle completely package name. For example, you have 'main' package and you will create 'sub' package, you should entitle this with 'main.sub'. If you have just 1 package in package, may be you can't see hierarchicaly these packages.
After that, package view:
Second sub package creation:
Finally package view:

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

Can I put a java package into another package?

Using Netbeans, I want to put a package into another package. For example in Visual Studio 2008, I can have folder called "Nodes", and another folder inside of Nodes called "Expressions". How do I do this in Netbeans? I think a package in Java is equivalent to a folder in C#.
For a package within a package, put the parent name, a period, and then the name of the child's package like so: Nodes.Expressions.
It will appear as it's own separate package in an IDE perhaps, but the folder hierarchy will be as you desire: Nodes/Expressions/[classes etc]
You can create subpackages in java. If your package is called nodes, adding an expressions folder inside of it will create a nodes.expressions package.
FYI in Java, it is customary to use all lower case for package names.
Kevin is correct in his answer about packages.
Heres Netbeans specific steps for adding a new package:
In your Projects view, go to the parent package under "Source Packages".
Right click on the Package and select New>Java Package (if Java Package doesn't appear in the list, select Other... and then pick Java>Java Package)
Fill out the New Java Package wizard with name of the child package
You can have any number of subpackages, e.g. abc.def.ghi.jkl.
You can simply put the source in a folder within the current package structure. You reference it by adding a dot and the new package name to the end of the existing package name. In Netbeans, in the new file wizard, where the package name is referenced, you can input the new package by choosing an existing package and adding the .newpackagename to the end. Netbeans will then create the directory structure for you.

Categories

Resources