What's wrong with my java code

I am learning java. My example directory looks like this:

./CSI
./src/yxy
./src/yxy/pack
./src/yxy/pack/testf
./src/yxy/pack/testf/Person.java
./Hello.java
./Hello.class

./testpack
./testpack/yxy
./testpack/yxy/pack
./testpack/yxy/pack/testf
./testpack/yxy/pack/testf/Person.class
./testpack.jar

The src directive is used as a library. It provides the Person class.
The src output file is located in the testpack directory.
I am generating testpack.jar according to the testpack directive.
Hello.java is the main java file. It uses the Person class.

Download source package Person.java :

package yxy.pack.testf;

public class Person {
  private String name;

  public Person(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

      

Download source package Hello.java :

import java.io.*; //IOException
import java.util.*; //Enumeration

import yxy.pack.testf.Person;

public class Hello{

    public static void main(String[] args)throws IOException  {
        Person p=new Person("abc");
        Class cls=Person.class;
        ClassLoader clsLoader=cls.getClassLoader();
        String clsFile=cls.getName();
        System.out.println("end");
    }
}

      

I am using the following method to generate testpack.jar in the current directory.

javac -verbose -d testpack src \ yxy \ pack \ testf \ Person.java
cd testpack
jar -cvf testpack.jar./yxy
move testpack.jar./../testpack.jar
cd ..

I am using the following method to create and run my application:

javac -classpath. \ testpack.jar Hello.java
java -classpath. \ testpack.jar Hello

this is the error i am getting:

    Exception in thread "main" java.lang.NoClassDefFoundError: Hello  
    Caused by: java.lang.ClassNotFoundException: Hello  
            at java.net.URLClassLoader $ 1.run (Unknown Source)  
            at java.security.AccessController.doPrivileged (Native Method)  
            at java.net.URLClassLoader.findClass (Unknown Source)  
            at java.lang.ClassLoader.loadClass (Unknown Source)   
            at sun.misc.Launcher $ AppClassLoader.loadClass (Unknown Source)  
            at java.lang.ClassLoader.loadClass (Unknown Source)  
    Could not find the main class: Hello. Program will exit.

I don't understand why this is happening. How can I fix it?

java Hello

    Exception in thread "main" java.lang.NoClassDefFoundError: yxy / pack / testf / Person

            at Hello.main (Hello.java:10)
    Caused by: java.lang.ClassNotFoundException: yxy.pack.testf.Person
            at java.net.URLClassLoader $ 1.run (Unknown Source)
            at java.security.AccessController.doPrivileged (Native Method)
            at java.net.URLClassLoader.findClass (Unknown Source)
            at java.lang.ClassLoader.loadClass (Unknown Source)
            at sun.misc.Launcher $ AppClassLoader.loadClass (Unknown Source)
            at java.lang.ClassLoader.loadClass (Unknown Source)
            ... 1 more

How can I fix this?

=== Anyway, I found out how to fix it. Just use the following in cmd.

>java -classpath testpack.jar;. Hello

      

I have to add testpack.jar and the current path. to the classpath.

-4


source to share


1 answer


Well, that could be a lot. If you are new to Java, I would recommend starting with an IDE, but if you don't want to, here is a link to 3 ways to resolve NoClassDefFoundError in Java

Here's also an excerpt from another user who explains why this error occurs:



I always get this error when I create a class file in a package and then try to run the program from within the package. The solution needs to go up one level in your filesystem so that you are at the package level, not the package content.

For example, if your package name is Hello and your class file is HelloWorld, your program contains the line:

package greetings;

The package name corresponds to a directory on your filesystem eg. C: \ Java \ greetings. Your class file is inside this directory, that is, its filename is C: \ Java \ greetings \ HelloWorld.class.

If you position c: \ Java \ greetings and then do:

java HelloWorld

you get NoClassDefFound error. If you go to one directory, in this case to C: \ Java instead of C: \ Java \ greetings, then do this:

java greetings.HelloWorld

Your program should work for you.

+2


source







All Articles