r/learnjava • • May 24 '21

Java Environment and "how it works"

As everyone learning Java I started my first lesson with "you don't care about that weird public static void main(String[] args) bit, we explain later" months after, tutorials, and people never explained whats going on within that method, and going deeper how java actually works.

I banged into this problem and lack of explanation when I was trying to make VSC work as Java Dev Environment. I have so many questions...like what is actually java_home how does that work. How does Maven project differs from basic projects I was making in Netbeans, how is JavaFX part of Java packages but when running project it never compiles correctly.

I'm sure these are very naive questions with layers and layers of knowledge that I should have but never got to learn. Is there a good tutorial series or video playlist (pls dont link basic Java docs) that I could learn these things from?

55 Upvotes

7 comments sorted by

View all comments

4

u/sparkless12 May 24 '21

thanks for replies, I get that post is tl;dr. But my question wasnt't about deconstruction of public static void main(String[] args). Thank you for that tough, I will think about presenting my question better next time

6

u/GuyWithLag May 24 '21

So, a main method gets called* in the middle of a really large amount of other stuff that is going on. Assuming you just have a System.out.println("Hello World!"); in your main method, a fractal high-level representation of what happens when you run your program is:

  • The OS gets an instruction to execute a Java program (this could be your double-clicking an icon on your desktop, debugging something in IntelliJ, running a CLI command; all these are different methods)

  • The OS starts executing the java program with a bunch of parameters (these are set from one of the above methods). Why this one? Because it ws determined so from the initial point. What does "start executing it" mean? Locating it on disk{1} loading it and any dependencies into RAM and creating the necessary OS-specific structures. Locating it on disk depends on the current PATH (based on some of the above methods), which uses JAVA_HOME (in some of the methods).

  • java will then start to initialize stuff. Memory, heap, stacks, thread, loading the boot class path, setting up System in/out/err, setting up the class path from the command line options or environment variables, locating JARs on disk, loading up the main class.

  • At this point your main method gets interpreted; it writes something to System.out, and that something percolates out to the OS. It might end up in the terminal, in a file, in an IntelliJ window - but your program doesn't care.

  • After main returns, the JVM is shutting down; waiting for and closing threads, freeing resources, and terminating its thread

  • The OS then cleans up any resources that were in an intermediate stage.

Now, note that this is about executing stuff, and some of the questions you have fit into this: JavaFX isn't part of the classpath by default, so unless you add it manually, it's not going to be there. Maven does dependency management, so it will track dependencies, including transitive dependencies, download everything and set up the class path for mvn java:exec to work correctly.