[英]How to create a working .jar for a java project w/ excel input and System.out.print output?
我創建了這個Java項目,該項目基本上從用戶確定的excel文件中獲取數據,並使用Syste.out.println()顯示結果。 它可以按我希望的方式工作,但是當我導出為.jar文件時,它無法正常工作。 它提示輸入excel文件位置,但是不顯示輸出,甚至不顯示錯誤。 我不知道如何從終端上執行此操作,因此我正在通過雙擊操作它。 另外,我希望用戶選擇他們想要的任何excel文件,那么當系統提示時,他們應該寫下什么位置? 現在,excel文件與項目位於同一目錄中。 因此,只需輸入excel文件的名稱就足夠了,但是如果它不在目錄中,該怎么辦呢?
謝謝
首先,在您的JAR文件中,文件META-INF / MANIFEST.MF必須包含您的主類(即具有main()
方法的類),並帶有如下一行:
Main-Class: mypackage.MyMainClass
確保生成JAR文件時,您在Eclipse中的設置正在生成此行。
您可以使用java -jar yourapp.jar
從終端運行它,但是我認為您需要使用-cp
開關將一些額外的庫包含在類路徑中。
工作目錄通常是您從中運行應用程序的目錄。 如果要指定其他路徑,則必須相對於該路徑或絕對路徑。
Windows中的絕對路徑將類似於:“ C:\\ mydatafolder \\ myexcelsheet.xls”
Unix中的絕對路徑類似於:“ /home/myaccount/mydatafolder/myexcelsheet.xls”
您需要閱讀System.in以獲取文件路徑,其功能如下:
private static String getFilePath () {
String filePath = null;
while (true) {
System.out.println("Please input the path of the file:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
filePath = br.readLine();
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Sorry, invalid file path. Please try again.");
} else {
return filePath;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Sorry, unexpected error happened, Please try again.");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.