• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Very beginner question regarding path

 
Max Benjamin
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 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
 
Rodrigo Alvarez
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 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.
    [ April 12, 2006: Message edited by: marc weber ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic