• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Package question

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your classpath, and what is this "homework" subdirectory that it is talking about?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic