• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Beginning to use classes

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using NetBeans IDE.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you saved Tile.java inside package exercise9_1?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree: NotePad++ is a nice tool. And welcome to the Ranch
 
reply
    Bookmark Topic Watch Topic
  • New Topic