Single Source File Java Programs
YAJC become Java programmer
As we already know, Java is a versatile and powerful programming language that is widely used for developing complex software applications. One of the features of Java is that you can write and run programs using a single file without the need to compile it separately. This feature has made Java programming much easier and more accessible for beginners. Please welcome Single Source File Java Applications.
In Java 11 they have introduced this new feature. In order to use it you just need to have a source file of a Java program and pass it as a parameter to java command. To be honest, there is not much to describe without real examples, so let's get to some of them.

We see it in the sample that we don't invoke javac but despite it the program runs just as it was compiled before. The answer why it works is simple: Java compiles it for us when we ask it to run the file.
Let's have a look at some more complicated program. Here's an example of a Java program that reads a file and counts the number of lines in it:

This sample shows us a more complicated version of a Single Source File Java Program.
We have LineCounter.java program and myfile.txt containing 3 lines. The program reads user parameters and accesses a file on a file system to count the number of lines the file contains.
In terminal section we call
ls - list the files.java LineCounter.java myfile.txt - run the program without compilation passing myfile.txt as a parameter.java LineCounter.java - run the program without parameter to ensure we will se an error message which tells us how to run the program correctly.
In conclusion
Single source file Java programs offer a simple and efficient way to write and run Java applications. By eliminating the need for a separate compilation step, Java developers can save time and simplify their workflows. Single file Java programs are particularly useful for
- quick prototyping
- small scripts
- educational purposes
Moreover, since Java is a widely-used programming language with a vast ecosystem of libraries and frameworks, single file Java programs can be built to solve a wide range of problems.
The next time you need to do something with a file quickly, remember this cool feature 😉
Feel free to ask any questions in the comments and don't forget to motivate us with your likes 👍 we in turn will motivate you to be a part of this wonderful programming world!