![](/img/trans.png)
[英]Error: cannot find symbol while compiling java program with package in cmd
[英]Strange "cannot find symbol" error (Compiling package from Terminal)
首先:我已經閱讀了所有我可以在這里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 中為PDFItemParser
類中的PDFItem
自動完成。 我和我的同事一直在努力解決這個問題 2 個小時。 如果這對你們來說真的很容易,我很抱歉,但我們無法解決它,因為此錯誤的通常情況不適用。 提前致謝!
編輯:我在(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 中毫無問題地編譯和運行這個程序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.