aspose file tools*
The moose likes Beginning Java and the fly likes Beginning to use classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Beginning to use classes" Watch "Beginning to use classes" New topic
Author

Beginning to use classes

Alex Richards
Greenhorn

Joined: Mar 07, 2011
Posts: 9
I'm doing an exercise in a text box with these instructions:
Exercise 9.1: In the board game Scrabble1, each tile contains a letter, which is
used to spell words, and a score, which is used to determine the value of a word.
a. Write a definition for a class named Tile that represents Scrabble tiles. The
instance variables should be a character named letter and an integer named
value.
b. Write a constructor that takes parameters named letter and value and initializes
the instance variables.
c. Write a method named printTile that takes a Tile object as a parameter and
prints the instance variables in some reader-friendly format.
d. Write a method named testTile that creates a Tile object with the letter Z
and the value 10, and then uses printTile to print the state of the object.

The point of this exercise is to practice the mechanical part of creating a new class
definition and code that tests it.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So here's my code for this program:


So, my code compiles fine, but when I run it, I get this error:
run:
java.lang.NoClassDefFoundError: exercise9_1/Main
Caused by: java.lang.ClassNotFoundException: exercise9_1.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: exercise9_1.Main. Program will exit.
Exception in thread "main" Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

Any ideas on whats going wrong here? This is my first time writing my own class with my own class objects, so I have no clue what I'm doing wrong right now.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Are you executing it from command prompt? If yes what is your working directory?


Swastik
Alex Richards
Greenhorn

Joined: Mar 07, 2011
Posts: 9
I'm using NetBeans IDE.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

Your code is fine. My guess is that if you take out the package statement, it will run as you expect.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1398
    
    6

Have you saved Tile.java inside package exercise9_1?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

The error message looks like your IDE is trying to run a class named exercise9_1.Main, but your class is not named Main, it is named Tile instead.

Change the run configuration to make sure that it tries to run class Tile instead of class Main, or rename your class to Main instead of Tile.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
I think Jesper is correct that you ought to run the Tile class.

I notice you have the main method there, presumably simply for testing.

I would suggest at this stage, you get rid of the package declarations, and stop using NetBeans. Get a decent text editor and run the whole thing from the command line.
jake dickens
Ranch Hand

Joined: Mar 23, 2011
Posts: 30
Notepad++ is the best one i know of can you can download the nppexec plugin it takes you to the command line within the program just press f6 after installing the plugin
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Agree: NotePad++ is a nice tool. And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginning to use classes
 
Similar Threads
Sun One Studio and executing main class...
main() & package question
Using Lamdaj of Google gave me an error of ClassLoader, can any body please help me
package access trouble
class file portability