java - 如何创建一个 jar 。在类里面使用它。在命令行中使用 jar 执行类?

标签 java linux jar

我正在尝试解决部分问题。 这是我的第一部分

我需要创建一个 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 中引用它了)

当我使用以下命令执行 Sample1Caller 时出现以下错误

    java -cp .:./asd.jar Sample1Caller

    Exception in thread "main" java.lang.NoClassDefFoundError: 
    com/test/sample/Sample1
    at Sample1Caller.main(Sample1Caller.java:12)
    Caused by: java.lang.ClassNotFoundException: com.test.sample.Sample1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.lo
    Class(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

它编译但不执行。我的执行命令或生成 .jar 文件或使用 java 编译 jar 有问题吗?

PS:我之前几乎没有在这里提出任何问题,所以请原谅我的格式问题。

最佳答案

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 ..

关于java - 如何创建一个 jar 。在类里面使用它。在命令行中使用 jar 执行类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57464470/

相关文章:

linux - 如何让 `diff -qr`显示子目录下的所有文件?

java - 无法安装 j-text-utils-0.3.3.jar - 无主 list 属性

java - 将本地jar添加到Spring boot应用程序的pom.xml中

java - 安卓。手机离线时存储和发送数据

java - 在ormlite中,如何拥有外部字段和字段列?

java - 如何修复 QueryFullProcessImageName 抛出 "A device attached to the system is not functioning"?

c - Linux 的 continue 语句

linux - 如何从特定文件夹中获取 PID

java - 包含一个exe文件到jar

java - 谷歌日历(共享)