Is there any way to force static initialization of some class B
before entering the main()
method of class A
, without changing class A
, using only VM options?
Invoke java class static initialization via VM option
133 Views Asked by FireFry At
2
There are 2 best solutions below
1

I'm not aware of any way to do it without code. In code it's easy of course.
public class C {
static {
// Fetch and throw away an instance of B to make sure it is loaded
B.getInstance();
}
static void main(String[] args) {
// Do stuff, B is initialized
A.main(args);
}
}
In fact you could just do
public class C {
static void main(String[] args) {
B.getInstance();
A.main(args);
}
}
Your request doesn't make a lot of sense though. Ask a question about the problem you are trying to solve by doing this and you will hopefully get a much more useful answer.
you could create a class that initializes other classes and then calls the real main method, e.g:
Then you would start the application with that class as the main class and specify the real main class and the classes to be initialized via properties.