Method and device for solving same-name packet loading conflict and storage medium
By dynamically loading the same-name package into the parent class loader, ensuring that there is only one copy of the same-name class in the JVM, solving the problem of loading conflicts of the same-name package in Java application development and achieving stable operation of the application.
Patent Information
- Application Number
- CN202411774490.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-05
- Publication Date
- 2025-04-11
AI Technical Summary
In Java application development, loading conflicts of packages with the same name lead to class loading errors, which in turn leads to program crashes.
By obtaining the target jar package or target class name that needs to be conflicted, dynamically load it using running configuration, load it into the parent class loader, and ensure that the same name class has only one copy in the JVM through the parent delegation mechanism.
It effectively solves the problem of loading conflicts of packages with the same name, ensures the stable operation of Java applications, and reduces the frequency of class conflict exceptions.
Smart Images

Figure CN119806667A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of Java application development, and in particular to a method, device and storage medium for resolving conflicts in loading packages with the same name. Background Art
[0002] As an object-oriented programming language, Java is powerful and easy to use, and has been widely used. When choosing Java language to develop an application, the classes required for running in Java need to be loaded into the Java Virtual Machine (JVM) through the class loader before they can be executed. In the actual development process, when a Java application fails to load the correct class for some reason, resulting in the running results of the related program code being inconsistent with expectations, it is considered that a class conflict has occurred. Class conflict can also be simply defined as a class loading error caused by Java classes with the same name in the same namespace being scattered in different locations or carriers on the class loading path. Class conflict problems are generally caused by the JVM loading and using the class of the same name that the application does not want to use, resulting in a program error.
[0003] Among the technologies currently used, the detection of Java class conflicts is mainly static. Among them, SPI is a service discovery mechanism built into JDK, and its components can be expanded and replaced through the framework. Framework developers define standard interfaces through the SPI mechanism and provide the interfaces to third parties according to certain rules. When the third parties implement the interface and integrate it into the framework, the framework can load different third-party implementations of the same interface according to certain rules to achieve dynamic expansion and compatibility of the framework.
[0004] However, after using SPI extension to achieve pluggability, for the classes with the same name in the third-party custom packages in SPI and the packages introduced by the platform itself, their respective class loaders are used to load them separately, and an exception occurs in the JVM underlying class link, causing the program to crash and become unusable. Summary of the invention
[0005] To this end, the present invention proposes a method, device and storage medium for resolving loading conflicts of packages with the same name to solve the above technical problems.
[0006] To achieve the above object, in a first aspect, the present invention provides a method for resolving a loading conflict of packages with the same name, comprising:
[0007] Get the target jar package or target class name that needs to be resolved if there is a version conflict with the third party;
[0008] Dynamically load the target jar package or the target class name using the running state configuration, and load the target jar package or the target class name into the parent class loader;
[0009] The class with the same name defined in the parent class loader is loaded from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM.
[0010] Furthermore, when a class conflict exception occurs, the conflicting class with the same name is found by retrieving the exception error information, and the conflicting class with the same name is loaded into the parent class loader through dynamic configuration. All SPI class loaders related to the class conflict exception are uninstalled, and the third-party custom jar package in the SPI is reloaded.
[0011] Furthermore, when an SPI exception occurs, an alarm is issued through the log analysis center, and parameter configuration is sent in real time in the running state according to the alarm information to load the conflicting classes with the same name into the parent class loader.
[0012] Furthermore, the alarm through the log analysis center also includes: regularly scanning log records. If alarm keywords with a specified frequency or above are found within a certain period of time, an alarm ticket is generated and transferred to the developer through the operation and maintenance process, and timely online notification is given.
[0013] In a second aspect, the present invention also provides a device for resolving loading conflicts of packages with the same name, comprising: an acquisition module, a loading module, and an execution module;
[0014] The acquisition module is used to obtain the target jar package or target class name that needs to be resolved if there is a version conflict with the third party;
[0015] The loading module is used to dynamically load the target jar package or the target class name using the running state configuration, and load the target jar package or the target class name into the parent class loader;
[0016] The execution module is used to load the class with the same name defined in the parent class loader from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM.
[0017] Furthermore, when a class conflict exception occurs, the conflicting class with the same name is found by retrieving the exception error information, and the conflicting class with the same name is loaded into the parent class loader through dynamic configuration. All SPI class loaders related to the class conflict exception are uninstalled, and the third-party custom jar package in the SPI is reloaded.
[0018] Furthermore, when an SPI exception occurs, an alarm is issued through the log analysis center, and parameter configuration is sent in real time in the running state according to the alarm information to load the conflicting classes with the same name into the parent class loader.
[0019] Furthermore, the alarm through the log analysis center also includes: regularly scanning log records. If alarm keywords with a specified frequency or above are found within a certain period of time, an alarm ticket is generated and transferred to the developer through the operation and maintenance process, and timely online notification is given.
[0020] In a third aspect, the present invention provides a computing device, comprising:
[0021] A memory for storing program instructions;
[0022] The processor is used to call the program instructions stored in the memory, and execute the above-mentioned method for resolving the loading conflict of packages with the same name according to the obtained program instructions.
[0023] In a fourth aspect, the present invention provides a computer-readable storage medium, including computer-readable instructions. When a computer reads and executes the computer-readable instructions, the above-mentioned method for resolving conflicts in loading packages with the same name is implemented. The readable medium may be multiple, and the multiple readable media can run independently of each other.
[0024] Compared with the prior art, this solution innovatively obtains the target jar package or target class name that needs to be conflicted with the third party version; dynamically loads the target jar package or the target class name using the running state configuration, and loads the target jar package or the target class name into the parent class loader; loads the class with the same name defined in the parent class loader from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM. And it is loaded by the parent class loader, and the version is based on the version introduced in the platform; other non-same-name classes are isolated by loading with a custom class loader. At this time, after the SPI extension is implemented as pluggable, the third-party SDK is integrated, and there are no exceptions in the running state or the frequency of exceptions is reduced. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0026] Figure 1 A flow chart of an embodiment of a method for resolving conflicts in loading packages with the same name provided by the present invention;
[0027] Figure 2 The overall process of an embodiment of the method for solving the loading conflict of packages with the same name provided by the present invention
[0028] Figure 3The present invention provides a schematic structural diagram of an electronic device. DETAILED DESCRIPTION
[0029] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain the present application, and cannot be interpreted as limiting the present application.
[0030] It will be understood by those skilled in the art that, unless otherwise stated, the singular forms "a", "an" and "the" used herein may also include plural forms. It should be further understood that the term "comprising" used in the specification of the present application refers to the presence of features, integers, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof. The term "and / or" used herein includes all or any unit and all combinations of one or more associated listed items.
[0031] For the convenience of description, some nouns or terms involved in the embodiments of the present application are explained below:
[0032] JVM, the full name of Java Virtual Machine, is an abstract computer used to run Java programs. It does this by simulating various computer functions on an actual computer. It provides a virtual execution environment with cross-platform capabilities, memory management, security, and multi-threading support. As an important part of Java technology, it enables Java programs to run seamlessly on different operating systems and hardware platforms.
[0033] In order to make the objectives, technical solutions and advantages of the present application more clear, the specific implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.
[0034] The core of the present invention is to provide a method for resolving conflicts in loading packages with the same name. Figure 1 The flowchart of the method for resolving the conflict of loading packages with the same name provided by this embodiment is shown in FIG. Figure 1 The technical solution of this embodiment is described in detail:
[0035] Step S1: Obtain the target jar package or target class name that needs to be resolved if there is a version conflict with the third party;
[0036] This step involves scanning the project's dependencies to identify potentially conflicting jar packages or classes. This can be done by analyzing the project's build files (such as Maven's pom.xml or Gradle's build.gradle) or using a specialized dependency analysis tool.
[0037] Step S2: dynamically load the target jar package or the target class name using the running state configuration, and load the target jar package or the target class name into the parent class loader;
[0038] Specifically, Java provides class loaders such as URLClassLoader to implement dynamic loading. Through custom class loaders, specified jar packages or classes are loaded into JVM at runtime.
[0039] Step S3: Load the class with the same name defined in the parent class loader from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM.
[0040] Specifically, when a child class loader receives a class loading request, it first checks whether the class has been loaded; if not, it delegates the request to the parent class loader. This process continues until the top-level startup class loader. If the parent class loader can load the class, it returns the class; if not, the child class loader will try to load it itself.
[0041] In a preferred embodiment, when a class conflict exception occurs, the conflicting class with the same name is found by retrieving the exception error information, and the conflicting class with the same name is loaded into the parent class loader through dynamic configuration, and all SPI class loaders related to the class conflict exception are unloaded, and the third-party custom jar package in the SPI is reloaded.
[0042] In a preferred embodiment, when an SPI exception occurs, an alarm is generated through the log analysis center, and according to the alarm information, parameter configuration is sent down in real time in the running state to load the conflicting classes with the same name into the parent class loader.
[0043] In a preferred embodiment, the alarming through the log analysis center also includes: regularly scanning log records. If an alarm keyword with a specified frequency or above is found within a certain period of time, an alarm ticket is generated and transferred to the developer through the operation and maintenance process, and timely online notification is given.
[0044] In the scheme of this embodiment, the target jar package or target class name that needs to be conflicted with the third party version is innovatively obtained; the target jar package or the target class name is dynamically loaded using the running state configuration, and the target jar package or the target class name is loaded into the parent class loader; the class with the same name defined in the parent class loader is loaded from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM. And it is loaded by the parent class loader, and the version is based on the version introduced in the platform; other non-same-name classes are isolated by loading with a custom class loader. At this time, after the pluggable is realized through the SPI extension, the third-party SDK is integrated, and no exceptions occur in the running state or the frequency of exceptions is reduced.
[0045] In one embodiment, the present invention provides a device for resolving conflicts in loading packages with the same name, see Figure 2 The structure block diagram of a device for resolving conflicts in loading packages with the same name provided by this embodiment is shown, and the device comprises: an acquisition module 10, a loading module 20, and an execution module 30;
[0046] The acquisition module 10 is used to acquire the target jar package or target class name that needs to be resolved if there is a version conflict with the third party;
[0047] The loading module 20 is used to dynamically load the target jar package or the target class name by using the running state configuration, and load the target jar package or the target class name into the parent class loader;
[0048] The execution module 30 is used to load the class with the same name defined in the parent class loader from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM.
[0049] In a preferred embodiment, when a class conflict exception occurs, the conflicting class with the same name is found by retrieving the exception error information, and the conflicting class with the same name is loaded into the parent class loader through dynamic configuration, and all SPI class loaders related to the class conflict exception are unloaded, and the third-party custom jar package in the SPI is reloaded.
[0050] In a preferred embodiment, when an SPI exception occurs, an alarm is generated through the log analysis center, and according to the alarm information, parameter configuration is sent down in real time in the running state to load the conflicting classes with the same name into the parent class loader.
[0051] In a preferred embodiment, the alarming through the log analysis center also includes: regularly scanning log records. If an alarm keyword with a specified frequency or above is found within a certain period of time, an alarm ticket is generated and transferred to the developer through the operation and maintenance process, and timely online notification is given.
[0052] In the scheme of this embodiment, the target jar package or target class name that needs to be conflicted with the third party version is innovatively obtained; the target jar package or the target class name is dynamically loaded using the running state configuration, and the target jar package or the target class name is loaded into the parent class loader; the class with the same name defined in the parent class loader is loaded from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM. And it is loaded by the parent class loader, and the version is based on the version introduced in the platform; other non-same-name classes are isolated by loading with a custom class loader. At this time, after the pluggable is realized through the SPI extension, the third-party SDK is integrated, and no exceptions occur in the running state or the frequency of exceptions is reduced.
[0053] The device for resolving conflicts in loading packages with the same name in this embodiment is used to implement the aforementioned method for resolving conflicts in loading packages with the same name. Therefore, the specific implementation method of the device for resolving conflicts in loading packages with the same name can be seen from the specific implementation method of the method for resolving conflicts in loading packages with the same name in the aforementioned text. For example, the acquisition module 10 is used to implement step S1 in the aforementioned method for resolving conflicts in loading packages with the same name, the loading module 20 is used to implement step S2 in the aforementioned method for resolving conflicts in loading packages with the same name, and the execution module 30 is used to implement step S3 in the aforementioned method for resolving conflicts in loading packages with the same name, etc. Therefore, its specific implementation method can refer to the description of the corresponding embodiments of each part. The device provided by the present invention is attached to the method provided by the present invention, and its specific effects can refer to the above method, which will not be repeated here.
[0054] In one embodiment, a computing device is provided, comprising:
[0055] A memory for storing program instructions;
[0056] A processor is used to implement the steps of any of the above methods for resolving conflicts in loading packages with the same name when executing the computer program.
[0057] As an example, Figure 3 A schematic diagram of the structure of an electronic device applicable to the embodiment of the present application is shown. Figure 3 As shown, the electronic device 2000 includes: a processor 2001 and a memory 2003. The processor 2001 and the memory 2003 are connected, such as through a bus 2002. Optionally, the electronic device 2000 may also include a transceiver 2004. It should be noted that in actual applications, the transceiver 2004 is not limited to one, and the structure of the electronic device 2000 does not constitute a limitation on the embodiments of the present application.
[0058] The processor 2001 is applied in the embodiment of the present application to implement the method shown in the above method embodiment. The transceiver 2004 may include a receiver and a transmitter. The transceiver 2004 is applied in the embodiment of the present application to implement the function of the electronic device of the embodiment of the present application communicating with other devices when executed.
[0059] Processor 2001 may be a CPU (Central Processing Unit), a general purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. Processor 2001 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.
[0060] The bus 2002 may include a path to transmit information between the above components. The bus 2002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus 2002 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0061] The memory 2003 can be a ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, a RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compressed optical disk, laser disk, optical disk, digital versatile disk, Blu-ray disk, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited to these.
[0062] Optionally, the memory 2003 is used to store application code for executing the solution of the present application, and the execution is controlled by the processor 2001. The processor 2001 is used to execute the application code stored in the memory 2003 to implement the investment and financing project recommendation method provided in any embodiment of the present application.
[0063] The electronic device provided in the embodiment of the present application is applicable to any embodiment of the above method and will not be described in detail here.
[0064] The method for solving the conflict of loading packages with the same name provided by the present invention has obvious advantages and beneficial effects compared with the prior art. It innovatively uses the particle swarm optimization support vector machine method and applies it in the field of commercial bank credit risk prediction. Compared with the manual evaluation method based on experience, a more intelligent support vector machine method is used to achieve risk assessment. At the same time, it is proposed to use the particle swarm algorithm to optimize the support vector machine kernel function parameters. Compared with the traditional support vector machine, it has a better evaluation effect. The SVM model based on PSO optimization will have better performance in the evaluation model of personal credit risk prediction. This embodiment can obtain the basic credit data of commercial bank customers and use scientific and effective prediction methods to more effectively predict credit risks and reduce bank bad debts caused by credit problems.
[0065] In one embodiment, a computer-readable storage medium is provided, including computer-readable instructions. When a computer reads and executes the computer-readable instructions, the steps of the method for solving the conflict of loading packages with the same name described above are implemented. The method for solving the conflict of loading packages with the same name provided by the present invention has obvious advantages and beneficial effects compared with the prior art. The method is innovatively used to optimize the support vector machine method by particle swarm optimization, and is applied in the field of commercial bank credit risk prediction. Compared with the manual evaluation method based on experience, a more intelligent support vector machine method is used to achieve risk assessment. At the same time, it is proposed to use a particle swarm algorithm to optimize the parameters of the support vector machine kernel function. Compared with the traditional support vector machine, it has a better evaluation effect. The SVM model based on PSO optimization will have better performance in the evaluation model of personal credit risk prediction. This embodiment can obtain the basic credit data of commercial bank customers and use scientific and effective prediction methods to more effectively predict credit risks and reduce bank bad debts caused by credit problems.
[0066] Through the description of the above implementation methods, technical personnel in the relevant field can understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the specific device can be divided into different functional modules to complete all or part of the functions described above.
[0067] Those of ordinary skill in the art will appreciate that the modules and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0068] In several embodiments covered by the present application, it should be understood that the disclosed devices, equipment and methods can be implemented in other ways. For example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another device, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or modules, or it can be an electrical, mechanical or other form of connection.
[0069] The modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place or distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the embodiments of the present application.
[0070] In addition, each functional module in each embodiment of the present application can be implemented in the form of hardware or in the form of software functional modules. If these functional modules are implemented in the form of software functional modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present application is essentially or part of the prior art that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a computer program product, which includes one or more computer instructions. When loading and executing computer program instructions on a computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer program product is stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, a computer instruction can be transmitted from a website site, a computer, a server or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or a data center that includes one or more available media integrations. The available media may be magnetic media (eg, floppy disks, hard disks, magnetic tapes), optical media (eg, DVDs), or semiconductor media (eg, solid state disks (SSDs)).
[0071] The above contents are merely examples and explanations of the structure of the present invention. The technicians in this technical field may make various modifications or additions to the specific embodiments described or replace them in a similar manner. As long as they do not deviate from the structure of the invention or exceed the scope defined by the claims, they should all fall within the protection scope of the present invention.
Claims
1. A method for resolving conflicts in loading packages with the same name, characterized in that: include: Get the target jar package or target class name that needs to be resolved if there is a version conflict with the third party; Dynamically load the target jar package or the target class name using the running state configuration, and load the target jar package or the target class name into the parent class loader; The class with the same name defined in the parent class loader is loaded from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM.
2. The method for resolving loading conflicts of packages with the same name according to claim 1, characterized in that: When a class conflict exception occurs, the conflicting class with the same name is found by retrieving the exception error information, and the conflicting class with the same name is loaded into the parent class loader through dynamic configuration. All SPI class loaders related to the class conflict exception are uninstalled, and the third-party custom jar package in the SPI is reloaded.
3. The method for resolving loading conflicts of packages with the same name according to claim 2, characterized in that: When an SPI exception occurs, an alarm is issued through the log analysis center, and according to the alarm information, parameter configuration is sent down in real time in the running state to load the conflicting classes with the same name into the parent class loader.
4. The method for resolving loading conflicts of packages with the same name according to claim 3, characterized in that: The alarming through the log analysis center also includes: regularly scanning log records. If an alarm keyword with a specified frequency or above is found within a certain period of time, an alarm ticket is generated and transferred to the developer through the operation and maintenance process, and timely online notification is given.
5. A device for resolving loading conflicts of packages with the same name, characterized in that: include: Get module, load module, execute module; The acquisition module is used to obtain the target jar package or target class name that needs to be resolved if there is a version conflict with the third party; The loading module is used to dynamically load the target jar package or the target class name using the running state configuration, and load the target jar package or the target class name into the parent class loader; The execution module is used to load the class with the same name defined in the parent class loader from the parent class loader through the parent delegation mechanism, so that there is only one copy of the class with the same name in the JVM.
6. The device for resolving loading conflicts of packages with the same name according to claim 5, characterized in that: When a class conflict exception occurs, the conflicting class with the same name is found by retrieving the exception error information, and the conflicting class with the same name is loaded into the parent class loader through dynamic configuration. All SPI class loaders related to the class conflict exception are uninstalled, and the third-party custom jar package in the SPI is reloaded.
7. The device for resolving loading conflicts of packages with the same name according to claim 6, characterized in that: When an SPI exception occurs, an alarm is issued through the log analysis center, and according to the alarm information, parameter configuration is sent down in real time in the running state to load the conflicting classes with the same name into the parent class loader.
8. The device for resolving loading conflicts of packages with the same name according to claim 7, characterized in that: The alarming through the log analysis center also includes: regularly scanning log records. If an alarm keyword with a specified frequency or above is found within a certain period of time, an alarm ticket is generated and transferred to the developer through the operation and maintenance process, and timely online notification is given.
9. A computing device, characterized in that include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory, and execute the method according to any one of claims 1 to 4 according to the obtained program instructions.
10. A computer-readable storage medium, characterized in that: It includes computer-readable instructions. When a computer reads and executes the computer-readable instructions, it implements the method as claimed in any one of claims 1 to 4. There can be multiple readable media, and the multiple readable media can operate independently of each other.