Forums Register Login

DrumKit OOP from "Head First Java"

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send

When I ran it, I got...

bang
ding



Works for me.

Henry
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
And welcome to the Ranch
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
. . . and didn’t you get a compiler error?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
"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.
+Pie Number of slices to send: Send
I think there's a lot of rhetoric thrown around in various industries.

anyone read Joel Spolsky?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
You need make  public class DrumKitTestDrive
+Pie Number of slices to send: Send
Welcome to the Ranch

Why does the test drive class need public access?
+Pie Number of slices to send: Send
I used intellij but the thing is:

Create a new class file under sources and label it DrumKitTestDrive, don't run them under one file.

Mine compiled when I run them in two seperate class files.
My, my, aren't you a big fella. Here, have a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2534 times.
Similar Threads
Boolean question
Head First Exercise Pg 43
Method help: how does this work?
class and testclass
Head First Java Beginnner's Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:58:42.