• Post Reply Bookmark Topic Watch Topic
  • New Topic

DrumKit OOP from "Head First Java"  RSS feed

 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on the DrumKit exercise from page 43, and I've ran the code verbatim as it appears on page 46




---

I'm running this code in Eclipse, and the following errs I get are as follows...

"at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Drumkit$DrumKitTestDrive. Program will exit."

What the heck? Why can't it find the main class?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I ran it, I got...

bang
ding


Works for me.

Henry
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like we'll need a bit more information for this one. Have you run a program in the eclipse IDE before? It could be an IDE setup issue. Could you provide any details about how you are running it? The stack trace you provided also doesn't appear to be complete. More information on what the exception was would help us figure out what might have gone wrong.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As somebody else said, the code you provided runs successfully.

Stop using IDEs.

You might have accidentally got your {} out of order and put a DrumkitTestDrive class inside Drumkit. Or maybe you have confused Eclipse by giving it an instruction to look for such an inner class.
 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:...Have you run a program in the eclipse IDE before? It could be an IDE setup issue. Could you provide any details about how you are running it? The stack trace you provided also doesn't appear to be complete. More information on what the exception was would help us figure out what might have gone wrong.


sorry, here's the full err message:

"Exception in thread "main" java.lang.NoClassDefFoundError: Drumkit$DrumKitTestDrive
Caused by: java.lang.ClassNotFoundException: Drumkit$DrumKitTestDrive
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Drumkit$DrumKitTestDrive. Program will exit."


Can't find main class, and I'm like... "WTF?"


I'm new to Linux/Ubuntu, thought I installed Eclipse right... maybe I didn't? It could have something to do with the way I set up the work environment, though I experienced no trouble at all with the following "Hello World" program...



runs 100%, no trouble. Compiler has no problem finding the main class, what gives?

---

II: Campbell Ritchie,

Why stop using IDE's?


if you guys are saying the code I provided runs successfully, then it's got to be a set up issue. How to resolve?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that your run configuration for this application is not set up properly. Eclipse will try to find the main method inside the class named by the filename. You have to tell it if the main method is actually somewhere else. Try putting DrumKitTestDrive in its own file instead of inside Drumkit. Much easier this way. Then you should be able to run simply by right-clicking on DrumKitTestDrive and selecting Run As -> Java Application.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rossco giordano wrote: . . . . Why stop using IDE's? . . .
Because its complicated features are getting in the way of your learning. When you are getting Java™ code always to run correctly from the command line or terminal, then go back to IDEs.

Your code ran when I tried it on Eclipse, so you have either mistakenly put classes into each other, or mistakenly told the IDE you want to run an inner class’ main method.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rossco giordano wrote: . . . runs 100%, no trouble. Compiler has no problem finding the main class, what gives? . . .
That has nothing to do with the compiler, but the JVM.
 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. It works.

I had to put the main DrumKitTestDrive class into a separate file than I did for the Drumkit class.

---

WHY is this necessary? is it just necessary to separate the main class file from other classes in Eclipse?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to keep them in the same file. What was the name of your java file when you tried it the first time? The name of the file should match the name of the class you were trying to run (ie. DrumKitTestDrive.java). This is the only class in that file that can be public, as well.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is usually a good idea to have a file for each class, but sometimes it makes more sense to put several package-private classes in one file. With most compilers, you must have a separate file for each public class.
 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bullers,

Originally, the class file wasn't called "DrumKitTestDrive" (and it didn't run), so I changed the name of the .java file to "DrumKitTestDrive" (which includes both classes), and it runs just fine.

---

Thanks everyone for the help!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and didn’t you get a compiler error?
 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
rossco giordano wrote: . . . runs 100%, no trouble. Compiler has no problem finding the main class, what gives? . . .
That has nothing to do with the compiler, but the JVM.


It was my (mis)understanding(?) that all the source code was checked for errors, then converted to byte code via the compiler. That the JVM wasn't in charge of this process, but whose role it was to take that compiled source code and execute it through the JVM as byte code. I never thought it was the role of the JVM to check for errors and then compile source code into bytecode (I thought the JVM just executed byte code), but what do I know? just a low-level C programmer trying to make sense of the Java situation.
 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Could not find the main class: Drumkit$DrumKitTestDrive. Program will exit."

I had named the file "Drumkit", so Eclipse was searching for a main class called "DrumKit"; though the main class was actually named "DrumKitTestDrive" (you know, the class with the public static void main() method defined)


thanks to Jason Bullers: "You should be able to keep them in the same file. What was the name of your java file when you tried it the first time? The name of the file should match the name of the class you were trying to run (ie. DrumKitTestDrive.java). This is the only class in that file that can be public, as well."

We were able to discover that there was a naming convention err being thrown on the behalf of Eclipse. So, I was wondering, is it like this on all Java IDE's?


And I'm going to have to say: I'm going to continue working in IDE's (and getting REAL comfortable with them) unless someone has incredible factoids leading AGAINST their use. popular argument is that it puts the learner at a disadvantage or creates bad habits. I personally do not think you can code PERIOD without knowing how to code, it's as simple as that. If a particular tool/method works for someone: okay, you may just have your own preferences.

I think there's a real difference between using a program like DreamWeaver and hardcoding HTML (or PHP) through notepad, which I've done for many hours (creating unique file-structures based on tech specs. and feature considerations). But I also figured out IDE's can save so much on time, and I'd like to put every tool to my use for best advantage. They're also the staple of teams, and I believe that's for good reason: It's highly collaborative, and when you got the best minds working through such IDE's, using it as a means to exchange such information- I don't think they could be THAT wrong.


If anything, I think I might be accused of running before taking baby steps; I get that a lot. I just like to go deep like it aint no joke... jump all the way in, sink or swim, win or lose.

I just have fun here.
 
rossco giordano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's a lot of rhetoric thrown around in various industries.

anyone read Joel Spolsky?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rossco giordano wrote:We were able to discover that there was a naming convention err being thrown on the behalf of Eclipse. So, I was wondering, is it like this on all Java IDE's?


I'm pretty sure that's a java thing, not an IDE thing. This is why others have suggested taking a step back from the IDE; you'd have encountered the very same problem trying to run your application from the command line. The advantage is that you are dealing with java more directly and nothing is hidden away from you. You also eliminate the possibility that the errors you hit were caused by an incorrect setup.
 
Dylan Martinez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why you have to set "snare" back to false afterward, and then include the if statement at the bottom. :S Why?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!