测试简单Rxjava和gradle代码是否出错?
我用java和gradle编写了一个简单的Rxjava代码,但出现了一些错误,详细信息如下:测试简单Rxjava和gradle代码是否出错?,java,gradle,rx-java,Java,Gradle,Rx Java,我用java和gradle编写了一个简单的Rxjava代码,但出现了一些错误,详细信息如下: 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(Na
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
这是我的简单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 +"!");
}
});
}
}
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
我运行命令“gradle build”是成功的,但是,
当我在build/libs/path中运行“java-jar proj1.jar”时,得到如下错误消息:
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
我是gradle的新手,所以我问了,但我在android studio中做了一个测试,使用相同的java代码,并且运行成功。您已经正确添加了依赖项,但是。。当您通过gradle准备的jar运行应用程序时,依赖项丢失。为什么?因为您没有为正在运行的jar配置类路径。要解决此问题,您需要:
shadowJar
任务完成的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
gradleclean run
运行主类。应用程序插件还可以准备一些带有脚本的发行版来运行它们看一看。我的答案不是最好的答案,但它对我很有用 我刚刚下载了我的项目所需的所有jar库,并将它们放在我的项目根目录中(或者您可以创建一个LIB目录并将它们放在那里),然后在我的项目根目录中运行下面的命令:
java-cp*:my\u jar/my\u output\u jar.jar Main目录
其中,“Main”是项目的主类。如果将main类放入包中,请编写包和类名,例如:com.example.main
java-cp*:my\u jar/my\u output\u jar.com.example.Main目录
谢谢,这对我帮助很大。演示非常清晰。