Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:14:35+00:00 2026-05-24T11:14:35+00:00

This seems like a simple problem, but somehow I’m having issues with this code.

  • 0

This seems like a simple problem, but somehow I’m having issues with this code. I’m getting back into Java after a few years, and I’m making a 2D game. In it, I have a main driver class called SnakeGame that loads a new instance of the class GameBoard.

SnakeGame.java

package snake2;
import javax.swing.JFrame;

public class SnakeGame extends JFrame {

public SnakeGame() {
    add(new GameBoard());

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(320, 340);
    setLocationRelativeTo(null);
    setTitle("Snake Game");

    setResizable(false);
    setVisible(true);
}

public static void main(String[] args) {
    new SnakeGame();
}
}

In the same directory lies GameBoard.java, which has a constructer with no required parameters:

public GameBoard() {
    [... more code ...]
}

Edit

Both GameBoard.java and SnakeGame.java have package snake2; at the first line of their files.

However, I keep receiving the following error:

SnakeGame.java:7: cannot find symbol
symbol  : class GameBoard
location: class snake2.SnakeGame
    add(new GameBoard());
            ^
1 error

Edit #2

I’ve tried to add it to my class path, using java -cp . GameBoard after javac. Here’s the first line of the terminal’s scary looking and unnecessarily verbose response:

Exception in thread "main" java.lang.NoClassDefFoundError: GameBoard (wrong name: snake2/GameBoard)

This is as if I’ve misspelled the class, or misspelled a file name. Although, to my knowledge, I haven’t done either. Is there some other problem with my code that I haven’t noticed?

Thanks for any help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:14:36+00:00Added an answer on May 24, 2026 at 11:14 am

    Besides being in the same directory, do you have a package statement in GameBoard.java?


    Edit for running the program —

    @Stephen: I’m running java, and all the files are in the same directory.

    From @kbolino

    You must put your source files in a snake2 directory and run javac from the parent directory […]

    You must also invoke java, not just javac, with the correct package and classpath.

    Examples that worked for me, after creating SnakeGame and GameBoard stubs:

    1. Current dir is projects which is the parent of the snake2 dir where the files are:
      java snake2.SnakeGame

    2. Current dir is snake2 where I was editing the files:
      java -cp .. snake2.SnakeGame

    For case #2 since you’re in the package dir you have to put the parent dir in the classpath.

    .

    Adding a ridiculous amount of information showing my session with both the javac compile command and java run. You don’t need -cp for compiling as suggested by some.


    [~/tests/java/snake2]$ pwd
    /home/stephenp/tests/java/snake2
    
    [~/tests/java/snake2]$ cat GameBoard.java
    
    package snake2;
    
    class GameBoard {
    
        private static int instanceCount = 0;
    
        GameBoard() {
            GameBoard.instanceCount++;
        }
    
        void howMany() {
            System.out.println(instanceCount + " GameBoards have been created.");
        }
    }
    
    [~/tests/java/snake2]$ cat SnakeGame.java
    
    package snake2;
    
    public class SnakeGame {
    
        private GameBoard board = null;
    
        public SnakeGame() {
            this.board = new GameBoard();
        }
    
        void report(String msg) {
            System.out.println(msg);
        }
    
        public static void main(String[] args) {
            SnakeGame game = new SnakeGame();
            game.report("I exist");
            game.board.howMany();
        }
    }
    
    
    [~/tests/java/snake2]$ ls -1
    GameBoard.java
    SnakeGame.java
    
    [~/tests/java/snake2]$ javac *.java
    
    [~/tests/java/snake2]$ ls -1
    GameBoard.class
    GameBoard.java
    SnakeGame.class
    SnakeGame.java
    
    [~/tests/java/snake2]$ java SnakeGame
    Exception in thread "main" java.lang.NoClassDefFoundError: SnakeGame (wrong name: snake2/SnakeGame)
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
            at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
            at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
            at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    
    [~/tests/java/snake2]$ java snake2.SnakeGame
    Exception in thread "main" java.lang.NoClassDefFoundError: snake2/SnakeGame
    Caused by: java.lang.ClassNotFoundException: snake2.SnakeGame
            at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    
    [~/tests/java/snake2]$ java -cp .. snake2.SnakeGame
    I exist
    1 GameBoards have been created.
    

    Packaging

    Package up your application in a .jar file along with a Manifest that specifies the main class to run, then you can run your program just using

    java -jar myprogram.jar
    

    To do that, create a file Manifest.txt in the directory above your package directory

    [~/tests/java/snake2]$ cd ..
    
    [~/tests/java]$ ls -1F
    Manifest.txt
    snake2/
    
    [~/tests/java]$ cat Manifest.txt
    Main-Class: snake2.SnakeGame
    
    [~/tests/java]$ jar cfm myprogram.jar Manifest.txt snake2/*.class
    
    [~/tests/java]$ jar tf myprogram.jar
    META-INF/
    META-INF/MANIFEST.MF
    snake2/GameBoard.class
    snake2/SnakeGame.class
    

    You can also wrap a script (shell script, batch file, etc.) around that so you just run myprogram and it runs java -jar myprogram.jar

    This would all be part of your build process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.