• 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 Headache

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm studying for the SCJP programmer test and am having an issue with implementing a 'package statement'.
I have a package statement in my '.java' file which represents the path of the class file in underlying directory structure. The code should print out a single line of text. Instead, an error is thrown:
Exception in thread "main" java.lang.NoClassDefFoundError: Example1 (wrong name: com/christiangarcia/examples/Example1)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Any idea what is going on here???
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the source code for main, including the package statement? Also, what are your command line arguments for the java command? I want to see how you are setting your classpath.
Thanks!

Rob
 
Christian Garcia
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Ross:
Can you post the source code for main, including the package statement? Also, what are your command line arguments for the java command? I want to see how you are setting your classpath.
Thanks!

Rob



Rob,
Here's the code:
package com.christian.examples;
public class Example1
{
public static void main(String[] args)
{
System.out.println( "This should work!!!" );
}
}

My CLASSPATH is set as an environment variable and includes the generic root (".") and E:\com\christian\examples;
Thanks for your help.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Christian
I was facing similar problems
Thing to remember is that packages memic directory structures
and when you run this file you should use the fully qualified name in your case :
at this dir : E:\com\christian\examples\
run this command :
java com.christian.examples.Example1
This should work
satish
 
Christian Garcia
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by satish limaye:
Hi Christian
I was facing similar problems
Thing to remember is that packages memic directory structures
and when you run this file you should use the fully qualified name in your case :
at this dir : E:\com\christian\examples\
run this command :
java com.christian.examples.Example1
This should work
satish


Satish,
That did the trick. However, this is still somewhat confusing.
I was under the impression that having the path to the class file(s) listed in the CLASSPATH would provide sufficient information for the Runtime to locate and execute the particular '.class' file.
I had expected that when I typed "java Example1" at the command line that the Example1.class file would have been located via the entry in the CLASSPATH environment variable. At the very least, the root "." should have worked.
If you could provide any more info regarding this I'd appreciate it.
CG
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doesnt the " . " (dot) mean current directory?
try setting your class path to e:\
i had the same problem and this did the trick.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually as i understand this the compiler goes to the path defined in your classpath and from there it seeks the whole directory trail of your package.
so u need to define the starting point to the whole package name.
so if your package starts from
c:\my documents\
for example..
then define that directory to your path.
im not sure if this is the way it is meant to be but hey this is the thing that WORKS!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic