简体   繁体   English

运行简单的Java程序时出现NOClassDefFound错误

[英]NOClassDefFound error while running a simple java program

This code is compile fine, but whenever I try to run, it gives an error says NoClassDefFound. 这段代码可以很好地编译,但是每当我尝试运行时,都会显示NoClassDefFound错误。 What is the possible reason and solution, please explain. 请问可能的原因和解决方法。

package myPack;

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

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

}

Are you sure you're calling with the correct package name prefix (ie "java myPack.PasswordVerification")? 您确定使用正确的软件包名称前缀(即“ java myPack.PasswordVerification”)进行呼叫吗?

Also, there are some improvements you can make- 此外,您可以进行一些改进-

  • Testing a string variable, better to test the constant against the variable- eg 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. 测试字符串变量,最好是针对变量测试常量,例如if(“ prajnut” .equals(userId)而不是if(userId.equals),因为如果您碰巧传入空值,则第一种形式不受NullPtrExceptions的影响串。
  • you can simplify by removing the "else" clause -you really only need 1 line 您可以通过删除“ else”子句来简化-您实际上只需要1行

    return "prajnut".equals(id)&& "password".equals(pass): 返回“ prajnut” .equals(id)&&“ password” .equals(pass):

Make sure you are in the directory that contains the myPack folder. 确保您位于包含myPack文件夹的目录中。 You should not be within the myPack folder. 您不应位于myPack文件夹中。 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. 我只是在Linux机器上尝试过,它看起来自动将工作文件夹包括在类路径中,但前提是未设置CLASSPATH环境变量。 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". 如果您在其他类似Unix的操作系统中键入“ dir”(Windows)或“ ls”,则应该看到目录名称“ myPack”。

then type java myPack.PasswordVerification 然后输入java myPack.PasswordVerification

here some suggestion to code better and respect the Java coding conventions 这里有一些建议可以更好地编码并遵守Java编码约定

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"));
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM