This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Package question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Package question" Watch "Package question" New topic
Author

Package question

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
I created the Class Hello
package hello.java;
class Hello {
public static void main (String arg[]) {
System.out.println("Hello");
}
When I compile and interept(java Hello) the program with the "package hello.java" gives me a bunsh o erros. But when I delete the line the program runs fine. Does someone can explain what coul cause the problem?
Thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
A package is not a file. It is a bunch of classes that are bundled together. In java the default is that the subdirectory that the class file is in is the package that the class is in.
In your example you are saying that this class will be in
JAVA_HOME\hello\java\Hello.class
I think that the compiler can not find all those sub-directories that you are talking about.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
the Hello.java class is stored on drive E:. But when I do javac Hello.java it's ok.. It give me an error when I type java Hello
I can't find what's is wrong
E:\>java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: ho
mework/Hello)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:11
1)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
What is your classpath, and what is this "homework" subdirectory that it is talking about?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Mike-
I think it might be a time to take a giant step back and understand the basics of packages... check out this thread as a start. Hopefully that will explain things a bit.
So... if you want all your apps to be in the "homework" package. Change the package name on your Hello class to "homework". And make sure your Hello.java is in the "homework" directory.
Then from the commandline, on the level above homework type (to compile it):
javac homework\Hello.java
Then (to run it) type:
java homework.Hello
Hope that helps!


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Package question
 
Similar Threads
help me. Package usage
Running Java Programs from the command prompt
package trouble
Running compiled code
Using cmd.exe