我正在开发我的第一个平凡的Java应用程序。它是由一位同事开始的,但是她没有时间完成,所以我接管了。我决定使用Buildr作为构建工具:https://buildr.apache.org/

首先,从空目标目录开始,我这样做:

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


所以我:

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


我想获取更多信息,所以我将整个main()函数包装在一个大型try / catch语句中:

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,所以我将jar复制到了另一个文件夹,cd到该文件夹​​,然后:

 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似乎已经找到了所有文件。

如何强制应用程序向我提供有关该问题的更多信息?

更新:

我尝试将以下行添加到清单中:

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中,因此它知道所有内容在哪里。

最佳答案

我重新创建了该问题,并能够通过手动删除清单文件中本身出现的冒号(:)来解决此问题。不知道为什么buildr会把它放在那里。这描述了一种可能在buildr中覆盖清单文件的方法:https://buildr.apache.org/rdoc/Buildr/Packaging/Java/JarTask.html我的信誉太低,无法发布评论,因此很遗憾,我只能将其发布为答案。希望这会有所帮助。

07-24 21:30