如何解决此错误引起的:java.lang.ClassNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22605831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:48:22  来源:igfitidea点击:

How to resolve this error Caused by: java.lang.ClassNotFoundException

javaclasspathclassnotfoundexception

提问by vaj oja

I wrote a console application helloworld.javaand ran javac helloworld.java,and then java helloworld. This worked fine.

我写了一个控制台应用程序helloworld.java并运行javac helloworld.java,然后java helloworld. 这工作得很好。

I then created a package com.abc.projectand moved helloworld.java into it(the package import statement is correctly generated which is package com.abc.project;). And then i ran javac helloworld.javathis worked also fine and generated the class properly.

然后我创建了一个包com.abc.project并将 helloworld.java 移动到其中(包导入语句正确生成,即package com.abc.project;)。然后我运行javac helloworld.java它也工作正常并正确生成了类。

However, when I ran java com.abc.project.helloworldfrom the console, it threw an "class not found" error.

但是,当我java com.abc.project.helloworld从控制台运行时,它抛出了“找不到类”错误。

Please can anyone advise what is the problem?

请任何人都可以建议是什么问题?

采纳答案by Keerthivasan

Try running

尝试跑步

java -cp ABSOLUTE_PATH com.abc.project.helloworld

java -cp ABSOLUTE_PATH com.abc.project.helloworld

Where ABSOLUTE_PATH refers to the directory where the class files along with packages are present. say it is bindirectory where the class files are generated along with same directory structure as source files

其中 ABSOLUTE_PATH 是指存在类文件和包的目录。说它bin是生成类文件的目录以及与源文件相同的目录结构

回答by Oliver

First Please name Class with Capital Letter like HelloWorld.java

首先请用大写字母命名类,如 HelloWorld.java

If you are in a folder '/myjava' in cmd and your .java files is in this folder then do this in cmd

如果您在 cmd 中的文件夹“/myjava”中并且您的 .java 文件在此文件夹中,则在 cmd 中执行此操作

D:\myjava\:> javac -d HelloWorld.java

This will create correct package Structure for you Then don't go anywhere from the same location do this

这将为您创建正确的包结构然后不要从同一位置去任何地方执行此操作

D:\myjava\:>  java com.abc.project.HelloWorld

It should work fine!!

它应该工作正常!!