Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Java’s duplicate class error means two top-level types with the same name are declared in the same package. A mismatched-filename error is different: it means a public type’s source file does not use the expected name, or the source file is not organized where the compiler expects it. Check the diagnostic, declarations, package statements, filenames, and build inputs to identify which problem you have.

What the two Java errors mean

“Duplicate class” means a same-package name collision

The Java Language Specification makes it a compile-time error for a top-level type’s name to also be the name of another top-level class or interface declared in the same package. In practical terms, look for two top-level declarations such as class Person and public class Person that belong to the same package. The rule concerns top-level types; a nested class is not itself a second top-level declaration. See the Java SE 26 Language Specification, Chapter 7.

A filename error concerns source-file organization

A compiler may report that a public class should be declared in a correspondingly named file—for example, class Person is public, should be declared in a file named Person.java. This is a filename complaint, not proof that another Person declaration exists. The JLS describes filesystem-based naming restrictions that a host may enforce for public types and types referenced from other compilation units. In a conventional source tree, a public type named Toad in package wet.sprocket belongs in Toad.java under the matching package directory.

OpenJDK’s compiler resources include distinct diagnostic text for duplicate class: {0} and bad file name: {0}. The wording varies by diagnostic and compiler context, so use the actual message and named file to guide the investigation. See OpenJDK’s compiler diagnostic resources.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

How to tell which problem you have

Clue Duplicate top-level declaration Filename or source-layout mismatch
Typical diagnostic duplicate class: Person A message that a public class should be declared in a file named Person.java, or a bad-file-name diagnostic
What to verify Whether another top-level type with that name is declared in the same package Whether the public type’s spelling and capitalization match the .java filename
Package and path Compare package declarations; same simple names in different packages do not trigger this same-package rule Check that the source file is in the directory corresponding to its declared package in a conventional source tree

Find and fix the cause

  1. Start with the complete compiler message. Note the reported type name, source file, and line. Begin at the named location, but do not assume that the first file mentioned is the only relevant source.
  2. Search all compiled sources for another top-level declaration. Look for a class, interface, enum, or record with the same name. Confirm that it is top-level, rather than nested inside another type.
  3. Compare package declarations. A package statement determines a compilation unit’s package membership; a file with no package statement is in the unnamed package. If a type T is in package P, its fully qualified name is P.T. Two types with the same simple name in different packages are not the same-package collision described by the JLS.
  4. For a filename complaint, compare exact names. If the public top-level declaration is public class Person, make the source file Person.java, including the same capitalization. Also check that its folder matches its package in your source tree.
  5. Inspect build inputs and source roots. Check whether the build compiles a second copy of the source or includes the same source through more than one input or source root. Remove the unintended duplicate input or consolidate the declarations as appropriate. The particular build configuration is project-specific; it is not a separate language-specification rule.
  6. Recompile after one targeted change. If another diagnostic appears, investigate it on its own. Renaming a file can address a filename complaint without removing a duplicate declaration.

Common causes and corrections

Two declarations share a package

Search for repeated top-level declarations and decide which one should remain. If both types are needed, give them distinct names or put them in different packages, then update references accordingly.

The package statements do not match the folders you expect

A source file’s package statement—not merely its folder name—determines its package membership. Correct an unintended package declaration or move the file into the appropriate package directory and keep the project’s source-root configuration consistent.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

The public type and filename differ

Rename the source file to match the public top-level type, preserving capitalization, or rename the type and its references to match the intended filename. Check the actual filesystem spelling, particularly when code is moved between systems with different filename case behavior.

The build sees another copy of the source

When declarations appear unique in the files you opened, inspect the build’s source roots and compiler inputs for generated, copied, or otherwise unintended source files. Fix the input set rather than changing an unrelated declaration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why one change can reveal two errors

The diagnostics answer different questions: whether a top-level name is declared twice in a package, and whether a source file is named or organized as expected. A project can have both problems, or fixing one can expose the other. After each edit, compile again and follow the new diagnostic instead of treating every message as a filename issue or every repeated name as a duplicate-class error.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft