• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Very beginner question regarding path

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just trying to get started with Java and have run into some difficulties already. I think the problem is that I haven't set the environment path correctly. When I attempt to run a short program(from the headfirst java book) I get the following error:

Welcome to Darwin!
Max-Bejnamins-Computer:~ maxbenjamin$ java /Users/maxbenjamin/Desktop/BeerSong.class
Exception in thread "main" java.lang.NoClassDefFoundError: /Users/maxbenjamin/Desktop/BeerSong/class


I tried to set the PATH variable by altering the /private/etc/profile to:

# System-wide .profile for sh(1)

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/bin"
export PATH
[ -r /etc/bashrc ] && source /etc/bashrc

I'm on OSX 10.3.9 using the bash shell.
If anyone could help me out I'd be very thankful.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing you have to know is that you don't need to specify ".class" when running the program.
Try again with : java /Users/maxbenjamin/Desktop/BeerSong
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max, welcom in the wonderful world of Java

try to :

cd /Users/maxbenjamin/Desktop/

then :

java nameOfTheApplicationWithDotClass

note that this assumes that you are using the default package (i.e. not "package" keyword at the beginnig of the .java file).

If you file were using the package "name.of.the.package" and were name "app.java", you would have to:

* put this line in the very beginning of the java file:
package name.of.the.package

* create the directory /Users/maxbenjamin/Desktop/name/of/the/package
* put the java file in /Users/maxbenjamin/Desktop/name/of/the/package
* compile it
* cd /Users/maxbenjamin/Desktop/
* execute : java name.of.the.package.app


Cheers
 
Rodrigo Alvarez
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

java nameOfTheApplicationWithDotClass



oups, sorry , I meant WITHOUT the .class, so BeerSong in your case
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Max Benjamin:
...I'm on OSX 10.3.9 using the bash shell...


Welcome to JavaRanch!

Macs come ready to go for Java, so you shouldn't need to change any path settings. Also, I wouldn't complicate things with packages until you get to that point in the book. For now...
  • Change the directory (cd) to where your Java files are (this will save you some command line typing).
  • Compile with javac, including the .java extension on the file name.
  • Run with java, using no extension on the file name. (I think this was your original problem.)
  • ...$ cd /Users/maxbenjamin/Desktop
    ...$ javac BeerSong.java
    ...$ java BeerSong

    Also note that we have a Mac OS forum for Mac-specific Java questions. I'll move this post there for you.
     
    What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
    Enterprise-grade Excel API for Java
    https://products.aspose.com/cells/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!