简体   繁体   中英

Java can't find class errors in compilation

I am trying to compile and run my java application from the command prompt but am having some errors. I change to the bin bin folder thus am running from the bin folder.I tried compiling with :

>> javac foe.java

but i get some errors that it can't find some classes that are been referred to by the above main class.

Do i have to compile all classes that the above main class references? and if so how do i do it? thanks.

javac looks in the classpath for those classes referenced by your programs.

If you are unfamiliar with the classpath concept, please see the appropriate Java Tutorial section: http://download.oracle.com/javase/tutorial/essential/environment/paths.html

In foe.java , what are the import statements at the beginning of that file? If you're using classes that aren't part of the standard Java Runtime (classes whose package name begins with java. ) such as classes you made, classes that belong in other libraries (jar files), you must add them to your classpath before compiling, otherwise javac won't be able to find them.

The rule is: if it ain't part of the JRE, you have to be explicit when you compile.

if your main file is in c:\\path1\\mainfile.java

and referenced java file is in c:\\path2\\reffile.java

from c:\\

java -cp c:\\path2 c:\\path1\\mainfile.java

will compile both your files. (i am assuming you are not using any packages)

to run mainfile.java from c:\\

java -cp c:\\path1;c:\\path2 mainfile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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