0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
phili0881
  • 103
  • 2

1 Answers1

0

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

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • The source code can also be viewed if one presses `ctrl` and then clicks on the class name. – MC Emperor Apr 13 '22 at 16:22
  • *Where is this package storaged on my computer? I use Mac.* This would depend on your version of Java. In module-based JDKs/JREs it would be in java.base.jmod. In my case that's /usr/lib/jvm/bellsoft-java16-amd64/jmods/java.base.jmod – g00se Apr 13 '22 at 17:04
  • In java the libraries are .jar files which contains .class files. One of that .class is Scanner.java – JRichardsz Apr 13 '22 at 20:57