This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about packages

 
Paulo DeLimma
Greenhorn
Posts: 9
C++ Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what error you are getting?
 
Paulo DeLimma
Greenhorn
Posts: 9
C++ Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it goes:
Error.png
[Thumbnail for Error.png]
 
Abhay Agarwal
Ranch Hand
Posts: 1375
Eclipse IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you mean javac -d?
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't you mean javac -d?


Yep.
 
Paulo DeLimma
Greenhorn
Posts: 9
C++ Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic