首先:我已经阅读了所有我可以在这里找到的
cannot find symbol
线程。他们都没有解决我面临的问题。我不是专业的 Java 开发人员,我只是帮助同事上这门课,所以请对我宽容一些。
先描述一下基本情况:
我有一个名为
pdfDownload
的软件包,位于 src/pdfDownload。在这个目录中,我有 2 个文件:PDFItem.java
和 PDFItemParser.java
。
两者都是公开课。您可以找到下面所附的类的代码。我正在使用
Eclipse IDE
没有显示任何警告或错误**。
当我编译它们时,我收到以下错误消息:
PDFItemParser.java:19: cannot find symbol symbol : class PDFItem
location: class pdfDownload.PDFItemParser public static
ArrayList<PDFItem> parseFile(String filePath){
^ PDFItemParser.java:11: cannot find symbol symbol : class PDFItem location: class pdfDownload.PDFItemParser
ArrayList<PDFItem> items = parseFile("data.csv");
^ PDFItemParser.java:20: cannot find symbol symbol : class PDFItem location: class pdfDownload.PDFItemParser ArrayList<PDFItem>
items = new ArrayList<PDFItem>(); /* Creates an ArrayList from type
PDFItem which will contain all parsed ItemObjects */
^ PDFItemParser.java:20: cannot find symbol symbol : class PDFItem location: class pdfDownload.PDFItemParser
ArrayList<PDFItem> items = new ArrayList<PDFItem>(); /* Creates an
ArrayList from type PDFItem which will contain all parsed ItemObjects
*/
^ PDFItemParser.java:21: cannot find symbol symbol : class PDFItem location: class
pdfDownload.PDFItemParser items.add(new PDFItem());
^ 5 errors
类都是公共的,位于正确的目录和包中。我还在 Eclipse 中为
PDFItem
类中的 PDFItemParser
获得自动完成功能。我和我的同事已经努力解决这个问题两个小时了。如果这对你们来说真的很容易,我很抱歉,但我们无法解决它,因为此错误的常见情况不适用。预先感谢!
编辑:我在(Mac)终端中编译它们。我在终端中打开路径,然后输入:
javac PDFItem.java
然后
javac PDFItemParser.java
PDFItem - 类代码:
package pdfDownload;
public class PDFItem {
String imageURL;
String pdfURL;
boolean imageLoaded;
boolean pdfLoaded;
String name;
public PDFItem() {
}
public PDFItem(String name) {
this.name = name;
}
}
PDFItemParser - Class Code:
---------------------------
package pdfDownload;
import java.util.ArrayList;
import java.io.*;
public class PDFItemParser {
public static void main(){
ArrayList<PDFItem> items = parseFile("data.csv");
if(items != null){
System.out.println(items.get(0).name);
}
}
public static ArrayList<PDFItem> parseFile(String filePath){
ArrayList<PDFItem> items = new ArrayList<PDFItem>(); /* Creates an ArrayList from type PDFItem which will contain all parsed ItemObjects */
items.add(new PDFItem());
try{
FileInputStream fstream = new FileInputStream(filePath);
DataInputStream in = new DataInputStream(fstream); /* Get the object of DataInputStream */
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) { /* Read File Line By Line */
System.out.println (strLine); /* Print the content on the console */
}
in.close(); /* Close the input stream */
}
catch (Exception e){ /* Catch exception if any */
System.err.println("Error: " + e.getMessage());
}
return items; /* Returns ArrayList */
}
}
您应该使用此命令编译您的类,以确保您的类进入为您的
package
创建的文件夹中:-
javac -d . PDFItem.java
javac -d . PDFItemParser.java
当您在没有
-d
标志的情况下编译它时,您的类不在实际搜索它们的 package
文件夹内。因此,您的 PDFItemParser
无法找到您的 PDFItem
课程。
另外,请确保您已在类路径中添加了直到
package folder
的路径。仅添加路径到包含包名称的文件夹,而不添加到类名称。
嘿,你没有合适的
main
功能。这就是全部:
我改变了这一行
public static void main(){
到
public static void main(String args[]){
在
PDFItemParser
班
现在我可以运行程序了(但当然它给出了运行时错误)
Error: data.csv (The system cannot find the file specified)null
编辑
此错误是预期的,因为我没有 data.csv 文件。
我能够在 Eclipse 中编译并运行这个程序,没有任何问题