why wrong name with NoClassDefFoundError

前端 未结 3 1867
忘了有多久
忘了有多久 2021-01-17 03:24

I created a List.java file in folder UtilityPack which contains this code

package Utilities;
public class List
{
    private class          


        
相关标签:
3条回答
  • 2021-01-17 03:48

    Use the complete name of the class to lauch your program :

     java Utilities.List
    

    But the folder name should also match the package name.

    0 讨论(0)
  • 2021-01-17 03:52

    Your directory structure needs to follow your Java package pathing. IOW, if the class Listis in the package Utilities, you need to situate it in a directory called Utilities, which should be at the root level of your project, i.e. the path of the source file should be C:\UtilityPack\Utilities\List.java. When you are in C:\UtilityPack (project root), you compile and run List by referencing it as Utilities.List.

    You might also consider using Eclipse, it will prevent this sort of things from happening, or any other Java IDE.

    0 讨论(0)
  • 2021-01-17 04:02

    You need the fully qualified name e.g.

    java -cp . Utilities.List
    

    i.e. you're telling the JVM to look from the current direct (-cp .) for a class Utilities.List, which it will expect in the file Utilities\List.class.

    To be more consistent you should put the .java file under a Utilities directory (yes - this is tautologous - the package specifies this, but it's consistent practise).

    I would also avoid calling your class List. At some stage you're going to import a java.util.List and it'll all get very confusing!

    Finally, as soon as you get more than a couple of classes, investigate ant or another build tool, and separate your source and target directories.

    0 讨论(0)
提交回复
热议问题