'NoClassDefFoundError' in Intellij [duplicate] - java

This question already has answers here:
Random NoClassDefFound error in Web Application
(1 answer)
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 6 years ago.
I am trying to run a spring project in intellij.
When I import the code and add the necessary jars, it there is no error during compile time. However, when I run the project, I get the following error
Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/company/access/KeyValueAccess
The KeyValueAccess class is present in the cassandra-access-1.12.0.jar as highlighted in the image.
I have tried to add jars as modules and also as libraries.
What is the correct way to avoid this runtime error?
PS: This is the case for only manually added external libraries. The libraries downloaded with maven work just fine.
Edit
The following code generates the exception
#Bean
public KeyValueAccess keyValueAccess() {
return new CassandraAccessDatastax(); //This doesn't matter, the return type itself creates the exception
}
Exception generated is as below:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.worksap.bootcamp.webeditor.config.WebEditorConfig#0' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: com/company/access/KeyValueAccess
I also checked that the jar does it fact have the required class files.
Edit2
I manually ran maven to add the jar files, so I can now find them in ~/.m2/repository. When I call a class in the jar, it seems to find it but when that class calls another class in the same jar, it gives java.lang.NoClassDefFoundError

As I see in your screen, you added cassandra manually, not using maven? So this may be the problem. Add dependencies to pom.xml. Watch them here.
Also check if your dependencies added to special module, not just in libraries. Go to file->project structure->modules. Choose your module in which you use cassandra and click on "dependencies" tab. Press "+" and "library" from dropdown menu and then add your libraries to project.

Related

Getting the Maven build failure at JaxRS dependency updated the dependency and still getting the issue [duplicate]

This question already exists:
Getting Maven build failures while clean and install of my project [closed]
Closed last year.
Hi Please help me to resolve the below issue.
APPLICATION FAILED TO START
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
io.swagger.jackson.ModelResolver.resolve(ModelResolver.java:377)
The following method did not exist:
com.fasterxml.jackson.databind.introspect.AnnotatedMember.getType(Lcom/fasterxml/jackson/databind/type/TypeBindings;)Lcom/fasterxml/jackson/databind/JavaType;
The method's class, com.fasterxml.jackson.databind.introspect.AnnotatedMember, is available from the following locations:
jar:file:/C:/Users/konteja/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar!/com/fasterxml/jackson/databind/introspect/AnnotatedMember.class
The class hierarchy was loaded from the following locations:
com.fasterxml.jackson.databind.introspect.AnnotatedMember: file:/C:/Users/konteja/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar
com.fasterxml.jackson.databind.introspect.Annotated: file:/C:/Users/konteja/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.fasterxml.jackson.databind.introspect.AnnotatedMember

Eclipse java.lang.module.FindException and java.lang.ClassNotFoundException

I recently installed Eclipse 2020-06 and am using JDK 10.0.2. I wanted to test Eclipse using a simple Hello world program:
package eclispeTest;
public class eclipseTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello");
}
}
But I get the error: Error occurred during initialization of boot layer java.lang.module.FindException: Module eclispeTest not found
One quick fix I see for this to delete the module-info.java file but when I do this I get the error: Error: Could not find or load main class eclispeTest.eclipseTest Caused by: java.lang.ClassNotFoundException: eclispeTest.eclipseTest
I saw one potential fix that told me to go to "Check your project build-path and enable specific output folders for each folder. Go one by one though each source-folder of your project and set the output folder that maven would use."
from: Eclipse - java.lang.ClassNotFoundException
But I am new to Eclipse and have no idea what they are talking about.
I have also noticed that when I open the problems tab at the bottom of Eclipse there is one problem that is repeated about 8 times: Description Resource Path Location Type The project was not built due to "Failed to init ct.sym for C:\Program Files\Java\jre-10.0.2\lib\jrt-fs.jar". Fix the problem, then try refreshing this project and building it since it may be inconsistent eclispeTest Unknown Java Problem
Could someone help me figure out what I should try, with baby steps for how I should do it.
Thank you in advance.
PS in case it helps here is a picture of my Eclipse window after I try to run my code:
EDIT: I made a new java project and below is my java project cration window
(I still got the same error)
Fist of all, In order to work the line " System.out.println("Hello");"
you will require two classes
1)java.lang.System
2)java.io.PrintStream
using above two classes the code runs.you don't have to import these class,because JRE(Java Runtime Environment) automatically does for you.
If you are gettting this error
Error occurred during initialization of boot layer java.lang.module.FindException: Module eclispeTest not found
Which means You didn't setup your "Java project" properly with "JRE"
Let create new project to solve this problem
1)Open Eclipse IDE
2)click FILE (top left most)
3)New
4)java project
5)give you project name (make sure that- "use default location checkbox" clicked)
6)IN JRE section - select use an execution environment JRE(edition as required)
7)Then click finish
This would have solved your problem , go create a Class do your eclipseTest. This time it should work.
java project creation should look like this

I am getting NoClassDefFound error when running with listeners included and getting passed without listeners

Getting
java.lang.NoClassDefFoundError error when running xml file using
testng.
Error details: java.lang.NoClassDefFoundError: Could not
initialize class com.listeners.ExtentTestManager at
com.listeners.Listeners.onTestFailure(Listeners.java:56)
I am using testng version 6.9.10 in pom.xml.
Hi, I am getting this error when I use listeners in my xml file. If I remove listeners and run the xml file, it is working. Please let me know if anything wrong with ExtentTestManager class or the dependencies. I am unable to add code in stackoverflow, so I have attached in this link: selenium99.com/java-lang-noclassdeffounderror Pls check.
You can try maven update project in eclipse after that use clean install, it should work.
Basically
NoClassDefFoundError can occur for multiple reasons like
ClassNotFoundException -- .class not found for that referenced class irrespective of whether it is available at compile time or not(i.e base/child class).
Class file located, but Exception raised while initializing static variables
Class file located, Exception raised while initializing static blocks
But in this case it looks like you are missing some dependency in the building.
you can try cleaning whole project and build again.
If it's a eclipse project
mvn clean eclipse:eclipse
mvn clean package
try above commands

Get value from build.gradle in Spring Batch Project

I have some data in my gradle.properties file:
appVersion=1.4.31
appGroup=com.business
appName=projectName
I want to be able to access them in a .java project. I am attempting to access the variable like this:
#Value("${appVersion}")
private int version;
However, when I try to run the application from the main class, I get the following error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
Which shows that it can't find the correct variable.
Attempts at solving the problem
Adding a #propertySource Annotation
I have tried adding this to the main class
#PropertySource(value = "file:build.gradle", ignoreResourceNotFound = true) which was unsuccessful.
Making changes in build.gradle
I attempted the following, taken from this StackOverflow answer:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
Which was also unsuccessful.
This answer below
I did what was said in this answer (I had to add an application.properties file) and I got this error message:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Circular placeholder reference 'appVersion' in property definitions
I thought that it might be getting mixed up with using the same names (appVersion = appVersion), so I changed the application.properties line to
version=${appVersion}
And then in my .java file, I changed the line to:
#Value("${version}")
private String version;
And got the error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'appVersion' in value "${appVersion}"
This should work:
processResources {
filesMatching('application.properties'){
expand(gradle.properties)
}
}
But it seems like you forgot the other part. This just tells Gradle to process your application.properties file and replace any placeholders it finds. You need to also put a placeholder into your application.properties:
version=${appVersion}
Then your value annotation should work (but it should be a String not an int)
#Value("${version}")
private String version;
Finally, since you are launching via IDEA, you may need to make sure that gradle:processResources runs before your application starts so that Gradle has a chance to replace the placeholder you put in application.properties. You should be able to utilize the "Before Launch Options" section in your run configuration to have IDEA run the processResources Gradle Task after the build task. This should cause the placeholder to be properly replaced every time you run the application.
Also, just to help with the understanding. Gradle is the build system for the application, it's not part of the application itself. Once the project is built, everything related to Gradle is irrelevant. This is why you can't just do #Value("${appVersion}") and have it work. The application/Spring doesn't know anything about the tool that's being used to build it (and it shouldn't). So you need to somehow, at build time, inject the Gradle project version into the application if you want access to it. There are a number of ways of accomplishing this but the resource processing method above is a pretty common approach.

Project is tied to IDE or security?

I have a Java/Spring project for which I am responsible for familiarizing myself with the code. To do this, I am debug stepping through different parts of the program and studying the variable states and call stack. I would like to run this in IDEA since I like that IDE more and have used it before. Whereas the program runs fine in Eclipse-STS, when run in IDEA, I get this:
[ERROR] Application [main]: Can't build application context: Error creating bean with name 'entityManagerFactory' defined in class path resource [springconfig.xml]: Invocation of init method failed; nested exception is java.lang.SecurityException: class "org.eclipse.persistence.internal.helper.Helper"'s signer information does not match signer information of other classes in the same package
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [springconfig.xml]: Invocation of init method failed; nested exception is java.lang.SecurityException: class "org.eclipse.persistence.internal.helper.Helper"'s signer information does not match signer information of other classes in the same package
I noticed that there is a class Helper.java in our software but it is the only class in it's package so why would it complain about this class's signer information not matching that of the rest of the classes in the same package?
Elsewhere on Stack Overflow, I learned this can happen when a particular class is being imported by multiple JARs into the same project, hence causing the conflict. I wasn't sure how this conflict could be because as the image before shows this class is a native *.java file in the project and not imported from any JAR. What's more, it's the only class in it's package which makes the error message difficult to understand. But then I realized that our project also imports a JAR which has this class in it. Namely, we import eclipselink-2.5.0.jar. So, I thought that our source class when compiled might be conflicting with the Helper.class in this package.
So, I thought the original error message is about our Helper.class being treated as belonging to the JAR and so the IDE complaining that the JAR's manifest signature didn't match with any signature on our Helper.class.
So, as an experiment I tried to delete this JAR's signature manifest as well as delete the Helper.class from within it. Then I cleaned and recompiled the project to no avail.

Categories

Resources