使用其他类并在没有main方法的情况下编译时出错

时间:2015-02-18 20:52:06

标签: java packages

我有一个Game类扩展了JApplet类,我希望能够编译它,但我还需要使用我的Character类。问题是我在编译Character类时遇到错误,因为我没有主要方法。汇编Character的错误是:

Exception in thread "main" java.lang.NoClassDefFoundError: Character (wrong name: kingdomofcarthania/Character)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
[Finished in 0.9s with exit code 1]

编译Game类时的错误是:

Game.java:3: cannot find symbol
symbol  : class Character
location: package kingdomofcarthania
import kingdomofcarthania.Character;
                     ^
 Game.java:10: cannot find symbol
 symbol  : constructor Character()
 location: class java.lang.Character
    Character c = new Character();
                      ^
 Game.java:11: cannot find symbol
 symbol  : method resetCharacter()
 location: class java.lang.Character
    c.resetCharacter();
     ^
3 errors
[Finished in 0.7s with exit code 1]

以下是我的Applet的一些可能有帮助的片段

Game.java

package kingdomofcarthania;

import kingdomofcarthania.Character;
import javax.swing.JApplet;
import java.awt.*;

public class Game extends JApplet{

public static void main(String[] args) {
    Character c = new Character();
    c.resetCharacter();
    }
}

Character.java

package kingdomofcarthania;

import kingdomofcarthania.*;
import java.util.ArrayList;

public class Character{
 private int xCoord;
 private int yCoord;
 private int currentZone;
 //private Map world;
 //private ArrayList<Zone> map;
 //private Inventory inventory;
 private int gold;

 //private ArrayList<Follower> followers;

 private int might;
 private int wisdom;
 private int dexterity;
 private int toughness;
 private int vigor;
 private int maxVigor;

 private int level;
 private int reputation;
 private int greatness;
 private int greatnessNeeded;

 private String title;


 public Character(){
 }

//some methods
}

我觉得我犯了一个愚蠢的错误,任何帮助都会非常感激。

编辑:

这些是我在更改某些内容后遇到的新错误:

Game.java:9: cannot find symbol
symbol  : class Champion
location: package kingdomofcarthania
        kingdomofcarthania.Champion c = new kingdomofcarthania.Champion();
                      ^
 Game.java:9: cannot find symbol
 symbol  : class Champion
 location: package kingdomofcarthania
      kingdomofcarthania.Champion c = new kingdomofcarthania.Champion();
                                                          ^
 2 errors

1 个答案:

答案 0 :(得分:0)

您的角色类与 java.lang.Character

冲突

使用explictly kingdomofcarthania.Character而不仅仅是Character。

java.lang 包中的

类不需要导入,因此会发生冲突。

您不需要导入相同包的类。

尝试删除

import kingdomofcarthania.Character;

import kingdomofcarthania.*;

最好的方法是不创建一个Character类,而是创建一个MyCharacter或KingDomCharacter来避免命名冲突。

最终在此处查看相关问题讨论:http://www.coderanch.com/t/569181/java/java/class-conflicts