NOClassDefFound error while running a simple java program

久未见 提交于 2019-11-29 18:44:49
Steve B.

Are you sure you're calling with the correct package name prefix (i.e "java myPack.PasswordVerification")?

Also, there are some improvements you can make-

  • Testing a string variable, better to test the constant against the variable- e.g. if ("prajnut".equals(userId) rather than if (userId.equals), as the first form is immune to NullPtrExceptions if you happen to pass in an empty string.
  • you can simplify by removing the "else" clause -you really only need 1 line

    return "prajnut".equals(id)&& "password".equals(pass):

Make sure you are in the directory that contains the myPack folder. You should not be within the myPack folder. I just tried it on my linux machine and it looks like it automatically included the working folder in the classpath, but only if the CLASSPATH environment variable is NOT set. If it is set, then you should either add the current folder to it, or specify the classpath on the command line as follows:

java -cp . myPack.PasswordVerification

Be sure that you're at the root project.

if you type "dir" ( windows) or "ls" other Unix-like os, you should see a directory name "myPack".

then type java myPack.PasswordVerification

here some suggestion to code better and respect the Java coding conventions

package myPack;

public class PasswordVerification{


    public boolean verify(String usrId, String pass){
        if("pranjut".equals(usrId) && "password".equals(pass)){
            return true;
        }
        return false;

    }

    public static void main(String[] main){
       PasswordVerification vp=new PasswordVerification();
       System.out.println(vp.verify("pranjut","password"));
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!