javaintellij-ideaclassloadergit-branchjava-17

Running Multiple Application Instances on Different Branches in IntelliJ - Potential Class Loading Issues?


I'm trying to understand how IntelliJ handles class loading when running multiple instances of a Java application on different branches.

When you run an application directly from the IDE (like IntelliJ), it doesn't create a Jar file but runs the built class files and resources directly, right?

Now, let's say I start my application on port 8080 from branch A in IntelliJ. Then, I checkout branch B and run the same application on port 8081.

When switching from branch A to branch B, the working directory changes according to the new branch. The class files in my working directory would now correspond to branch B. Since Java loads classes dynamically, it might not load all the classes initially but only when they are needed later. Could there be a situation where, when the application tries to load a class later, it fails because the class files from branch A are no longer available due to the checkout to branch B? I'm curious if this scenario could cause issues and how IntelliJ or Java handles such cases. Any insights would be appreciated!

How I ran two applications1

How I ran two applications2


Solution

  • As you mentioned, IntelliJ doesn't generate .jar files until you make it to do so. It just compiles the .java files into .class files, and then runs them. When you switch to another branch and compile the program once again, the .class files will be overwritten. Then, running Java program will probably raise ClassNotFoundException or, if you are lucky, just use the updated version of class.

    The solution that might be possible is to build the each version of solution you need to run explicitly into .jar files and launch them as normal .jar.