当前位置: 首页 > 知识库问答 >
问题:

测试简单的Rxjava和gradle代码得到错误吗?

徐奇逸
2023-03-14

我用java和gradle写了一个简单的Rxjava代码,但是我得到了一些错误,细节如下:

这是我的简单java源代码:

import rx.functions.Action1;
import rx.Observable;
public class HelloWorld{
    public static void main(String[] args) {
        hello("Wjm");
    }

    public static void hello(String... names){
        Observable.from(names).subscribe(new Action1<String>(){

            @Override
            public void call(String s){
                System.out.println("Hello " +s +"!");
            }
        });
    }
}

这是我的gradle.build源代码:

apply plugin: 'java'


jar{
    manifest{
        attributes 'Main-Class':'HelloWorld'

    }

}

repositories {
    maven { url 'https://oss.jfrog.org/libs-snapshot' }
}

dependencies {
    compile 'io.reactivex:rxjava:1.1.0'
    runtime 'io.reactivex:rxjava:1.1.0'
}
.
├── build
│   ├── classes
│   │   └── main
│   │       ├── HelloWorld$1.class
│   │       └── HelloWorld.class
│   ├── dependency-cache
│   ├── libs
│   │   └── proj1.jar
│   └── tmp
│       ├── compileJava
│       │   └── emptySourcePathRef
│       └── jar
│           └── MANIFEST.MF
├── build.gradle
└── src
    └── main
        └── java
            └── HelloWorld.java
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: rx/functions/Action1
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: rx.functions.Action1
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

我是一个初学者,所以我问,但我做了一个测试在android工作室,相同的java代码,并运行成功。

共有1个答案

卫博学
2023-03-14

您已正确添加了依赖项,但..当您通过使用gradle准备的jar运行应用程序时,依赖项将丢失。为什么?因为您没有为正在运行的jar正确配置类路径。要修复它,您需要:

>

  • 使用shadow插件并准备jar,以便将所有依赖项重新打包并包含到生成的jar中。这是通过shadowjar任务完成的。

    运行准备好的jar时手动配置classpath。例如:

    java -cp ~/.gradle/caches/modules-2/files-2.1/io.reactivex/rxjava/1.1.0/748f0546d5c3c27f1aef07270ffea0c45f0c42a4/rxjava-1.1.0.jar:build/libs/37183502.jar org.opal.Elo 
    

    使用gradle application plugin并使用gradle clean run运行主类。应用程序插件还可以用脚本准备一些发行版来运行它们。

    看一下演示。

  •  类似资料:
    • 我的功能测试文件 我尝试了2个不同的functional.suite.yml文件来运行测试: 失败时显示错误消息: 示例Cest:尝试测试签名:api\测试\示例Cest: tryToTest测试:测试/功能/示例Cest.php: tryToTest场景-我发送get"例如/测试2"[请求]GET v1/例如/测试2[请求头][][yii\web\HttpExc0019:404]'yii\bas

    • 线程“main”java.lang.noClassDefounderRor: org/openqa/selenium/remote/internal/circularoutputStream在org.openqa.selenium.firefox.firefoxBinary.(firefoxBinary.firefoxBinary.firefoxBinary.firefoxBinary.firef

    • 运行此代码时,在行获得NullPointerException 如果我在调试模式下运行,它会在同一行上停止,但在变量选项卡中,的值可能是原样的,即来自main方法的值。 那么为什么运行时中有NullPointerException呢?

    • 到目前为止,我已经在IntelliJ中尝试了几个小时的代码覆盖率测试,但没有成功。 RuntimeException:存根! 现在,我已经读到在模块选项中更改依赖顺序可以修复这个问题。所以在改变顺序后,我得到了这个错误: 它指向下面的代码: 我谷歌了又谷歌,但唉,到目前为止我还没有找到解决方案。

    • 下面是我遇到错误的程序: //导入扫描程序类导入java。util。扫描仪; 这些是我得到的错误:

    • 我正在尝试使用Codeception进行基本的单元测试。没有使用任何框架。 我的工作根目录如下: 在 我的测试用例是: 我的composer.json中还有: 当我跑的时候