This is very entry level question and might have been answered already.
My question is, what is the correct way to name my package folders?
for example, I want to have package named - com.stack.abc
Option A- shall I create package folder with name - com.stack.abc?
or
Option B- shall I create folder structure as shown below?
com
|_stack
|_abc
To summarise, I should have only one folder (com.stack.abc) or 2 subfolders under com?
To your question, com.stack.abc should be the folder hierarchy structure.
com
|-stack
|-abc
Edit due to main post edited;
You would create 3 different folders. One folder called com, one folder inside com called stack, and one folder inside stack called abc. On some, or most IDEAs, it will look like 1 folder because it looks like com.stack.abc but it's really 3 folders.
Generally you define the source folder (top level of all java files) as
src/main/java/
Then you define the package that represents your website (if any)
com.stack.abc
This would resolve to;
abc.stack.com
The entire directory would look like this;
src/main/java/com/stack/abc
This might be a helpful link into packaging conventions.
Related
i got this sample solution from my Professor, but somehow it isn't working.
IntelliJ does not recognize the packages. That's the case for every class.
When I hover over it it says:
"Package name 'spaceman.view.util' does not correspond to the file path 'view.util' "
Has anyone any idea what could be wrong?
I'm a total beginner when it comes to coding so sorry, if the question is banal.
Thank you!!
Your project spaceman is the sources root. That means anything inside a folder view/welcome in that folder is supposed to be in the package view.welcome. If you want to keep the package spaceman.view.welcome then move the whole tree into another folder spaceman inside your spaceman project directory.
Note that it's rather unusual to have the project root itself be the source root. It's way more common to have a directory like src/ be the source root (or, if you follow the Maven/Gradle convention something like src/main/java).
I am a complete beginner when it comes to Java. I recently picked up Head First Java and it says: "Put a class in a source file. Put methods in a class. Put statements in a method." When I open eclipse i started a new project called helloWorld, this created a project with a src folder(guessing this is the source file?), i then followed an eclipse tutorial from their website and it stated that i needed to first create a project, then a package, then a class in that package. What is the difference between a source file and a package?
A package more-or-less equates to a directory under your "src" folder in this case. Examples might include "com.project.ui" or "com.project.models" (and so there would be a "com" directory inside "src" and inside "com" you would have "project" and so on).
A source file is just that--it's an individual file that will live in one of those packages, probably named as "MyClass.java" where "MyClass" corresponds exactly to the name you give the one public class that the source file should contain.
BTW, if you will build your code with Maven, you should follow the suggested Maven directory structure--see this. In the case of Maven then, your java packages would start under "src/main/java" rather than under just "src" which is maybe what Eclipse will assume you want by default.
EDIT: Also take care to align the package you declare at the top of your Java source file with the package that it actually "lives in" on your filesystem--it's essential that these be in agreement. So, if your "MyClass.java" lives on the filesystem in com/projects/models, your package statement at the top of "MyClass.java" must be "package com.projects.models;" By convention package names will be all lowercase, class names will be upper and lower ("camel case") starting with a capital letter and method names start with a lowercase letter, but then are also camel case.
The following is a java source text:
package org.apache.twinkle;
public class Elfie {
...
}
It resides under a sources directory (generally src), and has a file path:
org/apache/twinkle/Elfie.java
(Directories org, apache, twinkle and file Elfie.java.)
So a package indicates some hierarchy and corresponds 1:1 with a directory.
The source file has a .java extension.
Paths should be case-sensitive. Package paths are hierarchical and generally follow the convention of starting with a reversed URL.
http://mit.com
package com.mit.mathlib.graphs;
http://univ-abu-dabi2.net
package net.univAbuDabi2.linguistics;
import com.mit.mathlib.graphs.GraphUtils;
Source file is complete Java code.
Package gather a several Java file under some issue like: GUI, server, login and etc.
Try to create several package and then go to the workspace to see what you got.
Also, when it comes to package issues, you also have the 'package' definition for class variables, which means that you are able to use this variable from other classes in the same package.
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.
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..
When we create a simple android app, we can find MainActivity.java in src/com.app.main and R.java in gen/com.app.main (Also, you can define another file or package name, but the format is similar). and from all the indication i found in the code, it seems that src/com.app.main && gen/com.app.main means the same package. why? IMO package is only a folder, and MainActivity.java is in the folder src/com/app/main, R.java is in the folder gen/com/app/main, obviously two different folders, and i think that two different folders mean two different package, anything wrong?
There are two thing one is Java package name in src/ and second is android app package name. The folder structure in gen is generated based on the package name defined in the manifest file.
These two could be different also, in that case you will import R with appropriate package name appended to it in the Java files.
First of all, package is not a folder. Source file belongs to the package indicated by the package clause at the first line of that file. Creating folder structure that corresponds to the package structure is just a convention.
Secondly, when mapping folders to packages folders are treated as relative to their respective base source folders. In your case there are two source folders: src for your sources and gen for generated sources, and both folders contain subfolders that correspond to the same package.