javac编译并执行2个java文件
该程序由 2 个文件 filter.java 编写 - 其中是主函数,而 ClasifiedWord.java 只是容器类。我在 windows 上的 eclipse 上写了它,但想在 Linux javac 编译器上编译它。
javac filter.java ClasifiedWord.java
运行没有错误,但是当我尝试运行该程序时:
snowy:Filter$ java filter
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
snowy:Filter$ javac filter.java ClasifiedWord.java
snowy:Filter$ java filter ClasifiedWord
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
如何运行该程序?看来编译是没问题的。我怀疑我可能对程序中的文件路径犯了错误......但我认为事实并非如此? 感谢您的回答!
The program is written with 2 files filter.java - in which is the main function and ClasifiedWord.java which is only container class. I wrote that on windows on eclipse but want to compile it on Linux javac compiler.
javac filter.java ClasifiedWord.java
runs without mistakes, but when I try to run the program:
snowy:Filter$ java filter
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
snowy:Filter$ javac filter.java ClasifiedWord.java
snowy:Filter$ java filter ClasifiedWord
Exception in thread "main" java.lang.NoClassDefFoundError: filter
Caused by: java.lang.ClassNotFoundException: filter
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: filter. Program will exit.
How can I run the program ? It seems that the compilation is ok. I have doubts that maybe I have made a mistake about the file paths in the program.... but I think that this is not the case?
Thanks for your answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
键入以下命令。
如果您只想输入
java filter
,请遵循这些指南。PS 最后一条命令
java filter ClasifiedWord
的意思是运行程序过滤器
并使用字符串ClasifiedWord
作为参数。Type the following command.
If you want to type just
java filter
, please following these guides.P.S. The last command
java filter ClasifiedWord
means to run a programfilter
and use a stringClasifiedWord
as an argument.罗兰在评论中是正确的,您应该能够使用
java -classpath运行它过滤器。另外,您应该将 filter.java 更改为 Filter.java,因为类名(包括包含它们的文件)的标准 Java 约定是将首字母大写。
Roland is right in the comment, you should be able to run it with
java -classpath <folderContainingDotClassFiles> filter
. Also, you should change filter.java to Filter.java as the standard Java convention for class names (including the files containing them) is to capitalize the first letter.这看起来要么是你的 CLASSPATH 设置错误(通常你根本不需要这个,但如果你有它,它应该在当前目录的列表中包含
.
),或者你正在使用包(错误地),或者两者都使用。对于类路径问题,请参阅其他答案。如果您使用包,即您的文件开头包含
package ...;
行,则应根据包结构将源文件放在目录结构中。例如,如果您有package example;
,则当前目录中应该有一个example
目录,其中有两个源文件。然后像这样调用编译器和解释器:(
这里假设
filter
是主类,否则替换它的名称。)This looks like you either have a wrong CLASSPATH setting (usually you don't need this at all, but if you have it, it should include
.
in the list, for the current directory), or you are using packages (wrongly), or both. For the classpath issue, see the other answers.If you are using packages, i.e. your files contain a
package ...;
line at the beginning, you should put your source files in a directory structure according to the package structure. For example, if you havepackage example;
, you should have anexample
directory in your current directory, and in this the two source files.Then call compiler and interpreter like this:
(This assumes that
filter
is the main class, otherwise replace its name.)