File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Beginning to use classes

Alex Richards

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
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:
java.lang.NoClassDefFoundError: exercise9_1/Main
Caused by: java.lang.ClassNotFoundException: exercise9_1.Main
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
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

Joined: Jan 08, 2009
Posts: 1579

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

Alex Richards

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

Joined: Oct 02, 2003
Posts: 11955

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

Joined: Jan 08, 2009
Posts: 1579

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

Joined: Aug 16, 2005
Posts: 15081

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 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
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

Joined: Oct 13, 2005
Posts: 46337
Agree: NotePad++ is a nice tool. And welcome to the Ranch
I agree. Here's the link:
subject: Beginning to use classes
jQuery in Action, 3rd edition