I have a web application and two class files,
我有一个Web应用程序和两个类文件,
First class is MyClass.class which is inside the abc.jar file (WEB-INF/lib/abc.jar) and Second class is YourClass.class which is inside classes folder (WEB-INF/classes/ YourClass.class).
第一类是MyClass.class,它位于abc.jar文件(WEB-INF / lib / abc.jar)中,第二类是YourClass.class,它位于classes文件夹(WEB-INF / classes / YourClass.class)中。
My question is which class would load first when the Application starts? And Why ?
我的问题是,当应用程序启动时,哪个类会首先加载?为什么?
4 个解决方案
#1
In my experience you can't predict the order in which the classes are loaded by the JVM.
根据我的经验,您无法预测JVM加载类的顺序。
Once I made a test runner (kinda Maven's Surefire) and with the same JVM and OS it loaded classes in different order when run in different machines. The lesson learnt:
一旦我做了一个测试运行器(有点Maven的Surefire)并且使用相同的JVM和OS,它在不同的机器上运行时以不同的顺序加载类。经验教训:
You shouldn't build your applications to depend on class loading order
您不应该构建应用程序以依赖类加载顺序
#2
Classes are loaded as needed, for some definition of "needed". Exactly when a class is loaded is dependent upon the JRE implementation, javac implementation, exactly what thread are up to, server code and, of course, application code. It's a bad idea to make assumptions in this area. If you want to see what happens for a particular run, you can use -verbose:class
对于“需要”的某些定义,根据需要加载类。正确地加载类时,依赖于JRE实现,javac实现,究竟是什么线程,服务器代码,当然还有应用程序代码。在这个领域做出假设是一个坏主意。如果要查看特定运行的结果,可以使用-verbose:class
#3
Sun's class loader docs always say WEB-INF/classes OR WEB-INF/lib, but doesn't say which one will be checked first.
Sun的类加载器文档总是说WEB-INF / classes OR WEB-INF / lib,但是没有说明首先检查哪一个。
From IBM docs:
来自IBM docs:
"The rules for loading classes are spelled out in detail in the JVM specification. The basic principle is that classes are only loaded when needed (or at least appear to be loaded this way -- the JVM has some flexibility in the actual loading, but must maintain a fixed sequence of class initialization). Each class that gets loaded may have other classes that it depends on, so the loading process is recursive."
“在JVM规范中详细说明了加载类的规则。基本原则是类只在需要时加载(或者至少看起来像这样加载 - JVM在实际加载方面有一定的灵活性,但是必须维护一个固定的类初始化序列。)每个被加载的类可能都有其它依赖的类,因此加载过程是递归的。“
So I think the answer is: It depends on which classes is needed in your application first.
所以我认为答案是:它取决于您的应用程序首先需要哪些类。
#4
As duffymo points out, this can vary. One way you might ascertain the sequence for this specific app is to insert Response.Write text in the class constructors and web web app page loading methods. "Instantiated object in Class A", "Opened web page MyPage", and so on.
正如duffymo所指出的,这可能会有所不同。您可以确定此特定应用程序的顺序的一种方法是在类构造函数和Web Web应用程序页面加载方法中插入Response.Write文本。 “A类中的实例化对象”,“打开的网页MyPage”等。
Once you've figured out the sequence, comment out the code for those so you can reuse them later to verify that you haven't made a change (such as instantiating an object earlier or later) that affected the sequence.
一旦你弄明白了这个序列,就要注释掉那些代码,这样你以后就可以重用它们来验证你是否没有对序列进行更改(例如更早或更晚地实例化对象)。
#1
In my experience you can't predict the order in which the classes are loaded by the JVM.
根据我的经验,您无法预测JVM加载类的顺序。
Once I made a test runner (kinda Maven's Surefire) and with the same JVM and OS it loaded classes in different order when run in different machines. The lesson learnt:
一旦我做了一个测试运行器(有点Maven的Surefire)并且使用相同的JVM和OS,它在不同的机器上运行时以不同的顺序加载类。经验教训:
You shouldn't build your applications to depend on class loading order
您不应该构建应用程序以依赖类加载顺序
#2
Classes are loaded as needed, for some definition of "needed". Exactly when a class is loaded is dependent upon the JRE implementation, javac implementation, exactly what thread are up to, server code and, of course, application code. It's a bad idea to make assumptions in this area. If you want to see what happens for a particular run, you can use -verbose:class
对于“需要”的某些定义,根据需要加载类。正确地加载类时,依赖于JRE实现,javac实现,究竟是什么线程,服务器代码,当然还有应用程序代码。在这个领域做出假设是一个坏主意。如果要查看特定运行的结果,可以使用-verbose:class
#3
Sun's class loader docs always say WEB-INF/classes OR WEB-INF/lib, but doesn't say which one will be checked first.
Sun的类加载器文档总是说WEB-INF / classes OR WEB-INF / lib,但是没有说明首先检查哪一个。
From IBM docs:
来自IBM docs:
"The rules for loading classes are spelled out in detail in the JVM specification. The basic principle is that classes are only loaded when needed (or at least appear to be loaded this way -- the JVM has some flexibility in the actual loading, but must maintain a fixed sequence of class initialization). Each class that gets loaded may have other classes that it depends on, so the loading process is recursive."
“在JVM规范中详细说明了加载类的规则。基本原则是类只在需要时加载(或者至少看起来像这样加载 - JVM在实际加载方面有一定的灵活性,但是必须维护一个固定的类初始化序列。)每个被加载的类可能都有其它依赖的类,因此加载过程是递归的。“
So I think the answer is: It depends on which classes is needed in your application first.
所以我认为答案是:它取决于您的应用程序首先需要哪些类。
#4
As duffymo points out, this can vary. One way you might ascertain the sequence for this specific app is to insert Response.Write text in the class constructors and web web app page loading methods. "Instantiated object in Class A", "Opened web page MyPage", and so on.
正如duffymo所指出的,这可能会有所不同。您可以确定此特定应用程序的顺序的一种方法是在类构造函数和Web Web应用程序页面加载方法中插入Response.Write文本。 “A类中的实例化对象”,“打开的网页MyPage”等。
Once you've figured out the sequence, comment out the code for those so you can reuse them later to verify that you haven't made a change (such as instantiating an object earlier or later) that affected the sequence.
一旦你弄明白了这个序列,就要注释掉那些代码,这样你以后就可以重用它们来验证你是否没有对序列进行更改(例如更早或更晚地实例化对象)。