我正在處理我的第一個非平凡的Java應用程序。它是由一名同事創辦的,但她沒有時間完成,所以我接管了它。我決定使用Buildr作爲我的生成工具:https://buildr.apache.org/我可以嘗試/發現此錯誤:「嘗試打開jar時發生意外錯誤」
首先,先從空的目標目錄,我做的:
buildr clean
然後我:
buildr --verbose compile
這給了我:
(in /Users/cerhov/projects/openz/lofdg/buildr_fdg, development)
Compiling buildr_fdg
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/students.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/students.txt
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/scores.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/scores.txt
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/behavior.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/behavior.txt
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/schoolDates.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/schoolDates.txt
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company
cp /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/resources/com/company/assignments.txt /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources/com/company/assignments.txt
touch /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/resources
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
Compiling buildr_fdg into /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
Note: /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/java/com/company/Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
touch /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
Completed in 0.926s
這給我我的「.class」文件。現在,我要打包這件事作爲一個罐子,所以我:
cerhov : 15:26:04 : ~/projects/openz/lofdg/buildr_fdg $ buildr --verbose package
這給了我:
(in /Users/cerhov/projects/openz/lofdg/buildr_fdg, development)
Building buildr_fdg
Packaging buildr_fdg
Packaging buildr_fdg-1.0.0.jar
rm /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/buildr_fdg-1.0.0.jar
mkdir -p /Users/cerhov/projects/openz/lofdg/buildr_fdg/target
Running integration tests...
Completed in 0.212s
那麼我這樣做:
cerhov : 15:26:56 : ~/projects/openz/lofdg/buildr_fdg $ ls target/
,我看到一個新的jar文件已創建:
buildr_fdg-1.0.0.jar classes resources
so I:
java -jar target/buildr_fdg-1.0.0.jar
,但我得到:
Error: An unexpected error occurred while trying to open file target/buildr_fdg-1.0.0.jar
我想,所以我在一個大try/catch語句包裹着我的整個main()函數,以獲得更多信息:
public static void main(String[] args) {
try {
// all my real code goes here, but I have deleted it for clarity
} catch (Exception e) {
e.printStackTrace();
}
}
然後我重新編譯並重新打包,但我仍然遇到同樣的錯誤。我不清楚我如何強制應用程序給我更多的信息。
我想看看我的manifest.txt鑽進我的罐子,所以我複製的jar到另一個文件夾,cd'ed到該文件夾,並且:
jar xf buildr_fdg-1.0.0.jar
,然後這樣的:
cat META-INF/MANIFEST.MF
給我看:
Created-By: Buildr
:
Manifest-Version: 1.0
Main-Class: com/company/Main
它看起來正確的。也許我可以在類路徑中添加,但Buildr似乎已經找到了所有的文件。
我該如何強制應用程序給我關於這個問題的更多信息?
UPDATE:
我嘗試添加此行到清單:
Class-Path: /Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/java/com/company
然後我重新編譯和重新打包。但這:
java -cp buildr_fdg-1.0.0.jar com.company.Main
遞給我:
Error: Could not find or load main class com.company.Main
所以,我想每一個變化:
/Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/java/com/
/Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/java/
/Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/main/
/Users/cerhov/projects/launchopen/lofdg/buildr_fdg/src/
/Users/cerhov/projects/launchopen/lofdg/buildr_fdg/
其工作無。我想將類路徑瞄準源目錄或目標目錄嗎?
爲什麼Buildr無法爲我管理這個?它確實可以找到所有的文件,並將它們全部捆綁到jar中,因此它知道一切都在哪裏。
在Main.java文件中是否有package語句?如果運行'java -cp buildr_fdg-1.0.0.jar com.company.Main',會發生什麼? –
Main.java中的包語句:「package com.company;」 – cerhovice
有趣。這個:「java -cp buildr_fdg-1.0.0.jar com.company.Main」給我: 錯誤:無法找到或加載主類com.company.Main – cerhovice