• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

How Do I Do Classes In Packages from the Command-Line?

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm studying for the SCJP 6 exam. K&B Practice exams says not to use an IDE but instead to use the command-line (this is so dumb). Anyway, I'm trying to compile and run a simple application using the command-line.



The above code is easy to compile and easy to run from the command-line, just by using javac and java as you would think, but when I try to put my code into a package, it compiles, but I don't know how to run it.



I placed the code in com/kaydell/scjp/

I tried just running it with:

java PathNames

and it throws an exception:


kaydells-mac-mini:scjp kaydell$ pwd
/Volumes/Kaydell2/PracticeExams/com/kaydell/scjp
kaydells-mac-mini:scjp kaydell$ javac PathNames.java
kaydells-mac-mini:scjp kaydell$ java PathNames
Exception in thread "main" java.lang.NoClassDefFoundError: PathNames (wrong name: com/kaydell/scjp/PathNames)
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)
kaydells-mac-mini:scjp kaydell$

So, I tried:

java com.kaydell.scjp.PathNames

And I got a similar result.

How do I run Java code using the command-line when the code is in a package?

Best Regards,

-- Kaydell
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 2nd command you used is fine, you just need to run if from the correct directory. Try to run the command from /Volumes/Kaydell2/PracticeExams/ directory...
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule to remember here is that your current directory must be the super directory of the root directory ( com in your case ) for the package.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

-- Kaydell
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic