Having trouble with java.lang.NoClassDefFoundError (wrong name:) when running from cmd

huangapple 未分类评论52阅读模式
英文:

Having trouble with java.lang.NoClassDefFoundError (wrong name:) when running from cmd

问题

我是Java的新手,正如标题所说,我正在努力从cmd运行我的测试程序(基本上只是一个Hello World程序),我正在使用myfolder\eclipse\learning\src\learning目录来存储我的项目类/.java文件。
我得到了错误信息:

Caused by: java.lang.NoClassDefFoundError: learning/Test (wrong name: Test)

我不太确定原因,我已经运行了“javac Test.java”,这一步没有问题,但是每当我尝试“java Test”时,就会出现上述错误。我还尝试过“java Test.class”,但那只是给了我这个错误:

Caused by: java.lang.ClassNotFoundException: Test.class

这个类在Eclipse中也可以正常运行。我已经多次在Google上搜索了这个问题,但是没有找到解决办法,可能是我对这些解决办法的使用方式有问题。

谢谢您的时间。

英文:

I'm new to Java, and as the title says I'm struggling to run my Test program (it's basically just a hello world program) from the cmd, I'm using the directory of myfolder\eclipse\learning\src\learning to store my project classes/.java files.
I get the error

Caused by: java.lang.NoClassDefFoundError: learning/Test (wrong name: Test)

I'm not sure why, I've done "javac Test.java", and that goes fine, but whenever I try "java Test" I get that error. I've also tried "java Test.class" but that just gave me the error

Caused by: java.lang.ClassNotFoundException: Test.class

The class also runs fine in Eclipse. I've googled this problem many times and have found no solution, but I could just somehow be using those solutions incorrectly.

Thank you for your time.

答案1

得分: 0

我假设Test.java的第一行是:

package learning;

在编译后运行这个类,你需要在myfolder\eclipse\learning\src目录中。然后使用java learning.Test来运行它。这是因为你在一个包内创建了这个类,就像包声明所示。Eclipse被构建成可以理解包,并且会自动为你执行正确的操作。这就是为什么你在Eclipse中运行时不会出现任何错误。

英文:

I assume that the first line of Test.java is:

package learning;

To run this class after you compile it, you need to be in the myfolder\eclipse\learning\src directory. Then run it with java learning.Test. This is because you have created the class inside a package as shown by the package declaration. Eclipse is built to understand packages and does the right thing automatically for you. That's why you don't get any errors running in Eclipse.

答案2

得分: 0

Compile is as follows:

myfolder\eclipse\learning\src\learning\>javac -d . Test.java

Run it as follows:

myfolder\eclipse\learning\src\learning\>java learning.Test

Notes:

  1. -d specifies where to place generated class files.
  2. . stands for the current directory

Note that if you are using Java 11, you can run it directly as java Test.java even without compiling it. Check this to learn more about it.

英文:

Compile is as follows:

myfolder\eclipse\learning\src\learning\>javac -d . Test.java

Run it as follows:

myfolder\eclipse\learning\src\learning\>java learning.Test

Notes:

  1. -d specifies where to place generated class files.
  2. . stands for current directory

Note that if you are using Java 11, you can run it directly as java Test.java even without compiling it. Check this to learn more about it.

huangapple
  • 本文由 发表于 2020年4月7日 01:46:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/61065786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定