Results 1 to 6 of 6
  1. #1
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Compile Error: Duplicate Class.. (after package) but I don't have a duplicate class..

    This error is really bizarre..

    I developed a Library .java file. I wish to package it up.

    Say the file is : C:\java\ABC\lib.java

    if I don't have package, it will compile cleanly.

    However, if I add on the top line:

    package ABC;

    it gives a compile error:
    .\lib.java:7: duplicate class: ABC.lib
    public class lib {
    ^

    It's just 1 folder 1 .java file. Can't possibly have another lib class in this same package.. Googling doesn't give me helpful information.. The discussion out there mostly focused on "duplication".. I don't have that..

    Can someone give me a hand?

    Thanks!

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,930
    Rep Power
    19

    Default Re: Compile Error: Duplicate Class.. (after package) but I don't have a duplicate cla

    How do you compile?
    What else is in the directory when you compile?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: Compile Error: Duplicate Class.. (after package) but I don't have a duplicate cla

    Hi Tolls,

    Thanks for helping me again! Very nice hint!

    Before I simply do..

    c:\java\ABC> javac *.java

    but after reading your post, I did:

    c:\java\ABC> javac lib.java

    the packaged code gets compiled!!

    In the directory ABC, there are 2 java files. lib.java and its client testlib.java

    lib.java is a bunch of static functions.
    testlib.java calls them using lib.testFunction(a,b,c) etc

    I can sort of guess now what's the difference between the 2 compilation, but I've had no formal experience with packaging code so I wish to hear your insides on why did the error occurred before.

    Moreover, I have a new problem right now. lib.java is now compiled to be in the package ABC.

    I now need to recompile testlib.java

    I added a line
    import ABC.*;

    on top.

    with or without this line, both compilation returns me the error:


    C:\Java\ABC>javac testlib.java
    testlib.java:29: cannot access lib
    bad class file: .\lib.class
    class file contains wrong class: ABC.lib
    Please remove or make sure it appears in the correct subdirectory of the classpath.
    zz = lib.testFunction(a, b, c);

    ^
    1 error


    I did some experiments of packages before and I realize if the client program is in a different folder, it will compile..

    Could you give me some insights regarding this?

    I actually try to find discussions or tutorial that extensively discuss packaging alone, but I can't find any quick ones.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,930
    Rep Power
    19

    Default Re: Compile Error: Duplicate Class.. (after package) but I don't have a duplicate cla

    It's all down to the classpath.
    When you try to compile testlib it is looking for the class 'lib'.
    It finds it on the classpath (which defaults to '.', ie the current directory), except that 'lib' is actually 'ABC.lib' so it flags an error, because 'ABC.lib' should be in a directory 'ABC' on the classpath and it isn't.

    SO you need to set the classpath instead of relying on the default:
    javac -cp .;C:/java testlib.java

    which puts the current directory ('.') as well as the directory containing the ABC folder, which is the root of your packages.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: Compile Error: Duplicate Class.. (after package) but I don't have a duplicate cla

    I understand the classpath part. I in fact have .;C:\java set as my classpath environment variable. I've done other package using this way before.

    I notice it's placing the client java file in the same direction as the package that's giving the problem...

    I just moved testlib.java to a different folder called DEF, i tried to place DEF as a subfolder of ABC or in parallel as ABC, both way compiles, but when the client (importing ABC.*;) is in the same dir as ABC class files, it fails.

    I read up a bit about the sample project path structure (like those in eclipes and maven), I notice they place source .java file in src folder, compiled .class file in a com folder, and test client in a paralleled structured folder.. I guess there must be a strong reason for that practice. The striking element related to this context is the test client is not at the same folder as the .class files.

    These are all my extrapolated understanding.. tried before to read up a system discussion/tutorial on projects/packages alone but to no avail.

  6. #6
    otosaat is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Re: Compile Error: Duplicate Class.. (after package) but I don't have a duplicate cla

    Here is similar test case and solution to it. I'd also suggest that you use the suggested naming convention to make the discussion easier. For instance, use lowercase name for packages, and uppercase for class file/names.

Similar Threads

  1. Duplicate class error
    By hamedy in forum NetBeans
    Replies: 7
    Last Post: 06-28-2012, 05:02 PM
  2. Replies: 1
    Last Post: 02-02-2012, 12:53 PM
  3. Duplicate Class Error Message
    By bruceaj in forum New To Java
    Replies: 8
    Last Post: 11-05-2011, 11:50 AM
  4. Replies: 4
    Last Post: 05-13-2011, 10:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •