将类放入包后出现Java不兼容类型错误
我有一个名为“shapefile”的包,其中包含IndexFileReader和IndexFile类。IndexFileReader返回IndexFile类型的对象将类放入包后出现Java不兼容类型错误,java,package,javac,incompatibletypeerror,Java,Package,Javac,Incompatibletypeerror,我有一个名为“shapefile”的包,其中包含IndexFileReader和IndexFile类。IndexFileReader返回IndexFile类型的对象 import java.io.IOException; import shapefile.*; public class ReadFiles { public static void main(String[] args) throws IOException{ IndexFile indexFile;
import java.io.IOException;
import shapefile.*;
public class ReadFiles {
public static void main(String[] args) throws IOException{
IndexFile indexFile;
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
}
}
然后我有一个名为ReadFiles的类,它导入“shapefile”,并使用IndexFileReader创建一个IndexFile
import java.io.IOException;
import shapefile.*;
public class ReadFiles {
public static void main(String[] args) throws IOException{
IndexFile indexFile;
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
}
}
我的文件位于以下文件夹中:
- Java项目
- 形状文件
- IndexFileReader.java
- IndexFile.java
- 我的项目
- ReadFiles.java
- 形状文件
cd JavaProjects
javac .\shapefile\*.java
我像这样编译我的项目,我得到一个错误:
cd MyProject
javac -cp .. ReadFiles.java
错误:
ReadFiles.java:8: error: incompatible types
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
^
required: IndexFile
found: shapefile.IndexFile
这对我来说是新的,我相信我在运行javac时犯了一些错误。我以前已经成功编译了ReadFiles,我想我是用这里打印的相同命令编译的。事实上,我仍然有ReadFiles.class文件,当这个文件编译成功并且运行时没有任何问题,这就是为什么我认为我没有正确使用javac
我查看了其他不兼容类型的问题,但没有找到一个正确的方向。您可能还将
IndexFile.java
复制到了您的MyProject
目录中,但您仍然保留着它,或者您仍然有编译后的IndexFile.class
,这就是我的想法。确保IndexFile不在MyProject目录中。但不要使用此文件夹和包结构。要么声明您的类ReadFiles
位于包MyProject
中,要么将目录MyProject
向上移动一级,直到它成为JavaProjects
的同级目录。使用当前设置,您将面临许多问题。感谢您的回复。问题实际上是JavaProjects文件夹中已经有一个“IndexFile.class”文件。这是我把文件放进包里之前留下的。我已经按照建议将我的文件放入了一个更标准的文件夹结构中,一切都正常运行。