Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Why a simple package statement

 
Thread Tools Search this Thread
Old 11-20-2003, 05:49 PM   #1
Default Why a simple package statement


Hi,

I have below .java files:

c:\javapgm\Level1.java

File content:

package l1;

public class Level1 {
public static void main (String[] args) {
System.out.println("Level 1");
}
}

Compilation passed without error with above code. However, as I ran
the program, below run time error was output.

Exception in thread "main" java.lang.NoClassDefFoundError: Level1
(wrong name: l1/Level1)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java :502)
at java.lang.security.SecureClassLoader.defineClass(S ecureClassLoader.java:12)
..... skippped details.

After I commented out the statement package l1;, the program ran
without error.

Previously I have written a program Aclass3.java, the program also
passed compilation but failed in runtime with similar problem. The
problem got solved after I commented the package Aclass3.java;
statement at Aclass3.java.

I would like to ask why the simple package statement will cause the
program execution failed.

http://www.google.com.hk/groups?hl=z...com%26rnum%3D1


lonelyplanet999
  Reply With Quote
Old 11-20-2003, 08:00 PM   #2
John C. Bollinger
 
Posts: n/a
Default Re: Why a simple package statement
lonelyplanet999 wrote:

> Hi,
>
> I have below .java files:
>
> c:\javapgm\Level1.java
>
> File content:
>
> package l1;
>
> public class Level1 {
> public static void main (String[] args) {
> System.out.println("Level 1");
> }
> }
>
> Compilation passed without error with above code. However, as I ran
> the program, below run time error was output.
>
> Exception in thread "main" java.lang.NoClassDefFoundError: Level1
> (wrong name: l1/Level1)
> at java.lang.ClassLoader.defineClass0(Native Method)
> at java.lang.ClassLoader.defineClass(ClassLoader.java :502)
> at java.lang.security.SecureClassLoader.defineClass(S ecureClassLoader.java:12)
> ..... skippped details.
>
> After I commented out the statement package l1;, the program ran
> without error.


That is because when you have the package statement in the source, the
fully-qualified name of your class is "l1.Level1", not just "Level1".
You must specify the fully-qualified name to the java executable. You
may then also find that java expects to find the class file in
l1/Level.class (substitute correct directory seperator for your OS) with
respect to one of the directories in your classpath (which apparently
includes the working directory). (I.e. class files arranged in a
directory tree mirroring the package hierarchy.) Best practice would be
to also arrange your source files in a similar (or the same) directory tree.

> Previously I have written a program Aclass3.java, the program also
> passed compilation but failed in runtime with similar problem. The
> problem got solved after I commented the package Aclass3.java;
> statement at Aclass3.java.
>
> I would like to ask why the simple package statement will cause the
> program execution failed.


It's not the package statement that's broken, it's the "java" command
line you use to try to load and run the class.


John Bollinger




John C. Bollinger
  Reply With Quote
Old 11-21-2003, 03:30 PM   #3
lonelyplanet999
 
Posts: n/a
Default Re: Why a simple package statement
"John C. Bollinger" <> wrote in message news:<bpj6hc$n7d$>...
> lonelyplanet999 wrote:
> >
> > After I commented out the statement package l1;, the program ran
> > without error.

>
> That is because when you have the package statement in the source, the
> fully-qualified name of your class is "l1.Level1", not just "Level1".
> You must specify the fully-qualified name to the java executable. You
> may then also find that java expects to find the class file in
> l1/Level.class (substitute correct directory seperator for your OS) with
> respect to one of the directories in your classpath (which apparently
> includes the working directory). (I.e. class files arranged in a
> directory tree mirroring the package hierarchy.) Best practice would be
> to also arrange your source files in a similar (or the same) directory tree.
>
> > Previously I have written a program Aclass3.java, the program also
> > passed compilation but failed in runtime with similar problem. The
> > problem got solved after I commented the package Aclass3.java;
> > statement at Aclass3.java.
> >
> > I would like to ask why the simple package statement will cause the
> > program execution failed.

>
> It's not the package statement that's broken, it's the "java" command
> line you use to try to load and run the class.


That means, if I should give a .java file a package name such that
it's class can be referenced in other .java files. I must create
corresponding directory to accommodate it ?

e.g. Now I place all .java files under c:\javapgm. I call
c:\j2sdk1.4.1_01\bin\javac & c:\j2sdk1.4.1_01\bin\java under
c:\javapgm to compile & run java programs.

1. If I named File1.java as "package package1;", I need to place
File1.java under c:\javapgm\package1\

2. If another file under c:\javapgm named File2.java without package
statement needs to reference class inside package1.
3. File2.java needs the statement "import package1.*;" or
"package1.<specific class>;" to import specific class.

4. If another file named File3.java with "package package3;" statement
needs to reference class inside File1.java too.
5. File3.java needs to be stored under "c:\javapgm\package3\".
6. File3.java needs import "package1.*" or "package.<specific class>;"
inside it under "package package3;" statement.

Is that correct ?

:>

>
>
> John Bollinger
>



lonelyplanet999
  Reply With Quote
Old 11-21-2003, 04:12 PM   #4
lonelyplanet999
 
Posts: n/a
Default Re: Why a simple package statement
"John C. Bollinger" <> wrote in message news:<bpj6hc$n7d$>...
> lonelyplanet999 wrote:
>
> > Hi,
> >
> > I have below .java files:
> >
> > c:\javapgm\Level1.java
> >
> > File content:
> >
> > package l1;
> >
> > public class Level1 {
> > public static void main (String[] args) {
> > System.out.println("Level 1");
> > }
> > }
> >
> > Compilation passed without error with above code. However, as I ran
> > the program, below run time error was output.
> >
> > Exception in thread "main" java.lang.NoClassDefFoundError: Level1
> > (wrong name: l1/Level1)
> > at java.lang.ClassLoader.defineClass0(Native Method)
> > at java.lang.ClassLoader.defineClass(ClassLoader.java :502)
> > at java.lang.security.SecureClassLoader.defineClass(S ecureClassLoader.java:12)
> > ..... skippped details.
> >


1. Just moved Level1.java to c:\javapgm\l1 & called
c:\j2sdk1.4.1_01\bin\javac l1\Level1.java from c:\javapgm.
Compilation succeeded & Level1.class created in c:\javapgm\l1\.

2. Called c:\j2sdk1.4.1_01\bin\java l1\Level1 from c:\javapgm, same
runtime exception triggered.

3. Called c:\j2sdk1.4.1_01\bin\java Level1 from c:\javapgm\l1, same
runtime exception triggered.


lonelyplanet999
  Reply With Quote
Old 11-21-2003, 05:59 PM   #5
John C. Bollinger
 
Posts: n/a
Default Re: Why a simple package statement
lonelyplanet999 wrote:
> 1. Just moved Level1.java to c:\javapgm\l1 & called
> c:\j2sdk1.4.1_01\bin\javac l1\Level1.java from c:\javapgm.
> Compilation succeeded & Level1.class created in c:\javapgm\l1\.


As it should be.

> 2. Called c:\j2sdk1.4.1_01\bin\java l1\Level1 from c:\javapgm, same
> runtime exception triggered.


> 3. Called c:\j2sdk1.4.1_01\bin\java Level1 from c:\javapgm\l1, same
> runtime exception triggered.


The name of the class is "l1.Level1". The command line
"c:\j2sdk1.4.1_01\bin\java l1.Level1" should be executed from directory
c:\javapgm.

The java class loader will find the correct directory on its own, based
on the class name and the applicable classpath.


John Bollinger




John C. Bollinger
  Reply With Quote
Old 11-21-2003, 06:06 PM   #6
John C. Bollinger
 
Posts: n/a
Default Re: Why a simple package statement
lonelyplanet999 wrote:
> That means, if I should give a .java file a package name such that
> it's class can be referenced in other .java files. I must create
> corresponding directory to accommodate it ?


That is not absolutely required, but it is very good practice. Among
other things, it allows the java compiler to find and compile
dependencies at need.

> e.g. Now I place all .java files under c:\javapgm. I call
> c:\j2sdk1.4.1_01\bin\javac & c:\j2sdk1.4.1_01\bin\java under
> c:\javapgm to compile & run java programs.
>
> 1. If I named File1.java as "package package1;", I need to place
> File1.java under c:\javapgm\package1\


That should work. There are alternatives, but I wouldn't worry about
them right now.

> 2. If another file under c:\javapgm named File2.java without package
> statement needs to reference class inside package1.
> 3. File2.java needs the statement "import package1.*;" or
> "package1.<specific class>;" to import specific class.


The location of the source file is not directly relevant to how the Java
class(es) it describes must refer to other classes. If classes in any
package other than package1 want to access members of package1, though,
then the alternatives you describe are indeed the available options.

> 4. If another file named File3.java with "package package3;" statement
> needs to reference class inside File1.java too.
> 5. File3.java needs to be stored under "c:\javapgm\package3\".


This would be best.

> 6. File3.java needs import "package1.*" or "package.<specific class>;"
> inside it under "package package3;" statement.


As described above, the relevant criterion is the package to which a
class is assigned, not the structure of the source tree, but yes, that's
correct.


John Bollinger




John C. Bollinger
  Reply With Quote
Old 11-22-2003, 03:40 AM   #7
lonelyplanet999
 
Posts: n/a
Default Re: Why a simple package statement
"John C. Bollinger" <> wrote in message news:<bpljs9$hbe$>...
> lonelyplanet999 wrote:
> > 1. Just moved Level1.java to c:\javapgm\l1 & called
> > c:\j2sdk1.4.1_01\bin\javac l1\Level1.java from c:\javapgm.
> > Compilation succeeded & Level1.class created in c:\javapgm\l1\.

>
> As it should be.
>
> > 2. Called c:\j2sdk1.4.1_01\bin\java l1\Level1 from c:\javapgm, same
> > runtime exception triggered.

>
> > 3. Called c:\j2sdk1.4.1_01\bin\java Level1 from c:\javapgm\l1, same
> > runtime exception triggered.

>
> The name of the class is "l1.Level1". The command line
> "c:\j2sdk1.4.1_01\bin\java l1.Level1" should be executed from directory
> c:\javapgm.
>


Sorry, the compilation failed again after I renamed
c:\javapgm\l1\Level.java package name as "package l1.Level1;" and
compiled the file from c:\javapgm i.e. c:\j2sdk1.4.1_01\bin\javac
l1\Level1.java.

Compiler complained:
l1\Level1.java:3: package l1.Level1 clashes with class of same name
package l1.Level1;
^
1 error

Of course c:\j2sdk1.4.1_01\bin\javac Level1.java failed due to
Level1.java can't be read when javac called from c:\javapgm.



> The java class loader will find the correct directory on its own, based
> on the class name and the applicable classpath.
>
>
> John Bollinger
>



lonelyplanet999
  Reply With Quote
Old 11-22-2003, 08:03 AM   #8
Christophe Vanfleteren
 
Posts: n/a
Default Re: Why a simple package statement
lonelyplanet999 wrote:

> "John C. Bollinger" <> wrote in message
> news:<bpljs9$hbe$>...
>> lonelyplanet999 wrote:
>> > 1. Just moved Level1.java to c:\javapgm\l1 & called
>> > c:\j2sdk1.4.1_01\bin\javac l1\Level1.java from c:\javapgm.
>> > Compilation succeeded & Level1.class created in c:\javapgm\l1\.

>>
>> As it should be.
>>
>> > 2. Called c:\j2sdk1.4.1_01\bin\java l1\Level1 from c:\javapgm, same
>> > runtime exception triggered.

>>
>> > 3. Called c:\j2sdk1.4.1_01\bin\java Level1 from c:\javapgm\l1, same
>> > runtime exception triggered.

>>
>> The name of the class is "l1.Level1". The command line
>> "c:\j2sdk1.4.1_01\bin\java l1.Level1" should be executed from directory
>> c:\javapgm.
>>

>
> Sorry, the compilation failed again after I renamed
> c:\javapgm\l1\Level.java package name as "package l1.Level1;" and
> compiled the file from c:\javapgm i.e. c:\j2sdk1.4.1_01\bin\javac
> l1\Level1.java.
>
> Compiler complained:
> l1\Level1.java:3: package l1.Level1 clashes with class of same name
> package l1.Level1;
> ^
> 1 error


Don't put capitals in packagenames (especially not the first letter), that
makes it confusing (by convention, only classes start with a capital
letter).

>
> Of course c:\j2sdk1.4.1_01\bin\javac Level1.java failed due to
> Level1.java can't be read when javac called from c:\javapgm.
>
>


Of course that is not going to work.
Your compiler is expecting a directory called l1/Level1, but in your case,
that is allready a class file.

Take the following as a base rule when working with packages:
If you put a file in package aaaa.bbb, you'll need to put it in a directory
called aaa/bbb, and compile the class from the directory that contains the
aaa dir.

You cant just change a package statement and leave the file where it was
before.


--
Regards,
Christophe Vanfleteren


Christophe Vanfleteren
  Reply With Quote
Old 11-22-2003, 09:15 PM   #9
Sudsy
 
Posts: n/a
Default Re: Why a simple package statement
lonelyplanet999 wrote:
> Sorry, the compilation failed again after I renamed
> c:\javapgm\l1\Level.java package name as "package l1.Level1;" and
> compiled the file from c:\javapgm i.e. c:\j2sdk1.4.1_01\bin\javac
> l1\Level1.java.
>
> Compiler complained:
> l1\Level1.java:3: package l1.Level1 clashes with class of same name
> package l1.Level1;
> ^
> 1 error


Try reading this:
<http://www.sudsy.net/technology/java-classes.html>



Sudsy
  Reply With Quote
Old 11-23-2003, 02:45 PM   #10
lonelyplanet999
 
Posts: n/a
Default Re: Why a simple package statement
Christophe Vanfleteren <> wrote in message news:<YKEvb.36146$>...
> lonelyplanet999 wrote:
>
> > "John C. Bollinger" <> wrote in message
> > news:<bpljs9$hbe$>...
> >> lonelyplanet999 wrote:
> >> > 1. Just moved Level1.java to c:\javapgm\l1 & called
> >> > c:\j2sdk1.4.1_01\bin\javac l1\Level1.java from c:\javapgm.
> >> > Compilation succeeded & Level1.class created in c:\javapgm\l1\.
> >>
> >> > 2. Called c:\j2sdk1.4.1_01\bin\java l1\Level1 from c:\javapgm, same
> >> > runtime exception triggered.

>
> >> > 3. Called c:\j2sdk1.4.1_01\bin\java Level1 from c:\javapgm\l1, same
> >> > runtime exception triggered.
> >>
> >> The name of the class is "l1.Level1". The command line
> >> "c:\j2sdk1.4.1_01\bin\java l1.Level1" should be executed from directory
> >> c:\javapgm.
> >>

> >
> > Sorry, the compilation failed again after I renamed
> > c:\javapgm\l1\Level.java package name as "package l1.Level1;" and
> > compiled the file from c:\javapgm i.e. c:\j2sdk1.4.1_01\bin\javac
> > l1\Level1.java.
> >

>
> Don't put capitals in packagenames (especially not the first letter), that
> makes it confusing (by convention, only classes start with a capital
> letter).


It only confuses human reader, it shouldn't cause compilation error, right ?

>
>
> Of course that is not going to work.
> Your compiler is expecting a directory called l1/Level1, but in your case,
> that is allready a class file.
>
> Take the following as a base rule when working with packages:
> If you put a file in package aaaa.bbb, you'll need to put it in a directory
> called aaa/bbb, and compile the class from the directory that contains the
> aaa dir.


Already put file Level1.java into c:\javapgm\l1 described above. Compilation failed.

>
> You cant just change a package statement and leave the file where it was
> before.



lonelyplanet999
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
enterprisedb package execution kanchuparthi.rams Software 0 05-22-2008 01:59 PM
Simple video editor? trs80 DVD Video 7 04-10-2007 05:14 PM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
The Practice Test Package Development: A New Service on the Certification Market David Johnson A+ Certification 0 01-19-2005 10:52 AM
PC Package deals? jkl A+ Certification 0 10-28-2004 05:12 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46