src/com.app.main && gen/com.app.main is the same package? - java

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.

Related

Java package folder naming conventions

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.

Is source file and a package the same thing in Eclipse?

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.

Java: silly newbie issue about path used in import statement

I'm working through the example here:
http://www.vogella.com/articles/JavaPDF/article.html
In my file, I've got:
package com.mycompanyname.mydirectory;
import com.mycompanyname.OneOfMyClasses;
import com.itextpdf.text.Document;
...
public class MyClass {
...
}
Everything is working fine. What I don't understand is that since I just copied the import statement directly from the link above for the iText portion -- why does com.itextpdf.text.Document work?
I mean, if I look in directory com.mycompanyname I can see OneOfMyClasses.java there.
But in the com directly, there is no itextpdf directory (although maybe my user doesn't have permission to see it(?)).
Hoping someone can help me understand what I'm missing here. Doesn't the import point to a specific directory that I should be able to see the class? Is there a different com directory somewhere that iText is using, and com.itextpdf.text points to there? (if so, where's the directory located)?
I installed the jar file for iText in the lib folder as per usual, and made sure it was included in the classpath.
Those classes are inside a JAR file that is added to the classpath:
Create a new Java project "de.vogella.itext.write" with the package "de.vogella.itext.write". Create a folder "lib" and put the iText library (jar file) into this folder. Add the jar to your classpath.
import statements will look inside whatever directory trees are in the classpath, which includes the current directory at compilation time (tipically the src/ directory in your project) as well as any directory specified through environment variable or JVM startup parameter. See this about the classpath.
EDIT
You do need the imports whenever you use classes across packages. Every public class/interface you define is in a package. If whatever you are referencing belongs to another package, you need to import it.
JARs are zip files that contain directories and files inside. It's the same as plain directories and files, only packed.
It comes from the iText dependency (jar) you added in an earlier step.
Not necessarily - you could also import from libraries, etc.
In fact, Java will try to search through the classpath. Here is some helpful documentation.
That class is most probably imported in a JAR library. Inside such JAR file, the class files are kept in exact package/folder structure as you use when importing them.

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