简体   繁体   English

Javac编译和2个Java文件的执行

[英]Javac compilation and execution of 2 java files

The program is written with 2 files filter.java - in which is the main function and ClasifiedWord.java which is only container class. 该程序用2个文件filter.java编写-其中是main函数,而ClasifiedWord.java是仅容器类。 I wrote that on windows on eclipse but want to compile it on Linux javac compiler. 我是在eclipse的Windows上写的,但是想在Linux javac编译器上进行编译。

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! 感谢您的回答!

Type the following command. 键入以下命令。

java -classpath . filter

If you want to type just java filter , please following these guides. 如果您只想输入java filter ,请遵循以下指南。

PS The last command java filter ClasifiedWord means to run a program filter and use a string ClasifiedWord as an argument. PS最后一个命令java filter ClasifiedWord意味着运行程序 filter并使用字符串 ClasifiedWord作为参数。

Roland is right in the comment, you should be able to run it with java -classpath <folderContainingDotClassFiles> filter . Roland在注释中是正确的,您应该可以使用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. 另外,您应该将filter.java更改为Filter.java,因为类名(包括包含它们的文件)的标准Java约定是大写第一个字母。

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. 看起来您的CLASSPATH设置有误(通常根本不需要此设置,但如果有此设置,则该列表应在当前目录的列表中包含. ),或者您正在使用软件包(错误地),或两者。 For the classpath issue, see the other answers. 对于类路径问题,请参阅其他答案。

If you are using packages, ie your files contain a package ...; 如果您正在使用软件包,即您的文件包含一个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; 例如,如果您有package example; , you should have an example directory in your current directory, and in this the two source files. ,您应该在当前目录中有一个example目录,并在其中有两个源文件。

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.) (这假定filter是主类,否则替换其名称。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM