Several applications on tomcat

If I have two java applications running on tomcat. Application A and B. I have an open class with a static variable in application A, then application B can access it. If not, why?

I was asked in an interview. I said I can’t access. But I did not know why?

Can anyone help with the correct answer.

+4
source share
5 answers

Because each application has its own class loader, although the JVM is the same. For more information on the class loader, see What is Java ClassLoader?

+4
source

Each application war has a separate class loader, so no, they cannot access other methods.

However, this can be done using something like RMI or the HTTP web service.

+1
source

Because each WebApp allocates its own class loaders (Tomcat system).

Your exact question about static :

The key is structured by ClassLoaders. It is possible for two ClassLoaders within the same JVM for each class load, and therefore contains separate independent copies of static fields.

Prefer to read: Classloader Behavior on Tomcat with Multiple Applications

0
source

Let me put it in the easiest way.

Qusetion: How to access a static variable? Answer: The class name is definitely used.

We have a class as shown below:

 package com.package1; public ClassA { public static String GLOBAL_VARIABLE= "TEST"; } 

Question:. How the JVM sees ClassA. Answer: He sees how ClassLoaderForA.com.package1.ClassA

Assuming this was loaded using the Application1 class loader

Now for the second class loader, the ClassA application ClassA does not exist. He will not find it. So how can it get a static field from ClassA

0
source

It depends. The question is not clear enough to give a yes / no answer in my opinion.

If an application somehow expands this variable, then naturally other applications may just ask for a value. But this is unlikely what the question should have been raised. But keep that in mind. In the interview, I was always pleased to prove that you can think critically and out of the box.

I suppose they wanted to see if you understood the concept of static variables and ClassLoader s. A Java application is dynamically linked, which means that at runtime a ClassLoader (which depends on the application) will link, load all libraries, or delegate loading. Static variables are called a class. If you try to access a static variable, the class' ClassLoader will try to find and load the class. Since each application has its own ClassLoader , each application has its own set of variables.

This is not like the principle of dividing the address space found in older languages. The application works in a separate address space, even if it is located on one computer. Therefore, two applications running side by side cannot simply access variables in another address space of the application, as this violates the restriction that is applied by several watchdogs. But be careful, the address space can be split explicitly.

-2
source

Source: https://habr.com/ru/post/1502034/


All Articles