java multi-file compilation problem

Most of the textbooks say that as long as the class with the main function is compiled, the referenced class can be automatically compiled, but I found a problem that multiple files could not be compiled when compiling the supplementary code of "Java Core Technology I" (10th Edition).
These two classes are under an objectAnalyzer package, and both are written, but package objectAnalyzer;the class cannot be found when compiling. . . After asking the teacher, I got two solutions.

  1. Remove all package objectAnalyzer;to make it compile with default packages.
  2. Move the main class out, put only the referenced class in the package, and add the moved main class import objectAnalyzer.*;.

package objectAnalyzer;After using these two methods, the compilation and operation are passed, but the principle of why there is a problem that cannot be compiled after adding it is still unclear.


2018.5.2

The teacher gave the reason, because the -classpath is not added, it is automatically loaded in eclipse, so it can be compiled and run.
Compile:

D:\xxx\objectAnalyzer>javac -classpath D:\xxx ObjectAnalyzerTest.java

where xxxis objectAnalyzerthe directory one level above the folder.
run:

D:\xxx\objectAnalyzer>java -classpath D:\xxx objectAnalyzer.ObjectAnalyzerTest

There is even more to run here objectAnalyzer, but not to compile. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726523&siteId=291194637