aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How Do I Do Classes In Packages from the Command-Line? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How Do I Do Classes In Packages from the Command-Line?" Watch "How Do I Do Classes In Packages from the Command-Line?" New topic
Author

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

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59

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

Joined: Nov 18, 2006
Posts: 689

Thanks guys.

-- Kaydell
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Do I Do Classes In Packages from the Command-Line?