[英]How to run java code from command line that has source code in 2 different packages?
所以我想運行一個名為 WordFrequencyCounter 的類,它嵌套在文件夾中:C:\\Users\\Mike\\Documents\\Computer Science\\AI\\Machine Learning\\175\\Assignment 2\\src\\ir\\assignments\\two\\b
但是當我進入該文件夾並運行時:
javac WordFrequencyCounter.java
它給了我一個錯誤,因為它說它找不到 import ir.assignments.two.a.Frequency; 具體來說,它說包 ir.assignments.two.a 不存在,但該文件位於:
C:\\Users\\Mike\\Documents\\Computer Science\\AI\\Machine Learning\\175\\Assignment 2\\src\\ir\\assignments\\two\\a
我還嘗試從文件夾中調用 javac WordFrequencyCounter.java:C:\\Users\\Mike\\Documents\\Computer Science\\AI\\Machine Learning\\175\\Assignment 2
但這也不起作用。 所以我想要的是能夠編譯和運行 WordFrequencyCounter,然后從命令行向它提供一個位於 C:\\Users\\Mike\\Documents\\Computer Science\\AI\\Machine Learning\\175\\Assignment 2 中的輸入文件。 我可以從 IDE 執行此操作,但無法通過命令行執行此操作。
我認為這是一個與類路徑相關的問題,但是對於我的特定示例,我找不到任何有用的信息,而且我嘗試過的任何方法都不起作用。
這是 WordFrequencyCounter 的主要方法:
public static void main(String[] args) throws FileNotFoundException {
File file = new File(args[0]);
List<String> words = Utilities.tokenizeFile(file);
List<Frequency> frequencies = computeWordFrequencies(words);
Utilities.printFrequencies(frequencies);
}
轉到 src 文件夾並運行
javac -d . ir/assignments/2/b/MainClass.java
並使用java ir.assignments.2.b.MainClass
來運行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.