Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beginning to use classes

 
Alex Richards
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you executing it from command prompt? If yes what is your working directory?
 
Alex Richards
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using NetBeans IDE.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is fine. My guess is that if you take out the package statement, it will run as you expect.

 
Swastik Dey
Rancher
Posts: 1602
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you saved Tile.java inside package exercise9_1?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: NotePad++ is a nice tool. And welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic