aspose file tools*
The moose likes Beginning Java and the fly likes Head  First Java question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head  First Java question" Watch "Head  First Java question" New topic
Author

Head First Java question

Matt Pollicove
Greenhorn

Joined: Apr 14, 2008
Posts: 2
Can't believe I'm stuck and I'm only on page 15!

I've got a problem with the BeerSong application (checking Google, I see others have too, so I feel slightly better.)

I'm running Java 1.6.0_05 and can javac my code with no problems (at least after I finished de-bugging) but when I go to "java" the class file I get:

C:\java>java beersong.class
Exception in thread "main" java.lang.NoClassDefFoundError: beersong/class

Here's my source code:



(BTW, Java is not installed to c:\java, but rather somewhere in \program files. JAVA_HOME is set as: JAVA_HOME=C:\Program Files\Java\jdk1.6.0_05)

I get the feeling I'm missing something basic here.

Thanks for your help!

mp
[ April 14, 2008: Message edited by: Fred Rosenberger ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Matt Pollicove:
...C:\java>java beersong.class
Exception in thread "main" java.lang.NoClassDefFoundError: beersong/class...

Welcome to JavaRanch!

I see 2 issues here. First, Java is case sensitive, so "beersong" is not the same as "BeerSong." Second, when you run the program, do not type the ".class" extension. Just type...

java BeerSong

Let us know if this works.
[ April 14, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

also, it would help if when you posted your code, you used the ubb code tags. there are these nifty little buttons below that will drop them in your message, then just paste your code between them. it will preserve the formatting, making it much easier to read.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matt Pollicove
Greenhorn

Joined: Apr 14, 2008
Posts: 2
Ok, so here's what happens now:

C:\Program Files\Java\jdk1.6.0_05\bin>javac c:\java\BeerSong.java

C:\Program Files\Java\jdk1.6.0_05\bin>java c:\java\BeerSong
Exception in thread "main" java.lang.NoClassDefFoundError: c:\java\BeerSong
Caused by: java.lang.ClassNotFoundException: c:\java\BeerSong
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

So it seems we compile ok but we're still having trouble executing.

I'm at a loss here.

I'm reposting the code as well...

f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
code ran for me:

# tail out
2 bottles of beer on the wall
2 bottles of beer.
Take one down,
Pass it around.
1 bottles of beer on the wall
1 bottle of beer on the wall
1 bottle of beer.
Take one down,
Pass it around.
No more bottles of beer on the wall


so it looks like a path issue, unfortunately i don't do windows ...


Imagination is more important than knowledge "Albert Einstein"
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

try this:

java -cp . c:\java Beersong
Chia-you Chai
Greenhorn

Joined: Jul 22, 2006
Posts: 13
Maybe you can type the following command on your console :

set classpath = .


. means that you set the current directory as your classpath

Good luck !
Prashant Poman
Greenhorn

Joined: Feb 13, 2008
Posts: 7
If you set the PATH and CLASSPATH environment variables it will solve the problem.

There are good instructions about it on http://www.coreservlets.com in Tomcat tutorial.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36578
    
  16
Better instructions for Windows here.
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi Matt Pollicove ,

You have successfully compiled by explicitly stating your java file path
as
javac c:\java\BeerSong.java


but the reason for your problem is class file is not visible to JVM

To resolve that problem you have two options

1. Go to c:\java
then execute as java BeerSong

2. type in command prompt as
java -cp c:\java BeerSong

This is what happening :
========================

By doing the first option you will be in the directory where your class is available and by default class file is searched in current directory so problem resolved .

Alternatively you can instruct JVM to look for a directory where your class file is available which is what second option does

Hope this helps
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Head First Java question
 
Similar Threads
Beersong
7 errors for beersong source code in HFJ
BeerSong Java code
head first beersong problem?
Head first Beersong