Reputation: 367
I have a piece of code where I copy the similar properities of one class to another using BeanUtils.copyProperities(dest, orig)
. However. This does not work. I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
I am using BeanUtils 1.9.2, Java 8, Windows 10, Eclipse.
import org.apache.commons.beanutils.*;
public class Main{
public Main(){
Entity entity = new Entity();
AbstractGameObject aEntity = new AbstractGameObject();
try {
BeanUtils.copyProperties(aEntity, entity);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(aEntity.similar); // Should print out 10, No?;
}
public static void main(String[] args) {
Main main = new Main();
}
private class Entity{
int similar = 10;
int differentE = 9;
public Entity(){
}
}
private class AbstractGameObject{
int similar = 2;
int differentA = 1;
public AbstractGameObject(){
}
}
}
Upvotes: 0
Views: 3518
Reputation: 473
Also, just a note that BeanUtils.copyProperties()
will not work if you use Lombok to generate your public getters and setters. You must manually create them.
Upvotes: 3
Reputation: 5048
Important: classes must be public and copyProperties uses setters and getters. Try with:
public class Main {
public Main() {
Entity entity = new Entity();
AbstractGameObject aEntity = new AbstractGameObject();
try {
BeanUtils.copyProperties(aEntity, entity);
} catch (Exception ex) {
// use a logger
}
System.out.println(aEntity.similar);
System.out.println(entity.similar);
}
public static void main(String[] args){
Main main = new Main();
}
public class Entity {
private int similar = 10;
private int differentE = 9;
public int getSimilar() {
return similar;
}
public void setSimilar(int similar) {
this.similar = similar;
}
public int getDifferentE() {
return differentE;
}
public void setDifferentE(int differentE) {
this.differentE = differentE;
}
}
public class AbstractGameObject {
private int similar = 2;
private int differentA = 1;
public int getSimilar() {
return similar;
}
public void setSimilar(int similar) {
this.similar = similar;
}
public int getDifferentA() {
return differentA;
}
public void setDifferentA(int differentA) {
this.differentA = differentA;
}
}
}
Upvotes: 1