java.lang.NoSuchMethodError , build process is broken - java

I've a problem with my build and it caused a huge headache for me.
I had an old class and I was using it to fetch data from it, and I created a new class with the same methods. When I test it locally on my machine, everything works fine, but when I try to do a build, it broke because it's unstable and I got this error in the log file:
Caused by: java.lang.NoSuchMethodError: com.mashvisor.bean.Neighborhood.getTraditionalRates()Lcom/mashvisor/database/dao/views/NeighborhoodRentalRates;
at com.mashvisor.database.dao.PropertyDao.retrieve(PropertyDao.java:91)
The NeighborhoodRentalRates class is the old class, and in my code I'm sure im not using it nor trying to access it in that line, here's my code for that line:
Hibernate.initialize(property.getNeighborhood().getTraditionalRates());
and here's it's declaration
public TraditionalNeighborhoodRentalRates getTraditionalRates() {
return traditionalRates;
}
The TraditionalNeighborhoodRentalRates is the new class, and the only change here is the class name.
Could any body help?

Your code is still calling the old method, i.e. it is looking for a method with the signature:
public NeighborhoodRentalRates getTraditionalRates() { ... }
Just using the same names it not enough. To have classes with the same (method-)interface, you have to have the same names, return types and argument types in all methods.
So you need to go through your calling code and make sure the new type is expected everywhere as return type and recompile the calling code.

Related

Java NoSuchMethodError when Method Exists

I am referencing PlayerUtil.getMovementSpeed(player); in my Speed class, and in my PlayerUtil class, I have the method defined as:
public static double getMovementSpeed(Player player) {
//my code here
}
But whenever the getMovementSpeed method is referenced in my other classes, it throws this error:
java.lang.NoSuchMethodError: net.Swedz.util.PlayerUtil.getMovementSpeed(Lorg/bukkit/entity/Player;)D
I thought it may be that Eclipse was exporting incorrectly, but I rebooted it and tried again with no avail.
EDIT: I did try decompiling the exported jar, and the public static double getMovementSpeed(Player player) method does exist in the exported jar.
EDIT: My friend is also having a similar issue, and is using IntelliJ, so Eclipse is not the issue.
EDIT: Class definition for PlayerUtil:
package net.Swedz.util;
public class PlayerUtil implements Listener {
//getMovementSpeed is defined in here
}
Class definition for Speed:
package net.Swedz.hack.detect.move;
public class Speed implements Hack, Listener {
//my detection methods and method containing PlayerUtil.getMovementSpeed(player);
}
SOLUTION: I found on my own that I had classes conflicting between two plugins on my server. I had one jar with net.Swedz.util.PlayerUtil and another with net.Swedz.util.PlayerUtil both with different contents. I added my project name in all lower case after the net.Swedz and it seems to have fixed it!
Thanks!
This is a very simple to troubleshoot.
you have used that method and you were able to compile that class which uses this method.
so that means at compile time it reefers the class PlayerUtil which has this method.
But runtime class loader has loaded the class PlayerUtil which doesn't contain this method.
now what you have to do is just find out where that class has been loaded from (at run time)
if you can recreate the problem while it is running using eclipse/IDEA follow these steps.
(if it runs in in application server or standalone application, then start the application server or application with debug enabled.and you can do remote debug from your IDE).
put a break-point where exception was thrown (where you call this method).
start to debug , it will hit the break-point.
then evaluate this expression PlayerUtil.class.getResource("PlayerUtil.class")
4.you can find the path where the class was loaded from.
now you have two options , decompile the class and check whether that method is these (same return type, same name , same args).
or in debug , you can evaluate PlayerUtil.class.getDeclaredMethods() to find out.
So you can solve the problem by rectifying the class path entries if it was loaded from a wrong place.

java.lang.NoClassDefFoundError only in particular conditions

My problem is that when class B tries to use A.check() my execution stops due to a java.lang.NoClassDefFoundError.
So here is my class configuration. NB: the classes are in the same packages and I have already checked that the A.class file is placed where it should be.
public class A{
// vars
// declare some public method
public synchronized static boolean check(){
//do stuff, log some info and return boolean
}
}
public class B implements Runnable{
public void run() {
A.check();
}
}
And here is my stacktrace:
java.lang.NoClassDefFoundError:
org/mypackage/A
at org/mypackage.B.run()
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException:
org/mypackage.B
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
The project is really big and class A is used lots of times before this call without any problem, so i don't think that is something relative to the classpath. Note that this is part of the last call of the software that should close up everything.
Moreover, I have two maven goals: the first one execute the questioned code without any problem, instead the second rise this error every time.
So I have solved my problem and I post here the solution so maybe can be useful for someone else.
First of all the error: java.lang.NoClassDefFoundError
This error is really different from ClassNotFoundException and this is where I'have lost a lot of time.
NoClassDefFoundError in Java is raised when JVM is not able to locate a particular class at runtime which was available at compile time. For example, if we have a method call from a class accessing any member of a Class and that class is not available during runtime then JVM will throw NoClassDefFoundError. It’s important to understand that this is different than ClassNotFoundException which comes while trying to load a class at run-time only and the name was provided during runtime, not on compile time. Many Java developer mingles this two Error and gets confused. Here I quote a really useful blog that I uesd.
So in a shorter way NoClassDefFoundError comes if a class was present during compile time but not available in java classpath during runtime.
But even with those information the problem was still there until I found the mystery: one of the reason that can place the class in a state that can be compiled but not located at runtime is that if you have static initialization that fail (e.g. in my class I had as field a static variable instantiated badly).
So remember to check for you initialization phase if you have static variables in your class this could be the reason of your java.lang.NoClassDefFoundError.
By the way I don't get why this kind of error is not raising some more meaninful errors for example java.lang.ExceptionInInitializerError or something like that.
Try to debug maven execution by running: mvn -X <your_goals>
It would be useful to see your POM file.
If you are working with spring mvc and if you made bean entry in dispatche-servlet.xml for Controller class.
Example :
<bean id="MyClass" class="com.aaps.myfolder.MyClass">
<property name="methodNameResolver">
<ref bean="methodNameResolver" />
</property>
</bean>
And if MyClass.java is not compiled & if no class file is generated in classes folder of your project folder then it wil show java.lang.NoClassDefFoundError.
So check whether the MyClass.class is created or not in classes folder if you are working with spring mvc.
Does Class A have anything that is done in a static block. You can get this exception even if a class is being loaded and static blocks fails for any reason reason. try to put in logging to see if something like this is happening.

method undefined for the type error

I have three different projects which has its java source files DBHandler (DBHandler.java) DataObject (DataObject.java) and LinkedIn (LinkedIn.java)
DBHandler has a method addLinkedInMessages(lpo) which takes the dataObjects' object.
and my LinkedIn calls the method addLinkedInMessages(lpo). Now I'm getting error
The method addLinkedInMessages(LinkedInPageObject) is undefined for
the type LinkedInDBHandler LinkedInPageLoader.java
However here is my code in the LinkedInDBHandler
public ArrayList<LinkedInAccountObject> getAllLinkedInUsersFromDatabase() {
ArrayList<LinkedInAccountObject> laoarray = new ArrayList<LinkedInAccountObject>();
LinkedInAccountObject lao = new LinkedInAccountObject();
return laoarray;
}
I added the dependent projects to other projects build path. But I'm unable to call the newly created methods. However I'm able to use the existing methods.
Please any one help me
You must add method addLinkedInMessages(LinkedInPageObject) which gets object of type LinkedInPageObject, to class LinkedInDBHandler.
What this exception means is you don't have a method called addLinkedInMessages accepting an attribute of type LinkedInPageObject in class LinkedInDBHandler.
So in your LinkedInDBHandler.java you must include:
public void addLinkedInMessages(LinkedInPageObject linkedInPageObject)
// ^--- > or the return type you need
Thanks for your answers.
I actually had the method addLinkedInMessages(LinkedInPageObect) in the LinkedInDBHandler.java file.
The project was added and also build created and added the jar file into the project. Removed the jar resolved it since the jar was exported when these methods was not added.
Thanks once again for everyone posted answer here

Working around IllegalAccessError

I have the following situation:
2 Eclipse projects in the same workspace, Apa and Bepa (pseudonyms for the sake of brevity).
Project Apa references (includes) project Bepa.
I have a class X in Bepa, with public method b().
Invoking X.b() directly works fine in project Bepa.
However, creating a reference to an instance of X in Apa, and then invoking b() on this reference, results in this:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method java_cup.runtime.Symbol.<init>(II)V from class de.uni_freiburg.informatik.ultimate.smtinterpol.util.MySymbolFactory$LineColumnSymbol
at de.uni_freiburg.informatik.ultimate.smtinterpol.util.MySymbolFactory$LineColumnSymbol.<init>(MySymbolFactory.java:31)
at de.uni_freiburg.informatik.ultimate.smtinterpol.util.MySymbolFactory.startSymbol(MySymbolFactory.java:95)
at java_cup.runtime.LRParser.parse(LRParser.java:393)
at de.uni_freiburg.informatik.ultimate.smtinterpol.smtlib2.ParseEnvironment.parseStream(ParseEnvironment.java:152)
at de.uni_freiburg.informatik.ultimate.smtinterpol.smtlib2.ParseEnvironment.parseScript(ParseEnvironment.java:118)
at de.uni_freiburg.informatik.ultimate.smtinterpol.smtlib2.SMTLIB2Parser.run(SMTLIB2Parser.java:47)
at de.uni_freiburg.informatik.ultimate.smtinterpol.Main.main(Main.java:121)
at de.uka.ilkd.key.keynterpol.KeYnterpolInterface.main(KeYnterpolInterface.java:36)
From what I understand, the culprit is a third-party jar referenced by B. However, I cannot for the life of me figure out why I can only use it from within Bepa, and not Apa. Any help would be greatly appreciated.
IllegalAccessError is a subclass of IncompatibleClassChangeError. If an IncompatibleClassChangeError is thrown while your code is executing, this typically indicates that the your code isn't running with the same classes it was compiled against.
In your case, the method that is causing the IllegalAccessError to be thrown is a constructor of the java_cup.runtime.Symbol class that has default visibility:
Symbol(int sym_num, int state)
{
sym = sym_num;
parse_state = state;
}
The code that calls this constructor is in a different package and so should not be able to call this constructor directly. Normally an error such as this is caught by the compiler, as attempting to access a package-private member from another package should cause a compiler error. In your case however, you don't appear to have any such compiler error.
If you are running your code with the same classpath that you're compiling it with, then I can only guess that the java_cup.* classes appear more than once on the classpath, with different visibilities for the constructor above. The compiler must have found one copy of the Symbol class with this constructor being public and the JVM must have found a copy that had this constructor package-private.

"constructor has private access" error message

I'm working in Java and have come across an incredibly odd error. I have a very basic class as follows:
public class ClassA{
private static Logger log = Logger.getLogger(ClassA.class.getName());
private boolean trace;
public ClassA(){
trace = log.isTraceEnabled();
}
public void doSomething(){
//does stuff
}
}
I can use this class just fine within my current project. However, when I build, package, and install to my local repo (using Maven, no remote artifact repo set up), other projects cannot properly use this class because they cannot instantiate it. When I try anything like:
ClassA classA = new ClassA();
I get the following compilation error:
ClassA() has private access in [package].ClassA
I've decompiled the .jar in my local repo to ensure the constructor is present and is public - it is. I've also used the -U flag to force updates and the compilation continues to fail. What could be causing this error?
Maybe you have some other ClassA.class file somewhere in the classpath. Check all the jars used by the project that cannot call the constructor: one of them should contain an old version of your class.
My only thought is that you have a problem with your package. Make sure to define the package at the top of the source file for classA using the package keyword. When you call it ensure that the file is in include list with the include keyword. You could be running into the error because ClassA exists in some default package and that is what you are actually calling instead of calling your locally made ClassA class. The code you posted looks fine and you have already double checked to ensure the changes have taken effect in your repository.
//for those with Kotlin-Java mixed projects:
If the said file (With constructor) is in Kotlin and is being used in Java:
Instead of A a = new A(); //which causes the said error
Use A.INSTANCE. …
I have this error, where write "private", instead "public" for class constructor;

Categories