Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java question

 
Matt Pollicove
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matt Pollicove
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:

java -cp . c:\java Beersong
 
Chia-you Chai
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47261
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better instructions for Windows here.
 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic