File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes A question about packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A question about packages" Watch "A question about packages" New topic
Author

A question about packages

Paulo DeLimma
Greenhorn

Joined: Jul 02, 2011
Posts: 9


I've made an empirical observation and I'd like to hear from someone who really knows what goes on: when we declare a compilation unit as member of a package (using the package keyword), we are allowed to define a main method inside the public class, but not to call it? I mean, when I comment out "package blahblah..." I am able to compile it and run it, but when I say it's part of a package, I can only compile it, the java keyword results in an error. So in the second case, the main method becomes useless?

I hope it makes sense, sorry if the question is silly.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

what error you are getting?
Paulo DeLimma
Greenhorn

Joined: Jul 02, 2011
Posts: 9

Here it goes:


[Thumbnail for Error.png]

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1220
    
    1

is your class "Music" is in required package folder ?


I am describing a simple example regarding packages. Hope this will help you in understanding package concepts.


javac command accepts Java class name but java command accepts only full qualified name of Java class.

I have class Test.java. Code is mentioned below. This class resides in "C:\Testing" folder. Class is defined to be part of a.b package.



Now I compile this class on command prompt as

C:\Testing>javac Test.java

Class compile fine as javac commnad can accept Java class name. remember that currently I have not placed Test.java in any package folder.

Now if I run this class as
C:\Testing>java Test
then I get below mentioned error

Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: a/b/Test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Test. Program will exit.


Test.class is present in C:\Testing folder but still we get this error as 'NoClassDefFound' because we have defined this class to be part of a.b package.
This error can be removed by moving Test.class in required package structure. To do so , I have created folder 'a' within 'Testing' folder. Then folder 'b' inside folder 'a'.
So my hierarchy becomes
C:\Testing\a\b
Then I copied Test.class in folder 'b'.

Now again I ran java command but we fully qaulified name of class.
C:\Testing>java a.b.Test
This time I got my desired output as
Testing package

~ abhay


Oracle Java Web Service Developer (1z0-897), Oracle certified Java 7 Programmer, SCJA 1.0, SCJP 5.0, SCWCD 5.0, Oracle SQL Fundamentals I, CIW Certified Ecommerce specialist
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Yep, that is the way of the Java Namespace. If I only call for Paulo I most likely will get a lot of people answering. If I call out for Paulo DeLimma, then you are more likely to respond. If I also state your home town, we narrow it down even more.

Same with package. The full qualified class name of your example is a.b.Test. The whole Java API is build sorting the different classes into packages and you call them by importing them, using their fully qualified class names.

Take a look at http://cs.smu.ca/~porter/csc/465/notes/javapl_packages.html.


Additional info: if you use javac -d you will get the compiler to create the directory structure for you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Don't you mean javac -d?
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Campbell Ritchie wrote:Don't you mean javac -d?


Yep.
Paulo DeLimma
Greenhorn

Joined: Jul 02, 2011
Posts: 9

Abhay Agarwal wrote:

is your class "Music" is in required package folder ?



Yes, the code in Music.java starts with

package c07.music

So do the other packages required for Music to run: Instrument, Note and Wind. So the directory structure seems correct. (It's part of the code examples from Bruce Eckel's Thinking in Java, so not created by me).

Abhay Agarwal wrote:

javac command accepts Java class name but java command accepts only full qualified name of Java class.



That's what I needed to know, I put the full class name c07.music.Music and now it works, thanks.

Ove Lindström wrote:Yep, that is the way of the Java Namespace. If I only call for Paulo I most likely will get a lot of people answering. If I call out for Paulo DeLimma, then you are more likely to respond. If I also state your home town, we narrow it down even more.


Yes, I would've liked my parents to give me a less common first name, but in any case full names are always useful. Thanks for your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about packages