我正在尝试解决部分问题。这是我的第一部分
我需要创建一个 JAR,从 Java 类调用该 JAR 并在命令行中执行它。
这是我所做的。
目录结构:root->com->test->sample->Sample1.java
Sample1.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.java
javac -cp .:./asd.jar Sample1Caller.java
这会编译并生成 2 个类文件 Sample1Caller.class 和 Sample1.class
我不确定是否应该创建 Sample1.class(我已经从 asd.jar 中引用它)
它编译但不执行。我的执行命令或生成 .jar 文件或使用 java 编译 jar 有问题吗?
慕桂英546537
相关分类