raicoder
raicoder

Reputation: 15

Do the java packages differ from the concept of directory or folder? if yes how?

I have a created a class by writing code as:

pacakge packtst;
public class Class1
{
  public static void main(String args[])
  {
    System.out.print("This is class1");
  }
}

Then i compiled the program by

javac -d . Class1.java

It created a subdirectory in my current directory, named packtst. Then I went into the packtst deirectory. Within it i made another class as:

public class Class2
{
  public static void main(String args[])
  {
    System.out.print("this is class2");
  }
}

And compiled it in the packtst directory by

javac Class2.java

So, now in the package packtst i have two class file Class1.class and Class2.class Now I try to run Class1 from outside of the directory by

java packtst.Class1

It ran well. But then I tried to run Class2 from the same directory:

java packtst.Class2

It said couldn't find or load main class packtst.Class2 Why?

Upvotes: 0

Views: 62

Answers (3)

user207421
user207421

Reputation: 310883

I have a created a class by writing code as:

pacakge packtst;
public class Class1

Then i compiled the program by

javac -d . Class1.java

That was your first two mistakes. The .java file should have been in the packtst directory, relative to where you started, and you should have compiled it via:

javac packtst/Class1.java

Then I went into the packtst deirectory.

That was your second mistake. You should have stayed where you were.

Within it i made another class as:

public class Class2

That was your third mistake. As Class2 doesn't have a package statement, it should be in the directory that you started in.

And compiled it in the packtst directory by

javac Class2.java

That was your fourth mistake. The javac command line is correct but it should have been issued from the directory you started in, i.e. the one that contains the packtst directory.

So, now in the package packtst i have two class file Class1.class and Class2.class

That was your fifth mistake. Your directories from where you started should now contain:

packtest/Class1.class
Class2.class

Now I try to run Class1 from outside of the directory by

java packtst.Class1

It ran well.

So you must have now been in the directory you started in.

But then I tried to run Class2 from the same directory:

java packtst.Class2

That was your sixth mistake. Class2 isn't in the packtst package, because its source code doesn't contain such a package statement. The command should have been:

java Class2

It said couldn't find or load main class packtst.Class2

Correct.

Why?

Because there is no such class. The fully qualified name of Class2 including all its declared packages, is Class2.

Upvotes: 1

andih
andih

Reputation: 5603

It said couldn't find or load main class packtst.Class2

because Class2 does not have a / belong to a package name packtst Even the compiled class file is located in packtst the full qualified name of the class is Class2 (the full qualified name of Class1 is packtst.Class1). The package name is part of the (source) and the compiled class file.

That the package structure and the filesystem structure match is only a convention the java jvm and the java compilers use.

If the classes are stored on windows or *nix filesystems there is the convention that you must store a Java class in a directory with a relative directory path that matches the package name for that class.

Java Packages are an abstract construct. They help to organise the Applications into a set of packages. Each package has it's own set of names for the types (Classes, Interfaces,...) in the package, which helps to prevent naming conflicts.

The JLS (Java Language specification) only says about the package structure that it is hierarchical but also that you don't have to store java classes on an hierarchical filesystem you can also store them in a database.

JLS Java 8:

The naming structure for packages is hierarchical (§7.1). The members of a package are class and interface types (§7.6), which are declared in compilation units of the package, and subpackages, which may contain compilation units and subpackages of their own.

A package can be stored in a file system or in a database (§7.2). Packages that are stored in a file system may have certain constraints on the organization of their compilation units to allow a simple implementation to find classes easily.

If the classes are stored in a (hierarchical) file system which is similar to what you find on most PC and UNIX® systems, then the package name relates to the directory structure in which the class resides. This is how the actual java compilers, jvms / class loaders on Windows and *NIX work.

There is a article on the IBM Knowledge Base (Java classes, packages, and directories) which describes the relationship.

Upvotes: 0

Shubham Maheshwari
Shubham Maheshwari

Reputation: 114

In java, it is not enforced to have the same directory structure as your package structure referenced in that class. Since you are using the '-d' option to compile your code, the compiler will create a directory structure similar to the package structure to put the compiled class. In the case of Class2, since you did not define any package reference for it, "packtst.Class2" will not exist.

Directory structure is a best-practice (to reflect the package structure in the source directory structure) and not something enforced by java compiler.

Also, package structure is the structure in which all your compiled classes would be present in the jar (or your complete project)

Upvotes: 0

Related Questions