2016-07-31 3 views
0

У меня есть фрагмент кода, в котором я копирую аналогичные свойства одного класса другому, используя BeanUtils.copyProperities(dest, orig). Однако. Это не работает. Я получаю ошибку:Невозможно скопировать свойства одного класса другому с помощью BeanUtils

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

Я использую 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(){ 

     } 
    } 
} 
+0

Добавить библиотеку Обще-протоколирования http://commons.apache.org/proper/commons-logging/ –

+0

классам @ DavidPérezCabrera Можете ли вы уточнить? – Colourfit

+0

Конечно, 'BeanUtils' использует' Common-logging' (и 'commons-collections'), если вы используете maven, gradle или подобное, он получит зависимости BeanUtils для вас, но если вы этого не сделаете, вы должны для добавления зависимостей beanUtils к пути к классам вручную. –

ответ

1

Важно: классы должны быть общедоступными, а copyProperties использует сеттеры и геттеры. Постарайся с:

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; 
     } 

    } 
} 
+0

Этот код все еще работает, я получаю ошибку --- «Исключение в потоке» main «java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap' – Colourfit

+0

@Colourfit вы добавили' библиотека коллекций-коллекций для classpath? https://commons.apache.org/proper/commons-collections/ –

+0

Просто добавил. Еще доцентная работа. Такая же ошибка. – Colourfit

Смежные вопросы