多层级包 – Java

huangapple go评论94阅读模式
英文:

Multilevel package - Java

问题

以下是翻译好的部分:

我只是一个在 Java 中的新手,第一次使用包。我试图按照以下层次结构创建包:

/chess/game/Pieces.class

/chess/Board.class 导入 Pieces

/Game.java 导入 Board

我只是使用 javac 文件名.java 编译来获取类文件。

为了确保,Game.java 并未使用 Pieces.class 中的任何方法和变量。
所有的类、函数和变量都是公共的。
我测试了 board.java,使用一个主函数,它运行得很正常。但当我将其作为一个包使用时,它就无法正常工作,并且会出现“找不到类”的错误。

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)

我不能在 Game.java 中同时导入这两个类,因为我在 Pieces.class 中加了一个 // package game; 的注释,所以我不能在 Game.java 中使用 // import chess.game.Pieces; 导入它。

如果这是不合法的,请问是否有其他的面向对象编程方法?

编辑 -
我使用的代码如下:
Pieces.java -(编译成 .class 文件)

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 -(编译成 .class 文件)

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 -

```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); 
}
}
英文:

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 &quot;main&quot; 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(&quot;CHESS&quot;);
f.setVisible(true);
f.setSize(800,800);
GridLayout layout =new GridLayout(8,8,1,1);
f.setLayout(layout);
for(int ver=0;ver&lt;8;ver++){
for(int hor=0;hor&lt;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); 
}
}

答案1

得分: 0

  1. 转到包含文件 Game.java 的目录。
  2. 输入以下命令:
    javac chess/game/Pieces.java
  3. 然后,输入以下命令:
    javac -cp . game/Board.java
  4. 然后,输入以下命令:
    javac -cp . Game.java
英文:
  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

huangapple
  • 本文由 发表于 2020年5月31日 00:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/62105771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定