I'm sorry for my question ...

As a result of compiling some file, say, myProga.java, within which three classes are registered (C1, C1 and C3), the output is three files with names, C1.class, C2.class and C3.class.

It bothers me, but what if the program will consist of several dozen classes? After all, then several dozen class files will be created. I think this is not the best way to create programs.

The only tool I have found so far is the use of packages. However, the mechanics of packages are such that it does not combine class files, but only pulls them away into subdirectories. In other words, as there were a huge number of files, it remained so.

What do I want? After the compilation, I would like to see one file as it is done in C / C ++. Well, or at least a few pieces. But not dozens scattered on subdirectories!

Perhaps I do not understand something. I am not a Java programmer, I am a Schnick. I'm only reading the java book for the fifth day.

Please give me a magic pendel in the right direction. Thank!

  • Usually these classes and packages are packaged in a single file, for example, jar. - Pavel Parshin 5:56 pm
  • You, as sishnik, are familiar with object files, I suppose ... :) So here's a similar situation, just instead of linker (linker), like an archiver. - D-side
  • In IntelliJ IDEA, for example, in order to assemble a jar from your project files, you will need to add an artifact of the assembly and then build it ( Build/Build Artifacts... ). - StateItPrimitive
  • Yeah, got it! Next, I'll figure it out. (Ironically - in the previous question, a friend asks about the problem with jar.) Thank you all! The question can be considered closed. - Zhevak Alexander Antonovich
  • @ZhevakAleksandrAntonovich It seems to me that this is a rather popular question among novice java-programmers and should not be closed. You just need someone to write everything humanly in the answer (useful to others). - StateItPrimitive

1 answer 1

I will try to reassure about a large number of files. Good or bad, but the creators of java decided to use a flat model of classes where each class and interface compiled into a separate file. This applies to both normal classes and anonymous classes. At the core of this solution, I think, was the simplification of the implementation of the class loader. If several classes were written to one file, then in order to find and load a class, the loader would either look through all the files or have a table with paths for each class. With a flat structure, the path to the class file is constructed simply from the name of the class and the package. (For anonymous classes a bit more complicated) In java 8 with the addition of lambda, the number of files has increased dramatically because each expression is also compiled into a separate file. Inside, lambda is the same anonymous class.

In general, tens and even thousands of .class files are normal, and java works with them quickly enough. In everyday life, working with class files is very rare. Mostly programs and libraries are packaged in jar, war and ear archives. No one compiles java classes manually. Automation tools such as ant and maven are used for this.