Jython NoClassDefFoundError when using a class from another class - java

I am trying to use 2 Java defined classes in python using jython 2.7.0 but after i ran my script it throws a java.lang.NoClassDefFoundError
Those are my 2 Java classes:
Hello.java
package hello;
public class Hello {
public void printMsg(){
System.out.println("check out this cool message");
}
}
Hello2.java
package hello;
public class Hello2 {
public void printHelloMsg(){
Hello msg = new Hello();
msg.printMsg();
}
}
I compiled them with:
hello$ javac Hello.java Hello2.java
Then created a jar file with:
hello$ jar cf hello-jar.jar *.class
And my python script that is supposed to use the Hello2 class, which uses Hello class:
hello.py
import Hello2
h = Hello2()
h.printHelloMsg()
After i ran my script with jython using:
hello$ jython -J-cp ./hello-jar.jar hello.py
Traceback (most recent call last):
File "hello.py", line 1, in <module>
import Hello2
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.python.core.Py.loadAndInitClass(Py.java:991)
at org.python.core.Py.findClassInternal(Py.java:926)
at org.python.core.Py.findClassEx(Py.java:977)
at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:133)
at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:33)
at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:122)
at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:134)
at org.python.core.PyObject.__findattr__(PyObject.java:946)
at org.python.core.packagecache.PackageManager.lookupName(PackageManager.java:141)
at org.python.core.JavaImporter.find_module(JavaImporter.java:40)
at org.python.core.JavaImporter.find_module(JavaImporter.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: Hello2 (wrong name: hello/Hello2)
What could possibly be the problem here?
Thanks in advance

The problem is in the file hello.py. The import statement is incorrect; it should be:
from hello import Hello2
instead of
import Hello2

Related

Docker starts the springboot project and reports java version error

I use the following dockerfile to generate a image call userservice
FROM java:8-alpine
COPY ./userservice.jar /tmp/userservice.jar
ENTRYPOINT java -jar /tmp/userservice.jar
then I use the following command to run container
docker run --name userservice_container userservice
But the terminal prompts that the java version does not match,as follows:
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/imortal/UserApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
So I went to check the compiled bytecode version, it is indeed 52, I don’t know why this error is still reported
javap -verbose .\UserApplication.class
public class com.imortal.UserApplication
minor version: 0
major version: 52
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #2 //
com/imortal/UserApplication
super_class: #4 // java/lang/Object
interfaces: 0, fields: 0, methods: 2, attributes: 2
Use a docker image that has a compatible jdk version, so that it doesn't create any conflict with all the microservices that you are having. This issue probably is occuring because one of your microservices is build on some other jdk version.

Compiling and running JUnit test suite

I've made a JUnit test suite but I'm failing to build and run it from the command line as opposed to just letting IntelliJ perform its magic. I've downloaded junit-4.12.jar and hamcrest-core-1.3.jar. I'm ideally looking for a "portable" solution, i.e. one where all the necessary parameters are in the command rather than in a path variable in a bash file somewhere.
I believe I can compile it fine with: javac -cp /path/to/junit-4.12.jar:. TestSuite.java. This produces a TestSuite.class file with no errors.
But I've tried every command I can find to run it and I always get errors. For example: java -cp /path/to/junit-4.12.jar:/path/to/hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore TestSuite gives:
JUnit version 4.12
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCommandLineParseResult.createRequest(JUnitCommandLineParseResult.java:118)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 17 more
Any ideas what's going wrong?
I think if you surround your classpath with double quotes and use semi-colon instead of colon for the delimiter, it should work:
java -cp "/path/to/junit-4.12.jar;/path/to/hamcrest-core-1.3.jar;." org.junit.runner.JUnitCore TestSuite
If you still get the same error, then I think you just have a typo in your hamcrest path.

netbeans 9: error generating WSDL (NoClassDefFoundError: com/sun/mirror/apt/AnnotationProcessorFactory)

I'm trying to generate a webservices wsdl using netbeans, but it fail searching AnnotationProcessorFactory, that does not exist in java8 (as I've read googling)
I've tried a new web project from scratch, adding a web services using netbeans wizard.
This create a new class with a hello method
#WebService(serviceName = "NewWebService")
public class NewWebService
{
/**
* This is a sample web service operation
*/
#WebMethod(operationName = "hello")
public String hello(#WebParam(name = "name") String txt)
{
return "Hello " + txt + " !";
}
}
after I've lunched the wsdl generation (right click on webservice name and choose "Generate WSDL and copy", it start to compile and fail on wsgen
command line: wsgen -classpath /usr/lib64/java/lib/tools.jar:/home/rcazzola/NetBeansProjects/sugar/WebApplication1/build/web/WEB-INF/classes:/opt/glassfish-4.1.1/glassfish/modules/webservices-osgi.jar:/opt/glassfish-4.1.1/glassfish/modules/endorsed/webservices-api-osgi.jar:/opt/glassfish-4.1.1/glassfish/modules/jaxb-osgi.jar:/opt/glassfish-4.1.1/glassfish/modules/endorsed/jaxb-api.jar -d /home/rcazzola/NetBeansProjects/sugar/WebApplication1/build/generated-sources/jax-ws -Xendorsed -keep -wsdl -r /home/rcazzola/NetBeansProjects/sugar/WebApplication1/build/generated-sources/jax-ws/resources -s /home/rcazzola/NetBeansProjects/sugar/WebApplication1/build/generated-sources/jax-ws -verbose ws.NewWebService
/home/xxxx/NetBeansProjects/sugar/WebApplication1/nbproject/jaxws-build.xml:26:
java.lang.NoClassDefFoundError: com/sun/mirror/apt/AnnotationProcessorFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at com.sun.istack.tools.ParallelWorldClassLoader.findClass(ParallelWorldClassLoader.java:90)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.sun.tools.ws.ant.WsGen2.execute(WsGen2.java:524)
at com.sun.istack.tools.ProtectedTask.execute(ProtectedTask.java:55)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99)
at org.apache.tools.ant.Task.perform(Task.java:350)
at org.apache.tools.ant.Target.execute(Target.java:449)
at org.apache.tools.ant.Target.performTasks(Target.java:470)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1388)
at org.apache.tools.ant.Project.executeTarget(Project.java:1361)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:261)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:574)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:128)
Caused by: java.lang.ClassNotFoundException: com.sun.mirror.apt.AnnotationProcessorFactory
at com.sun.istack.tools.ParallelWorldClassLoader.findClass(ParallelWorldClassLoader.java:71)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 23 more
BUILD FAILED (total time: 0 seconds)
What I have missed?

Error java.lang.ClassNotFoundException: org.slf4j.LoggerFactory when trying to execute the jar

When I try to do java -jar app.jar I get this:
Exception in thread "JavaFX Application Thread" Exception in thread "main"
java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at tao.qaflash.GUI.<clinit>(GUI.java:14)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplicationWithArgs$156(LauncherImpl.java:352)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 12 more
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:383)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
... 5 more
My environment is: Java 1.8.0_92, Gradle 2.16, Eclipse Mars 4.5.0, Java FX.
Also I have googled this error and I have tried everything from there , but with no success. I tried to add to the build.gradle as dependencies slfj-simple, log4j, logback-classic, slf4j-jdk14,junit,vaadin-slf4j-jdk14 but the error kept appearing.
When I run the application from Eclipse, everything works fine, the logs are written, only when I try to run the jar from command line, I get this ClassNotFoundException.
build.gradle looks like :
dependencies {
compile 'net.sf.staf:jstaf:3.4.4'
// SLF4J API module which will be used for logging
compile 'org.slf4j:slf4j-api:1.7.21'
// Log4J2 logging framework including the binding module to slf4j
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.5'
compile 'org.apache.logging.log4j:log4j-api:2.5'
compile 'org.apache.logging.log4j:log4j-core:2.5'
testCompile 'junit:junit:4.12'
testCompile "org.testfx:testfx-core:4.0.+"
testCompile "org.testfx:testfx-junit:4.0.+"
}
Any help is greately appreciated.
Simple: the classpath that you are using when trying to execute your JAR file does not contain the required sl4j logger library.
That is all there is to this. This is not about your build setup - it is about the setup that gets used when you run java on the command line!
See here for further reading.
I have unzipped the zip file that was created in the dist folder and it works :) because in the lib folder there was the sl4j-api.
It seems you need slf4j at runtime as well. Have a look at the gradle dependency configuration options

Spring "hello world" example error: A JNI error has occured & NoClassDefFoundError: org/springframework/beans/factory/ListableBeanFactory

I tried to use Eclipse to run the Spring Framework "Quick Start" tutorial at
http://projects.spring.io/spring-framework/
I firstly built a Maven Project in Eclipse, and added three .java files.
The codes are as follows
package hello;
public interface MessageService {
String getMessage();
}
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
#Component
public class MessagePrinter {
final private MessageService service;
#Autowired
public MessagePrinter(MessageService service){
this.service = service;
}
public void printMessage(){
System.out.println(this.service.getMessage());
}
}
package hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
#Configuration
#ComponentScan
public class Application {
#Bean
MessageService mockMessageService(){
return new MessageService(){
public String getMessage() {
return "Hello World!";
}};
}
public static void main(String args[]){
ApplicationContext context =
new AnnotationConfigApplicationContext(Application.class);
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();
}
}
Then I right clicked the Eclipse and choose "run as > Java Application". Two error messages jumped out:
Error: A JNI error has occured, please check your installation and try again
A Java Exception has occurred.
and the console showed
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/beans/factory/ListableBeanFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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)
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: org.springframework.beans.factory.ListableBeanFactory
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)
... 19 more
I have tried to add more Spring dependencies (including aop aspects beans context context-support core ...) in the POM file, but that didn't work.
I have already fixed this problem. In fact this problem had little relationship to "A JNI error has occured".
The cause of the this problem is that an error(s) occured when Eclipse download Maven dependency jar file, namely "spring-beans".
So I (manually) deleted all the files in "....m2\repository\org\springframework\spring-beans", and used Eclipse to rebuild the project. In this way the problem was fixed.

Categories