我正在尝试解决部分问题。这是我的第一部分我需要创建一个 JAR,从 Java 类调用该 JAR 并在命令行中执行它。这是我所做的。目录结构:root->com->test->sample->Sample1.javaSample1.java是这样的: package com.test.sample; public class Sample1 { // --- Native methods //public native boolean booleanMethod(boolean bool); // --- Main method to test our native library public void Sample1() { } public void callingBool() { //System.loadLibrary("Sample1"); //boolean bool = this.booleanMethod(true); //System.out.println("booleanMethod: returned" + bool); System.out.println("booleanMethod: Called without loading sample"); } }我从这个转到根目录中创建了一个 Jar。使用命令 jar cvf asd.jar com Output: added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/test/(in = 0) (out= 0)(stored 0%) adding: com/test/sample/(in = 0) (out= 0)(stored 0%) adding: com/test/sample/Sample1.java(in = 494) (out= 236) (deflated 52%) jar -tf asd.jar META-INF/ META-INF/MANIFEST.MF com/ com/test/ com/test/sample/ com/test/sample/Sample1.java所以现在创建了 JAR。我将 JAR 复制粘贴到另一个目录“jaronly”将另一个 Java 文件 Sample1Caller 添加到 /jaronly 中: import com.test.sample.Sample1; public class Sample1Caller { // --- Native methods //public native boolean booleanMethod(boolean bool); // --- Main method to test our native library public static void main(String[] args) { //System.loadLibrary("Sample1"); Sample1 sample = new Sample1(); sample.callingBool(); System.out.println("Callled Sample1 now" ); } }在此之后,我在 /jaronly 中编译 Sample1Caller.javajavac -cp .:./asd.jar Sample1Caller.java这会编译并生成 2 个类文件 Sample1Caller.class 和 Sample1.class我不确定是否应该创建 Sample1.class(我已经从 asd.jar 中引用它)它编译但不执行。我的执行命令或生成 .jar 文件或使用 java 编译 jar 有问题吗?
1 回答
慕桂英546537
TA贡献1848条经验 获得超10个赞
JAR 文件应该包含类而不是源代码。所以在制作asd.jar 之前,您应该先编译Sample1.java。这条线可以解决问题
javac com/test/sample/*.java
但是 Sample1.java 仍将位于 JAR 中,您不希望它存在于此。
要为您的构建添加更多结构,我建议您创建以下文件夹结构:
sample_project
sample_project/src
sample_project/src/com/test/sample/Sample1.java
sample_project/target
sample_project/build.bat
然后 build.bat 的内容将是:
:: compile
cd src
javac com/test/sample/*.java -d ../target
:: create jar
cd ../target
jar cvf asd.jar com
:: finish
cd ..
添加回答
举报
0/150
提交
取消