1

I created a java agent class, that just uses java.lang.Instrumentation.getAllLoadedClasses() to retrieve an array of classes, over which I iterate, and print their full names.

I used this command to run this agent:

java -javaagent:<agent_jar> -jar <sample_jar>

<agent_jar> contains my agent class, and <sample_jar> just contains a class with an empty main method (since I have to pass some class/jar to the java command)

So, basically, it printed all the default classes loaded by the JVM, and here's a snippet for class names containing 'String' and 'Object' in their names:

(Output format: Class.getName() - Class.getTypeName())

java.util.Objects - java.util.Objects
[Ljava.lang.String; - java.lang.String[]
java.lang.String$CaseInsensitiveComparator - java.lang.String$CaseInsensitiveComparator
[Ljava.io.ObjectStreamField; - java.io.ObjectStreamField[]
java.io.ObjectStreamField - java.io.ObjectStreamField
[Ljava.lang.Object; - java.lang.Object[]
java.lang.StringBuilder - java.lang.StringBuilder
java.lang.StringBuffer - java.lang.StringBuffer
java.lang.AbstractStringBuilder - java.lang.AbstractStringBuilder
java.lang.reflect.AccessibleObject - java.lang.reflect.AccessibleObject
java.lang.String - java.lang.String
java.lang.Object - java.lang.Object

Why are there array entries for many classes, eg. Object and Object[]?

ArM
  • 307
  • 2
  • 9
  • Reference http://stackoverflow.com/questions/3442090/java-what-is-this-ljava-lang-object – Guy Dec 15 '16 at 08:43
  • Just because your `main` is empty, it doesn't mean that the JVM does *nothing*. It obviously needs to load some stuff to be able to load your class and to run your main method. – Tom Dec 15 '16 at 08:45

3 Answers3

0

It has to be loaded because every class in Java is extends Object class or we can say Object is a superclass of every class in Java.

So when you print out the loaded classes, it will show the Object class.

From Oracle Docs:

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object.

From here:

Class Loaders

In a JVM, each and every class is loaded by some instance of a java.lang.ClassLoader. The ClassLoader class is located in the java.lang package and developers are free to subclass it to add their own functionality to class loading.

Whenever a new JVM is started by typing java MyMainClass, the "bootstrap class loader" is responsible for loading key Java classes like java.lang.Object and other runtime code into memory first. The runtime classes are packaged inside of the JRE\lib\rt.jar file. We cannot find the details of the bootstrap class loader in the Java documentation, since this is a native implementation. For the same reason, the behavior of the bootstrap class loader will also differ across JVMs.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
0

Why are there array entries for many classes, eg. Object and Object[]?

It is just classes that you application, and/or the JVM's default application launcher code reference.

The Object class will be loaded because other other classes inherit from it. The Object[] array type is most likely used when the launcher uses reflection to find the Method object for the main(String[]) method.

There is a bunch of "black magic" happens behinds the scenes when a JVM bootstraps itself. If you really need to know, then the source code is available ....

(It is also possible that your agent's classes and dependencies will show up in the list, but they may also be loaded by a distinct classloader.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

If you call main(String[] args) you'll need Object (because everything extends Object), String[] and String already.

Now take a look at the imports of String, you'll recognize a few:

import java.io.ObjectStreamField;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Formatter;
import java.util.Locale;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Classes are not loaded right away just because they are imported. But when you call the main method, the JVM does some processing before, e.g. the parsing of parameters and writing them to the args array. It's not hard to imagine that some of the classes you listed are needed for that.

André Stannek
  • 7,773
  • 31
  • 52