This code is compile fine, but whenever I try to run, it gives an error says 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"));
}
}
解决方案
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
本文详细解释了当Java程序编译成功但运行时遇到NoClassDefFound错误的原因,重点在于类路径设置和工作目录。解决方法包括确保包含正确包的文件夹在类路径中,以及处理CLASSPATH环境变量。
4392

被折叠的 条评论
为什么被折叠?



