0% found this document useful (0 votes)
88 views

Package Class Public Static Void: Mypackage Helloworld (Main (String Args ) (.Out - Println ) )

The package that a source file belongs to is specified with the keyword "package" at the top of the file. This document discusses how to specify a package name for a Java file, compile files within packages, and run programs that belong to packages. It also covers topics like fully qualified class names and exceptions that can occur when running programs that belong to packages.

Uploaded by

jormn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Package Class Public Static Void: Mypackage Helloworld (Main (String Args ) (.Out - Println ) )

The package that a source file belongs to is specified with the keyword "package" at the top of the file. This document discusses how to specify a package name for a Java file, compile files within packages, and run programs that belong to packages. It also covers topics like fully qualified class names and exceptions that can occur when running programs that belong to packages.

Uploaded by

jormn
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

The package to which the source file belongs is specified with the

keywordpackage at the top left of the source file, before the code that defines
the real classes in the package. 
Suppose we have a source file called"HelloWorld.java" and we want to put this
file in a package "mypackage" then the following code is written as shown in
the given example:
package mypackage;
class HelloWorld {
  public static void main (String args[]) {
    System.out.println("Hello World!");
  }
}
Before running this program make sure to do the following things:

1. Create a directory "mypackage" .
2. Save the  source file as "HelloWorld.java" in the created directory.
3. Set the class path as  set CLASSPATH = .;C:\;        
4. Go to the "mypackage" directory and compile the program as

C:\mypackage>javac
HelloWorld.java

5. Run the program.

If you try to run this program, you will get the following exceptions (or error):
C:\mypackage>java HelloWorld
Exception in thread "main"
java.lang.NoClassDefFoundError: HelloWorld
(wrong name: mypackage/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native
Method)
at java.lang.ClassLoader.defineClass(Unknown
Source)
at
java.security.SecureClassLoader.defineClass(Unk
nown Source)
at
java.net.URLClassLoader.defineClass(Unknown
Source)
at
java.net.URLClassLoader.access$000(Unknown
Source)
 
This is, because the class "HelloWorld" belongs to the package "mypackage".
So If we want to run it, we have to tell the JVM about its fully-qualified class
name as (mypackage.HelloWorld) instead of its plain class name
(HelloWorld). Fully-qualified class name is the name of the java class that
includes its package name. 

Now run the program as shown:


C:\mypackage>java
mypackage.HelloWorld
Hello World!
The ways to Compile the Package:
Compile in the same directory:  If you have a hierarchy of packages to
compilation then you can compile the package without going to the subdirectories
and specifying the complete directory path with the class . Suppose, you have a
hierarchy of packages as
"india.mycompany.mainpackage.mypackage" including the
class "HelloWorld" then type the following command shown as:
C:\javac
C:\india\mycompany\mainpackage\myp
ackage\HelloWord.java
 

This command will reach to the last subdirectory and compile the
class "HelloWorld".
Compile into the Different Directory: On the other hand, if you want to
compile the same package available in the hierarchy manner to another directory
(location) then syntax is shown as:
C:\javac -d <target_directory>
<complete_directorypath>
Suppose, you want to save the compiled package to the
location "D:\myfolder" then type the following command shown as:
C:\javac -d D:\myfolder C:\
india\mycompany\mainpackage\mypackage\HelloWord.ja
va
 

This command puts the folder "india" along with its subfolders and the class
file"HelloWorld.class" to the new location as D:\myfolder.
.5.4 A Strange Example

Package names and type names are usually different under the naming conventions
described in §6.8. Nevertheless, in a contrived example where there is an
unconventionally-named package Vector, which declares a public class
named Mosquito:
package Vector;
public class Mosquito { int capacity; }
and then the compilation unit:
package strange.example;
import java.util.Vector;
import Vector.Mosquito;
class Test {
public static void main(String[] args) {
System.out.println(new Vector().getClass());
System.out.println(new Mosquito().getClass());
}
}
the single-type-import declaration (§7.5.1) importing class Vector from
package java.util does not prevent the package name Vector from appearing and
being correctly recognized in subsequent import declarations. The example
compiles and produces the output:
class java.util.Vector
class Vector.Mosquito

7.6 Top Level Type Declarations


A top level type declaration declares a top level class type (§8) or a top level
interface type (§9):
TypeDeclaration:
ClassDeclaration
InterfaceDeclaration
;
By default, the top level types declared in a package are accessible only within the
compilation units of that package, but a type may be declared to be public to grant
access to the type from code in other packages (§6.6, §8.1.1, §9.1.1).

The scope of a top level type is all type declarations in the package in which the top
level type is declared.

If a top level type named T is declared in a compilation unit of a package whose


fully qualified name is P, then the fully qualified name of the type is P.T.  If the type
is declared in an unnamed package (§7.4.2), then the type has the fully qualified
name T.

Thus in the example:

package wnj.points;
class Point { int x, y; }
the fully qualified name of class Point is wnj.points.Point.

An implementation of the Java platform must keep track of types within packages
by their binary names (§13.1). Multiple ways of naming a type must be expanded
to binary names to make sure that such names are understood as referring to the
same type.

For example, if a compilation unit contains the single-type-import


declaration (§7.5.1):

import java.util.Vector;
then within that compilation unit the simple name Vector and the fully qualified
name java.util.Vector refer to the same type.

When packages are stored in a file system (§7.2.1), the host system may choose to
enforce the restriction that it is a compile-time error if a type is not found in a file
under a name composed of the type name plus an extension (such as .java or .jav)
if either of the following is true:

 The type is referred to by code in other compilation units of the package in


which the type is declared.
 The type is declared public (and therefore is potentially accessible from code
in other packages).

This restriction implies that there must be at most one such type per compilation
unit. This restriction makes it easy for a compiler for the Java programming
language or an implementation of the Java virtual machine to find a named class
within a package; for example, the source code for
a public type wet.sprocket.Toadwould be found in a file Toad.java in the
directory wet/sprocket, and the corresponding object code would be found in the
file Toad.class in the same directory.

When packages are stored in a database (§7.2.2), the host system must not
impose such restrictions.

In practice, many programmers choose to put each class or interface type in its
own compilation unit, whether or not it is public or is referred to by code in other
compilation units. A compile-time error occurs if the name of a top level type
appears as the name of any other top level class or interface type declared in the
same package (§7.6).

A compile-time error occurs if the name of a top level type is also declared as a
type by a single-type-import declaration (§7.5.1) in the compilation
unit (§7.3)containing the type declaration.

In the example:

class Point { int x, y; }


the class Point is declared in a compilation unit with no package statement, and
thus Point is its fully qualified name, whereas in the example:

package vista;
class Point { int x, y; }
the fully qualified name of the class Point is vista.Point. (The package name vista is
suitable for local or personal use; if the package were intended to be widely
distributed, it would be better to give it a unique package name (§7.7).)

In the example:

package test;
import java.util.Vector;
class Point {
int x, y;
}
interface Point { // compile-time error #1
int getR();
int getTheta();
}
class Vector { Point[] pts; } // compile-time error #2
the first compile-time error is caused by the duplicate declaration of the name Point
as both a class and an interface in the same package. A second error detected at
compile time is the attempt to declare the name Vector both by a class type
declaration and by a single-type-import declaration.

Note, however, that it is not an error for the name of a class to also to name a type
that otherwise might be imported by a type-import-on-demand
declaration(§7.5.2) in the compilation unit (§7.3) containing the class declaration.
In the example:

package test;
import java.util.*;
class Vector { Point[] pts; } // not a compile-time error
the declaration of the class Vector is permitted even though there is also a class
java.util.Vector. Within this compilation unit, the simple name Vector refers to the
class test.Vector, not to java.util.Vector (which can still be referred to by code
within the compilation unit, but only by its fully qualified name).

As another example, the compilation unit:

package points;
class Point {
int x, y; // coordinates
PointColor color; // color of this point
Point next; // next point with this color
static int nPoints;
}
class PointColor {
Point first; // first point with this color
PointColor(int color) {
this.color = color;
}
private int color; // color components
}
defines two classes that use each other in the declarations of their class members.
Because the class types Point and PointColor have all the type declarations in
package points, including all those in the current compilation unit, as their scope,
this example compiles correctly-that is, forward reference is not a problem.

It is a compile-time error if a top level type declaration contains any one of the
following access modifiers: protected, private or static.

You might also like