Search code examples
javapackagenoclassdeffounderror

Multilevel package - Java


I am just a newbie in java And i am using packages for the first time. I am trying to make packages in this hierarchy:

/chess/game/Pieces.class

/chess/Board.class imports Pieces

/Game.java imports Board

I compiled using just javac filename.java to get the class files.

Game.java doesn't use any of the methods and variables of the Pieces.class just to be sure. All the Classes,Functions and Variables are public. I tested the board.java with a main and it works perfectly fine.But when i use it as a package it doesn't work and i get class not found.

Exception in thread "main" java.lang.NoClassDefFoundError: game/Pieces
        at chess.Board.initializeboard(Board.java:31)
        at Game.main(Game.java:14)
Caused by: java.lang.ClassNotFoundException: game.Pieces
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at chess.Board.initializeboard(Board.java:31)
        at Game.main(Game.java:14)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.execute(Main.java:415)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.run(Main.java:192)
        at jdk.compiler/com.sun.tools.javac.launcher.Main.main(Main.java:132)

I cannot import both the classes in Game.java cause i made a // package game; in Pieces.class so i cannot import it as // import chess.game.Pieces; in Game.java .

If this is illegal.Is there some other OOP approach?

EDIT- The code i used- Pieces.java -(compiled it to .class file)

package game;

import javax.swing.JButton;

public class  Pieces{
public enum Type {ROOKE,KNIGHT,BISHOP,KING,QUEEN,PAWN};

public boolean alive = true;
public boolean white = false;
public boolean firstmove = false;
public int mx;
public int my;
public Type types;

public JButton button=new JButton();
public void setButton(JButton b){
                        this.button=b;}  

}

Board.java - (compiled it to .class file)

package chess;

import game.*;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;

public class Board{

public void initializeboard(JButton [][] spots){
JFrame f = new JFrame("CHESS");
f.setVisible(true);
f.setSize(800,800);

GridLayout layout =new GridLayout(8,8,1,1);
f.setLayout(layout);

for(int ver=0;ver<8;ver++){
  for(int hor=0;hor<8;hor++){
      JButton button = new JButton();
      if((ver+hor)%2==0){
                    button.setBackground(Color.WHITE); }
      else{
           button.setBackground(new Color(255,205,51)); }
 Pieces p =new Pieces();
 spots[ver][hor] = button;
 p.setButton(button);
 f.add(button);
 } //close for loop
 } // close for loop
 f.revalidate();
 } //  close initializeboard
 }// close board

Game.java -

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;

import chess.Board;
public class Game{
public static void main(String [] args){

JButton [][] spots =new  JButton [8][8];

Board b =new Board();
b.initializeboard(spots); 
}
}

Solution

    1. Go to directory containing file Game.java
    2. Enter this command
      javac chess/game/Pieces.java
    3. After that, enter this command
      javac -cp . game/Board.java
    4. After that, enter this command
      javac -cp . Game.java