Ошибка в java duplicate class

Confused as to why I get duplicate class error for the following code?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database_console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


/**
 *
 * @author davidsonr
 */
public class DBConnect {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
}

Netbeans highlights DBConnect as red with duplicate class error.

asked Oct 11, 2013 at 12:56

Robbo_UK's user avatar

Robbo_UKRobbo_UK

11.2k25 gold badges80 silver badges117 bronze badges

7

This is a known issue with netbeans BUG 226360

it might help to clear Netbeans cache:

Go to Help -> About and you will see

Cache directory: PathtoDirectory

Close NetBeans, go to specified directory and delete everything.

Anish Sana's user avatar

Anish Sana

4881 gold badge11 silver badges30 bronze badges

answered Sep 29, 2014 at 21:07

Eduardo Dennis's user avatar

Eduardo DennisEduardo Dennis

13.5k13 gold badges81 silver badges105 bronze badges

7

This can also happen if the package name does not match the folder name, or if the package name is omitted. Check the package statement in your source.

answered Sep 8, 2020 at 16:36

Sudeep Prasad's user avatar

1

this might be due to 2 classes with the same name in the same package

answered Oct 11, 2013 at 12:57

SpringLearner's user avatar

SpringLearnerSpringLearner

13.7k20 gold badges77 silver badges116 bronze badges

This also happens if your referencing the erroring class in a separate file in the same package, with the erroring class with an unmatching package path to the file where you are referencing erroring class.

For example
file 1
some.incorrect.path.package

class_that_is_erroring{
}

file 2
some.correct.path.package

class new_class{

class_that_is_erroring myclass = null;

}

The package paths in both files must match each other and match the file system directory.

answered Feb 25, 2019 at 13:48

Christopher Hoffman's user avatar

If the file name doesn’t match the class name, NetBeans 8.0.1 will report this as a duplicate class.

answered Oct 7, 2016 at 17:50

james.garriss's user avatar

james.garrissjames.garriss

12.9k7 gold badges82 silver badges96 bronze badges

A new answer… In this case, the duplicate class error was confusing. So was part of the next error, but it also pointed to the real problem and the fix.

From my log file:

Error 1: agGoodClass error:duplicate class: a.g.GoodClass //Not the problem
Error 2: abBadClass error: cannot access GoodClass        //The problem
         bad source file: agGoodClass                     //No, it's fine
         file does not contain class x.y.GoodClass          //How to fix it
         Please remove or make sure it appears in the correct subdirectory of the sourcepath.

Java reports the first line of Error 2 because BadClass is using a wildcarded import, either import x.*; or import x.y.*;. The java compiler found x.y.GoodClass first, so couldn’t determine which you really wanted: a.g.GoodClass or x.y.GoodClass.

THE FIX: remove the wildcarded import and add the specific imports you need from library x.y.

answered Dec 2, 2021 at 21:51

cb4's user avatar

cb4cb4

6,2535 gold badges45 silver badges56 bronze badges

Друзья, помогите, пожалуйста, разобраться с ошибкой… IDEA? Может, и кода, мне не удалось до конца разобраться в причинах возникновения duplicate class :(
И почему-то при возникновении ошибки открывается старая, уже решенная задача. Может, надо как-то руками чистить историюпапку с задачами, прежде чем открывать новую?

Далее сам код, если ошибка всё же в нём, а не в обновлении задач JavaRush в IDEA… Хотя валидатор принял.
Но надоело вслепую валидатору кидать решения, без возможности запустить код в среде :(

package com.javarush.task.task06.task0613;

/*
Кот и статика
*/

public class Solution {
public static void main(String[] args) {
Cat cat1 = new Cat();
Cat cat2 = new Cat();
Cat cat3 = new Cat();
Cat cat4 = new Cat();
Cat cat5 = new Cat();
Cat cat6 = new Cat();
Cat cat7 = new Cat();
Cat cat8 = new Cat();
Cat cat9 = new Cat();
Cat cat10 = new Cat();

System.out.println(Cat.catCount);
}

public static class Cat {
public static int catCount = 0;

public Cat()
{
Cat.catCount++;
}
}
}

Как исправить дублирование классов?

Очистка кэша с перезагрузкой не помогает.
64218d24243cc905709841.png

Лог ошибки

> Task :app:checkDebugDuplicateClasses FAILED
Execution failed for task ':app:checkDebugDuplicateClasses'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable
   > Duplicate class com.google.android.gms.internal.measurement.zzhx found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.internal.measurement.zzmz found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.internal.measurement.zzna found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.internal.measurement.zznb found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.internal.measurement.zznc found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.internal.measurement.zznd found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzga found in modules jetified-play-services-measurement-19.0.0-runtime (com.google.android.gms:play-services-measurement:19.0.0) and jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2)
     Duplicate class com.google.android.gms.measurement.internal.zzhe found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzhf found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzhg found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzhh found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzhi found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzhj found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzio found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)
     Duplicate class com.google.android.gms.measurement.internal.zzit found in modules jetified-play-services-measurement-base-20.1.2-runtime (com.google.android.gms:play-services-measurement-base:20.1.2) and jetified-play-services-measurement-impl-19.0.0-runtime (com.google.android.gms:play-services-measurement-impl:19.0.0)


  • Вопрос задан

    27 мар.

  • 110 просмотров

Пригласить эксперта

Попробуйте добавить следующую строку в блок зависимостей.

implementation('com.example.library:library:1.0.0') {
    exclude group: 'com.google.android.gms', module: 'play-services-measurement-base'
}


  • Показать ещё
    Загружается…

13 июн. 2023, в 18:39

5000 руб./за проект

13 июн. 2023, в 18:22

180000 руб./за проект

13 июн. 2023, в 18:21

2000 руб./в час

Минуточку внимания

Dustin Marx

About |

A software developer’s public collection of tips and tricks, real-world solutions, and industry commentary related to Java programming.

Developers who are new to Java can sometimes have trouble with class and package naming. In fact, the introductory Java forums are filled with threads starting with questions about these areas of Java. In this blog post, I look at some of these errors and some of the causes of these errors.

One of the more obvious errors occurs when a public Java class is named differently than the file that contains the class definition.

This is demonstrated in the next screen snapshot. In this example, a class was declared as public with the name Person, but was saved in a file called Person2.java. The error message is pretty explicit: «class Person is public, should be declared in a file named Person.java»

The «duplicate class» error can sometimes be a little more tricky to resolve. One situation in which this occurs is when two source code directories include the same class with the same package structure. This error looks like that shown in the next screen snapshot.

As the above screen snapshot indicates, the class dustin.examples.Person exists in both the src2 directory and (not shown here) in the src directory («duplicate class: dustin.examples.Person»). Indeed, these are duplicate classes, at least in terms of package and class name.

The «duplicate class» error can also occur when the class is named the same with the same package naming hierarchy, even if one of the classes exists in a directory structure with directory names different than the package names. This is shown in the next screen snapshot.

This screen snapshot demonstrates that the «duplicate error» occurs when the class names and declared package names match even if the source file exists in directories with different names (different even than the declared package structure). What this implies is that if a particular class was copied to another directory without changing the package statement and the new directory was in the source path for javac, the «duplicate class» error will occur.

Another interesting observation from the last example is that javac uses the package statement to decide where to build the .class file rather than using the source file’s location in a directory structure.

In the blog post Java duplicate class error, Morgy describes a situation in which he ran into the «duplicate class» error message (he had neglected to place the class’s package declaration at the top of the class).

Conclusion

In this post, I have attempted to show some common causes of errors that can be troublesome for those new to Java. Specifically, I have demonstrated some of the common causes of the «duplicate class» error and of the public class not matching its file name.

This story, «Java: «duplicate class» and Mismatched File Name Error» was originally published by

JavaWorld.

Dustin Marx is a principal software engineer and architect at Raytheon Company. His previous published work for JavaWorld includes Java and Flex articles and «More JSP best practices» (July 2003) and «JSP Best Practices» (November 2001).

Copyright © 2010 IDG Communications, Inc.

Im using regular 2.7.1 jars, and appear work fine now. describe here my instructions:

1.- Add androidannotations to /lib
2,- Add androidannotations-api to /libs and add it as library.
3.- Set the (1) androidannotations as processor in settings, mark content root, and use «gen» and «aa-tests»
4.- androidannotations-api must be with compile scope.

These steps works for me, before i was using gen/aa as folder, but fails sometimes with duplicate class, now with «gen» work fine, and i think that is not bad mix it with R and others.

Thanks alls for the tips!

Понравилась статья? Поделить с друзьями:
  • Ошибка в ps4 как перезапустить
  • Ошибка в mortal kombat komplete edition d3d
  • Ошибка в itunes при подключение на айфон
  • Ошибка в premiere pro файл на диске открыт
  • Ошибка в modern warfare 2 мультиплеер