javac编译并执行2个java文件

发布于 2024-12-15 10:35:00 字数 1551 浏览 3 评论 0原文

该程序由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏花。依旧 2024-12-22 10:35:00

键入以下命令。

java -classpath . filter

如果您只想输入 java filter,请遵循这些指南。

PS 最后一条命令 java filter ClasifiedWord 的意思是运行程序 过滤器并使用字符串 ClasifiedWord作为参数。

Type the following command.

java -classpath . filter

If you want to type just java filter, please following these guides.

P.S. The last command java filter ClasifiedWord means to run a program filter and use a string ClasifiedWord as an argument.

傲鸠 2024-12-22 10:35:00

罗兰在评论中是正确的,您应该能够使用 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.

咽泪装欢 2024-12-22 10:35:00

这看起来要么是你的 CLASSPATH 设置错误(通常你根本不需要这个,但如果你有它,它应该在当前目录的列表中包含 . ),或者你正在使用包(错误地),或者两者都使用。对于类路径问题,请参阅其他答案。

如果您使用包,即您的文件开头包含 package ...; 行,则应根据包结构将源文件放在目录结构中。例如,如果您有 package example;,则当前目录中应该有一个 example 目录,其中有两个源文件。

然后像这样调用编译器和解释器:(

javac example/filter.java example/ClasifiedWord.java
java example.filter

这里假设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 have package example;, you should have an example directory in your current directory, and in this the two source files.

Then call compiler and interpreter like this:

javac example/filter.java example/ClasifiedWord.java
java example.filter

(This assumes that filter is the main class, otherwise replace its name.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文