barnarp
asked on
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The CLASSPATCH variable in my environmental variables contains 3 different directories, i s this correct?
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
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
>> You can do this with the New wizard:
That would be
(Right-clicking the project icon)
New | Other | Class
That would be
(Right-clicking the project icon)
New | Other | Class
ASKER
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?
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?
I'm trying to show you how the command line relates tothe packages in Eclipse. You're probably running from the wrong place
>>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
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
ASKER
Thanks
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.
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-
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.
:-)
ASKER
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?