Forum Stats

  • 3,769,403 Users
  • 2,252,961 Discussions
  • 7,875,020 Comments

Discussions

Why do we have two .class files created when compiling a java code?

User_2Q3RQ
User_2Q3RQ Member Posts: 1 Green Ribbon

Hi I just started learning java.

What troubles me is when a .java file is compiled using javac in the terminal, why are there 2 .class files (one in the out folder and another in src folder).

Tagged:

Answers

  • Vasily Strelnikov-Oracle
    Vasily Strelnikov-Oracle Member Posts: 15 Employee

    Compiler creates at least one .class file containing complied code per each .java source code file. This could be more than one .class file if a specific .java source file defines nested classes. Where these classes are created depends on the use of compiler command line options i.e. the -d that sets the destination folder for your classes. If -d is not set than the current folder is used as a destination where compiled class files would be stored. You've probably executed compilation more than once, with either different -d values, or with no -d at all, thus resulting in the duplication of compiled classes. You can simply erase all redundant copies of these class files, or even erase all of your compilation results, and just run compiler again, making sure you properly set compiler output destination -d option.