java源文件的目录形式运行的正常运行

内容

前言

我写了一个方法来解决我自己的情况下找不到或加载主类的问题。第一次写博客的时候cmd找不到或无法加载主类,难免写的不够清晰简洁。

问题

FriendshipGraph 在 Eclipse 上运行良好,但在 cmd 上不起作用。

可以看到使用javac“悄悄”完成了编译cmd找不到或无法加载主类,说明javac的配置正常。

在 IDE 中运行良好。

但是,使用java运行程序时出现上述错误,NoClassDefFoundError,表示没有找到该类。大致有两个原因。首先是classpath没有设置好,但是这是不可能的,因为大致在jdk1.5之后,jre可以自动搜索目录下的class文件,加载dt.jar和tool.jar的classes运行java程序,无需在环境变量中配置。所以报错的原因不是第一个,而是第二个:java的包名。

You can see that the java source code uses the package statement at the beginning, which indicates that the class defined by the java source file is under the P3 package. We can see that FriendshipGraph was run in the P3 directory before, but according to the warning of Java Core Technology Volume I, this is not right:

At the same time, it also gives the correct approach:

It seems that we want to run this program in the form of a directory P3.FriendshipGraph in the directory containing the package name, that is, the src directory, as follows:

You can see that the program is running normally.

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论