• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

package clause

Hi,

What exactly does it mean when a package is declared at the top of a java file.

Is it simply a "grouping" of .java files?

Does it mean that the file should be contained in a directory with the same name?

For instance, if in the code the line: package test; is declared, should the existing package be in a directory called c:\test.

What if it is in a directory called c:\dir1\test? Should the declaration then be package dir1.test?

Regards
0
barnarp
Asked:
barnarp
2 Solutions
 
CEHJCommented:
The answers are really yes all the way through apart from:

>>What if it is in a directory called c:\dir1\test? Should the declaration then be package dir1.test?

There's no automatic relationship between filesystem and package. The package root is a classpath. If the classpath is c:\dir1 then the package is test, if c:\ then its dir1.test
0
 
barnarpAuthor Commented:
I created a package in eclipse with 3 .java files under it.

In each of the three java file I have included the "package mypackage;" clause at the top.

My first 2 files (lets call them 1.java and 2.java) is referenced in 3.java, thus I use the import statement for both 1 and 2 in 3.java.

After I compile all 3 successfully in Eclipse, I go to the command line, to the specific directory and run: java 1, but I get the message:

NoClassDefFoundError: 1 (wrong name: mypackage/1)

What is wrong?
0
 
SibishCommented:
Packages are mainly used to manage name spaces. Packages are containers for classes that are used to keep the class name space compartmentalized. For example, a package allows you to create a class named Display, which you can store in your own package without concern that it will collide with some other class named Display.

 >>Does it mean that the file should be contained in a directory with the same name?
Yes the class files should be inside a directory with the same name as that of the package.

>>For instance, if in the code the line: package test; is declared, should the existing package be in a directory called c:\test? What if it is in a directory called c:\dir1\test? Should the declaration then be package dir1.test?

Until now, you have been storing all of your classes in the same, unnamed default package. Doing so allowed you to simply complie the source code and run it.This worked because the default working directory is usually in the CLASSPATH environmental variable.
CLASSPATH sets the top of the class hierarchy.

As you have said if you are working on a directory called c:\test then set your CLASSPATH to .;c:\test
so that you can run your code from any directory. If you want to create a package named dir1, then create a directory dir1 inside test. If you want to create a package hierarchy, for example

package dir1.dir2;
(test is not included here because c:\test is in the classpath)

For doing this create dir2 directory inside dir1 and put all the class files in it.

I hope you will be clear about the topic packages now.

Regards,
Sibish.K.Abraham

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
barnarpAuthor Commented:
The CLASSPATCH variable in my environmental variables contains 3 different directories, i s this correct?
0
 
CEHJCommented:
You can do this with the New wizard:

Create a class in package 'one.two'

package one.two;

public class TestPackage {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("Hello from packaged class " + TestPackage.class);
      }
}

Change to  <HOME>\workspace\<PROJECT NAME>

You can then run:

java one.two.PackageTest
0
 
CEHJCommented:
>> You can do this with the New wizard:

That would be

(Right-clicking the project icon)

New | Other | Class
0
 
barnarpAuthor Commented:
I am not sure what you mean.

I already have 3 java files of which the 3rd one is a test file/class.

I don't want to create anymore. I just need to know why the error occurs?
0
 
CEHJCommented:
I'm trying to show you how the command line relates tothe packages in Eclipse. You're probably running from the wrong place
0
 
CEHJCommented:
>>My first 2 files (lets call them 1.java and 2.java) is referenced in 3.java, thus I use the import statement for both 1 and 2 in 3.java.

That's not right actually - there's no need for an import of classes in the same package

>> go to the command line, to the specific directory and run: java 1

As i mentioned in my example that should be

java mypackage.1
0
 
barnarpAuthor Commented:
Thanks
0
 
aozarovCommented:
see http://www.cs.wisc.edu/~solomon/cs537/java-tutorial.html#compiling
or http://java.sun.com/docs/books/tutorial/java/interpack/packages.html
But basically it is more a runtime thing then a compilation issue.
when you run your program (e.g) java -classpath c:\my-java-files;c:\other-files\java test.Main
Then the file Main.class should be found under in c:\my-java\test or c:\other-files\java\test
To find classes during runtime the JVM will append the class package name (replacing . to / or \) to any of the folders in your classpath
and will expect to find your class file in one of them.
0
 
CEHJCommented:
:-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now