Reputation: 11
Some background: I'm a student just learning Java, and usually the professor handles making sure our assignments have the right class path. However, the whole semester I've been plagued by the same problem, and I still don't understand what's going wrong.
As an example, I have two files, MyProgram.java located in the folder "MyProject" and MyProgramTest.java located in the folder "ClassProject", which also contains "MyProject". MyProgramTest
creates a MyProgram
object and lets you test its functionality.
MyProgramTest
has the line import MyProject.MyProgram;
The compiling instructions my instructor gives is to use javac MyProject/*.java
while in "ClassProject" which works fine. Then, we are to use javac MyProgramTest.java
in the same directory. However, the compiler claims:
import MyProject.MyProgram;
bad class file: .\MyProject\MyProgram.class
class file contains wrong class: MyProgram
Please remove or make sure it appears in the correct subdirectory of the classpath.
So I don't quite understand why this is happening. MyProgram is in the MyProject directory, and that directory is in the folder I'm in. Since the instructor uses this exact method to compile these programs, I keep getting screwed since mine never compile correctly. Any idea what I'm doing wrong, or how I can fix the file to compile this way without changing the structure of the directories?
Upvotes: 1
Views: 24128
Reputation: 6721
Ensure this:
The MyProgram.java
file should contain this line at the top of the file:
package MyProject;
Compile MyProgram.java
from the ClassProject
folder:
javac -d . MyProject/*.java
Then Compile MyProgramTest.java
from the same folder:
javac MyProgramTest.java
This will create the class files correctly in the appropriate folder structure.
This should solve your problem.
Hope this helps!
Upvotes: 2