'Java packages storage location

I am new to Java and just downloaded Eclipse and wrote my first Hello World program

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {

        Scanner myObj = new Scanner(System.in);

        System.out.println("Whats your name?");

        String userName = myObj.nextLine();
        myObj.close();

        System.out.println("Hello " + userName);
    }

}

I was wondering what the storage location of the package java.util is? Where is this package stored on my computer? I use Mac.



Solution 1:[1]

Disclaimer

In java the libraries are .jar files, not packages. A package is just a folder used to groups the class. I mean package is not a file.

Javas code is compiled into .class files. One of that .class is Scanner.java. Usually the .class files are grouped in a jar file.

Core or default libraries

Any internal or core library is inside of JDK or JRE which was configured on your eclipse:

enter image description here

Depending of your configuration, usually you cannot see the source code of java.util.Scanner because it is compiled as .class and is inside of a .jar file. Some jdk or jre distribution has the source code :

enter image description here

On Linux, I found that class in the rt.jar located in ../jre/lib inside of the open jdk

enter image description here

Public libraries

This is the case when you are working with some library created by the community like: apache, spring, jackson, etc

In this case, maven download the library in the .m2 folder inside of the user folder of your os.

Java is open source

If you want to take a look in the source code, you will find it on github or google searching: "java.util.Scanner java"

References

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1