Java code hot update method and device and Java code development system

By monitoring and dynamically loading changes in source code files in Java application projects, hot updates of Java code are achieved, solving the application server restart problem caused by frequent code modifications, and improving development efficiency and user experience.

CN119690488APending Publication Date: 2025-03-25INSPUR SUZHOU INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411749040.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-30
Publication Date
2025-03-25

AI Technical Summary

Technical Problem

During Java software development, frequent code modifications lead to frequent restarts of application servers, affecting user experience and potential problems such as memory leaks and disconnection.

Method used

By monitoring the changes in the source code file in the specified directory in the Java application project, dynamically loading and replacing the corresponding class files, the Java code is hot updated and avoiding restarting the application server.

Benefits of technology

It realizes hot updates of Java code, improves development efficiency, reduces development costs, enhances user experience, and avoids potential problems caused by frequent restarts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119690488A_ABST
    Figure CN119690488A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a Java code hot update method and device and a Java code development system.The method comprises the steps that monitoring is conducted, specifically, whether a source code file under a specified directory in a Java application project changes or not is monitored in real time, and the specified directory is a directory for storing the source code file; in the updating step, the changed source code file is determined as a target source code file, the target source code file is compiled to obtain a new class file, the new class file is dynamically loaded to replace a class file corresponding to the target source code file, and the class file is a byte code file generated by compiling the source code file before change; and a calling step: creating the object corresponding to the new class file to call the method of the object corresponding to the new class file, so that hot update of the source code file is realized, and the problem that the user experience is influenced by frequent restart of Java code modification is solved. And frequent restarting is not needed, a large amount of time is saved, and the effect of improving the development efficiency is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer software technology, and in particular, to a Java code hot update method, device, computer-readable storage medium, and Java code development system. Background Art

[0002] In the process of Java software development, as the project is constantly iterating and the functions are constantly improving, developers often need to modify and optimize the code. The traditional approach is to manually restart the application server after the code modification is completed to make the modification take effect. However, this method has many shortcomings: first, it is inefficient, and frequent restarts of the application server will waste a lot of time; second, it affects the user experience, especially in the production environment, where the restart operation may cause service interruption; third, it may cause potential problems, such as memory leaks, disconnections, etc. Therefore, how to implement code updates without restarting the application server has become a technical problem that needs to be solved in the field of Java software development.

[0003] With the development of Java virtual machine (JVM) and class loading mechanism, some technologies such as OSGi (Open Service Gateway initiative) and HotSwap have been able to achieve a certain degree of code hot update. However, these technologies usually have problems such as complex configuration and limited scope of application, and are difficult to promote and use in a wide range of Java application projects. Therefore, it is of great significance to develop a simple, efficient and universal Java code hot update method. Summary of the invention

[0004] The embodiments of the present application provide a Java code hot update method, device, computer-readable storage medium and Java code development system to at least solve the problem in the related art that frequent Java code modification and restart affect user experience.

[0005] According to an embodiment of the present application, a hot update method for Java code is provided, comprising: a monitoring step, monitoring in real time whether a source code file in a specified directory in a Java application project changes, the specified directory being a directory storing the source code file; an updating step, determining the changed source code file as a target source code file, compiling the target source code file to obtain a new class file, dynamically loading the new class file to replace the class file corresponding to the target source code file, the class file being a bytecode file generated by compiling the source code file before the change; and a calling step, creating an object corresponding to the new class file to call a method of the object corresponding to the new class file to implement hot update of the source code file.

[0006] In an exemplary embodiment, real-time monitoring of whether source code files in a specified directory in a Java application project have changed includes: creating a file listener, which is Java's WatchService API or a third-party library; using the file listener to monitor in real time whether the source code files in the specified directory in the Java application project have changed.

[0007] In an exemplary embodiment, the file listener is used to monitor in real time whether the source code files in the specified directory of the Java application project have changed, including: generating a monitoring instruction when a scheduled monitoring task or a manual monitoring task is triggered, the scheduled monitoring task is a task triggered once at a predetermined interval, and the manual monitoring task is a task triggered in response to a predetermined operation; in response to the monitoring instruction, the file listener is used to monitor whether the source code files in the specified directory of the Java application project have changed.

[0008] In an exemplary embodiment, the changed source code file is determined as a target source code file, and the target source code file is compiled to obtain a new class file, and the new class file is dynamically loaded to replace the class file corresponding to the target source code file, including: determining the changed source code file as a target source code file, and obtaining the file path of the target source code file; locating the class file corresponding to the target source code file according to the file path of the target source code file to obtain the path of the class file; compiling the target source code file to obtain a new class file; creating a custom class loader, the custom class loader having a dynamic class loading function; using the custom class loader to load the new class file to replace the class file corresponding to the target source code file according to the path of the class file.

[0009] In an exemplary embodiment, an object corresponding to the new class file is created to call a method of the object corresponding to the new class file to implement hot update of the source code file, including: using a reflection mechanism to dynamically create an object corresponding to the loaded new class file to call a method of the object corresponding to the new class file to implement hot update of the source code file.

[0010] In an exemplary embodiment, after creating an object corresponding to the new class file to call the method of the object corresponding to the new class file to realize hot update of the source code file, the method also includes: integrating a file listener and a custom class loader into the Java application project, the custom class loader having a dynamic class loading function; initializing the file listener and the custom class loader during the startup process of the Java application project; modifying the class loading policy of the Java application project so that the custom class loader is used preferentially when loading the class file; modifying the source code file; calling the Java application project to execute the monitoring step, the updating step and the calling step in sequence; calling the test interface or page of the Java application project to output the output result of calling the method of the object; and determining that the modification of the source code file is effective when all the methods are called successfully in the output result.

[0011] In an exemplary embodiment, after calling the test interface or page of the Java application project to output the output result of calling the method of the object, the method further includes: if all the methods fail to be called in the output result, locating the source code file corresponding to the method that failed to be called, and issuing an error reminder to remind checking whether the corresponding source code file is modified incorrectly.

[0012] According to another embodiment of the present application, a hot update device for Java code is provided, comprising: a monitoring module, used to execute a monitoring step, and monitor in real time whether a source code file in a specified directory in a Java application project changes, wherein the specified directory is a directory where the source code file is stored; an update module, used to execute an update step, and determine the changed source code file as a target source code file, and compile the target source code file to obtain a new class file, so that the new class file replaces the class file corresponding to the target source code file, and the class file is a bytecode file generated by compiling the source code file; a first calling module, used to execute a calling step, and load the new class file, and create an object corresponding to the new class file to call a method of the object corresponding to the new class file, so as to realize hot update of the source code file.

[0013] According to another embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when run.

[0014] According to another embodiment of the present application, a Java code development system is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.

[0015] Through this application, the Java code hot update method monitors whether the source code file changes, that is, the source code file that has changed is determined as the target source code file, that is, the modified source code file, and the target source code file is compiled to obtain a new class file, so that the new class file replaces the class file corresponding to the target source code file, thereby dynamically loading the new class file, and creating an object corresponding to the new class file to call the method of the object corresponding to the new class file. Since the new class file is loaded dynamically, there is no need to restart to load the new class file, and the hot update of the source code file can be achieved. Therefore, the problem of frequent restart of Java code modification affecting user experience in the related technology can be solved. The lack of frequent restarts also saves a lot of time, achieving the effect of improving development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] Figure 1 A hardware structure block diagram of a mobile terminal for executing a hot update method of Java code provided in an embodiment of the present application is shown;

[0017] Figure 2 is a flowchart of a Java code hot update method according to an embodiment of the present application;

[0018] Figure 3 is a flowchart of another Java code hot update method according to an embodiment of the present application;

[0019] Figure 4 It is a structural block diagram of a Java code hot update device according to an embodiment of the present application. DETAILED DESCRIPTION

[0020] The embodiments of the present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0021] It should be noted that the terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.

[0022] The method embodiments provided in the embodiments of the present application can be executed in a server device or a similar computing device. Taking running on a server device as an example, Figure 1 1 is a hardware structure block diagram of a server device of a Java code hot update method according to an embodiment of the present application. Figure 1 As shown, the server device may include one or more ( Figure 1Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the server device may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above server device. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.

[0023] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the hot update method of the Java code in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, to implement the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the server device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0024] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of the server device. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0025] In this embodiment, a hot update of Java code is provided. Figure 2 is a flowchart of hot update of Java code according to an embodiment of the present application, such as Figure 2 As shown, the process includes the following steps:

[0026] Step S202, a monitoring step, real-time monitoring of whether source code files in a specified directory of a Java application project change, the specified directory being a directory storing the source code files;

[0027] In a specific application, the file monitoring function is used to monitor in real time whether the source code file has changed, so that the modified source code file can be found.

[0028] Step S204, an updating step, determining the changed source code file as a target source code file, compiling the target source code file to obtain a new class file, dynamically loading the new class file to replace the class file corresponding to the target source code file, the class file being a bytecode file generated by compiling the source code file before the change;

[0029] In a specific application, the changed source code file is found and determined as the target source code file, that is, the modified source code. The changed source code file is compiled using a Java compiler (such as javac) to generate a new class file (.class). The compiled new class file is loaded and replaces the original class.

[0030] Step S206, a calling step, creates an object corresponding to the new class file to call a method of the object corresponding to the new class file to implement hot update of the source code file.

[0031] In specific applications, when a new class file is loaded, we can use the reflection mechanism to dynamically create objects and call corresponding methods to implement hot update function.

[0032] Through the above steps, the Java code hot update method monitors whether the source code file changes, that is, the source code file that has changed is determined as the target source code file, that is, the modified source code file, and the target source code file is compiled to obtain a new class file, so that the new class file replaces the class file corresponding to the target source code file, thereby dynamically loading the new class file, and creating an object corresponding to the new class file to call the method of the object corresponding to the new class file. Since the new class file is loaded dynamically, there is no need to restart to load the new class file, and the hot update of the source code file can be realized. Therefore, it can solve the problem of frequent restart of Java code modification affecting user experience in related technologies. The lack of frequent restarts also saves a lot of time, achieving the effect of improving development efficiency.

[0033] The execution subject of the above steps may be a server, a terminal, etc., but is not limited thereto.

[0034] In summary, the hot update method of the above Java code has the following effects:

[0035] Improve development efficiency: Developers can see the effects of code modifications in real time without having to manually restart the application server, which greatly improves development efficiency.

[0036] Reduce development costs: Reduce the time and resource consumption caused by frequent restarts of application servers.

[0037] Enhanced user experience: During the development process, developers can respond to changes in requirements and fix problems more quickly, thereby improving the quality of the software and the user experience.

[0038] Flexibility and scalability: This approach does not rely on a specific framework or library and can be easily integrated into various Java application projects and expanded and optimized as needed.

[0039] In an exemplary embodiment, the above step S202 can be implemented by the following steps: step S2021, create a file listener, and the above file listener is Java's WatchService APl or a third-party library; step S2022, use the above file listener to monitor in real time whether the above source code file in the specified directory of the above Java application project changes.

[0040] In the above embodiments, different file listeners can implement file monitoring. For example, Java's java.nio.file.WatchService API or a third-party library (such as Apache Commons IO's FileAlterationObserver) is used to implement the file monitoring function, so as to timely discover the modified source code files for hot update.

[0041] In an exemplary embodiment, the above-mentioned step S2024 can be implemented through the following steps: Step S20241, when a scheduled monitoring task or a manual monitoring task is triggered, a monitoring instruction is generated, the above-mentioned scheduled monitoring task is a task triggered once at a predetermined interval, and the above-mentioned manual monitoring task is a task triggered in response to a predetermined operation; Step S20242, in response to the above-mentioned monitoring instruction, the above-mentioned file listener is used to monitor whether the above-mentioned source code file in the specified directory of the above-mentioned Java application project has changed.

[0042] In the above embodiment, a timed monitoring task is set to trigger the generation of a monitoring instruction once at a predetermined time interval, so that in response to the above monitoring instruction, the above file listener is used to monitor whether the above source code file in the specified directory of the above Java application project has changed, without the need for real-time monitoring, thus saving resources. In addition, a manual monitoring task can also be set, which will trigger the generation of the monitoring instruction only after the developer performs a predetermined operation, so that the developer can modify the source code and then perform a predetermined operation to trigger the generation of the monitoring instruction, so as to timely discover the modified source code file, thereby improving the flexibility and usage experience of file monitoring.

[0043] In an exemplary embodiment, the above-mentioned step S204 can be implemented by the following steps: step S2041, determining the above-mentioned source code file that has changed as the target source code file, and obtaining the file path of the above-mentioned target source code file; step S2042, locating the above-mentioned class file corresponding to the above-mentioned target source code file according to the file path of the above-mentioned target source code file, and obtaining the path of the above-mentioned class file; step S2043, compiling the above-mentioned target source code file to obtain a new class file; step S2044, creating a custom class loader, and the above-mentioned custom class loader has a dynamic class loading function; step S2045, using the above-mentioned custom class loader to load the above-mentioned new class file to replace the above-mentioned class file corresponding to the above-mentioned target source code file according to the path of the above-mentioned class file.

[0044] In the above embodiment, when the file listener detects that the source code file has changed, the corresponding class file is located according to the changed file path. The changed source code file is compiled using a Java compiler (such as javac) to generate a new class file (.class). A custom class loader (such as HotUpdateClassLoader) is created to load the compiled new class file and replace the original class. Since the custom class loader has a dynamic class loading function, there is no need to wait for restart to reload the new class file, and the original class can be directly dynamically loaded to replace the original class to achieve hot update.

[0045] In an exemplary embodiment, the above step S206 can be implemented by the following steps: Step S2061, using the reflection mechanism to dynamically create an object corresponding to the loaded new class file to call the method of the object corresponding to the new class file to achieve hot update of the source code file.

[0046] In the above embodiment, since the reflection mechanism can obtain the variable and method information of a class at runtime, and then create an object based on the obtained information, the object corresponding to the loaded new class file can be dynamically created, so that the method of the object corresponding to the new class file can be called to realize the hot update of the source code file.

[0047] In an exemplary embodiment, after the above step S206, the above method also includes: step S301, integrating the file listener and the custom class loader into the above Java application project, the above custom class loader has a dynamic class loading function; step S302, initializing the above file listener and the above custom class loader during the startup process of the above Java application project; step S303, modifying the class loading policy of the above Java application project so that the above custom class loader is used preferentially when loading the above class file; step S304, modifying the above source code file; step S305, calling the above Java application project to execute the above monitoring step, the above updating step and the above calling step in sequence; step S306, calling the test interface or page of the above Java application project to output the output result of calling the method of the above object; step S307, when all the above methods are called successfully in the above output result, determining that the modification of the above source code file is effective.

[0048] In the above embodiment, the above file listening and class reloading mechanism are integrated into the Java application. During the startup process of the Java application, the file listener and the custom class loader are initialized. The class loading strategy of the Java application is modified so that the custom class loader is used preferentially when the class needs to be loaded, so as to facilitate the subsequent modification of the source code at any time to perform hot updates of the source code files. A test interface or page is provided to verify whether the hot update is successful. After modifying the code, the developer checks whether the modification has taken effect by accessing the test interface or page, that is, the test interface or page outputs the output results of calling the methods of the above objects. If all the above methods in the output results are called successfully, the modification of the above source code file takes effect, otherwise it does not take effect.

[0049] In an exemplary embodiment, after the above step S306, the above method also includes: step S401, when the above output results show that all the above methods have failed to be called, locating the above source code files corresponding to the above methods that failed to be called, and issuing an error reminder to remind checking whether the corresponding source code files are modified incorrectly.

[0050] In the above embodiment, if the above output results show that all the above methods fail to be called, it means that at least one method cannot be called and a bug occurs. In this case, it is necessary to locate the corresponding source code file and issue an error reminder to remind the user to check whether the corresponding source code file is modified incorrectly, so as to timely check the modification errors of the source code, fix the bug in time and improve development efficiency.

[0051] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, disk, CD), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the above methods of each embodiment of the present application.

[0052] In order to enable those skilled in the art to more clearly understand the technical solution of the present application, the implementation process of the Java code hot update method of the present application will be described in detail below in conjunction with specific embodiments.

[0053] This embodiment relates to a specific Java code hot update method, such as Figure 3 As shown, the following steps are included:

[0054] 1. Implementation of file listener:

[0055] Create a class called FileWatcher that uses the java.nio.file.WatchService API to monitor file changes.

[0056] In the FileWatcher class, define a start() method to start the file monitoring service and register one or more monitoring directories.

[0057] When a file change event occurs, FileWatcher calls a processFileChange() method, which receives the path of the changed file as a parameter and triggers subsequent processing logic (such as compiling source code, loading new class files, etc.).

[0058] The following is the specific Java code:

[0059]

[0060]

[0061] 2. Implementation of custom class loader:

[0062] When the file listener detects that the source code file has changed, it locates the corresponding class file (if it exists) according to the changed file path.

[0063] Use a Java compiler (such as javac) to compile the changed source code file to generate a new class file (.class).

[0064] Create a HotUpdateClassLoader class that inherits from java.lang.ClassLoader. It is used to load new class files and replace the original classes. After the new class files are loaded, we can use the reflection mechanism to dynamically create objects and call corresponding methods to implement the hot update function.

[0065]

[0066] In the above code, we create a custom class loader HotUpdateClassLoader to load new class files. When hot update is needed, we can pass the bytecode of the new class file through the loadClass method, and then define the new class through the defineClass method. After the new class file is loaded, we can use the reflection mechanism to dynamically create objects and call the corresponding methods to implement the hot update function.

[0067] 3. Application integration and testing:

[0068] During the startup of the Java application, FileWatcher and HotUpdateClassLoader (or corresponding class loader mechanism) are initialized.

[0069] Modify the class loading policy of the Java application so that the custom class loader is used first when classes need to be loaded.

[0070] Next, we need to create a sample class to demonstrate the effect of hot update. Suppose we have a simple service class TestService, which contains a method printMessage:

[0071]

[0072]

[0073] Now we need to update TestService and modify the output of the printMessage method. We can create a new class file NewTestService, which contains the modified printMessage method:

[0074]

[0075] Next, we can load the new class file through HotUpdateClassLoader and replace the original TestService class:

[0076]

[0077] In the sample code above, we load the new class file NewTestService through HotUpdateClassLoader and replace the original TestService class. Finally, we create an instance of NewTestService and call the printMessage method to output the updated information, and determine whether the modification is effective based on the output result.

[0078] In this embodiment, a hot update device for Java code is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware for a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.

[0079] Figure 4 is a structural block diagram of a hot update device for Java code according to an embodiment of the present application, such as Figure 4 As shown, the device comprises:

[0080] In a specific application, the file monitoring function is used to monitor in real time whether the source code file has changed, so that the modified source code file can be found.

[0081] A monitoring module 22, used to execute the monitoring step, and monitor in real time whether the source code files in the specified directory of the Java application project are changed, wherein the specified directory is the directory where the source code files are stored;

[0082] An updating module 24 is used to execute the updating step, determine the changed source code file as the target source code file, compile the target source code file to obtain a new class file, dynamically load the new class file to replace the class file corresponding to the target source code file, and the class file is a bytecode file generated by compiling the source code file before the change;

[0083] In a specific application, the changed source code file is found and determined as the target source code file, that is, the modified source code. The changed source code file is compiled using a Java compiler (such as javac) to generate a new class file (.class). The compiled new class file is loaded and replaces the original class.

[0084] The first calling module 26 is used to execute the calling step, create an object corresponding to the new class file to call the method of the object corresponding to the new class file, and realize the hot update of the source code file.

[0085] In specific applications, when a new class file is loaded, we can use the reflection mechanism to dynamically create objects and call corresponding methods to implement hot update function.

[0086] Through the above modules, the hot update device of the Java code monitors whether the source code file changes, that is, the source code file that has changed is determined as the target source code file, that is, the modified source code file, and the target source code file is compiled to obtain a new class file, so that the new class file replaces the class file corresponding to the target source code file, thereby dynamically loading the new class file, and creating an object corresponding to the new class file to call the method of the object corresponding to the new class file. Since the new class file is loaded dynamically, there is no need to restart to load the new class file, and the hot update of the source code file can be realized. Therefore, the problem of frequent restart of Java code modification affecting user experience in the related technology can be solved. The need for frequent restarts also saves a lot of time, achieving the effect of improving development efficiency.

[0087] In summary, the hot update device of the above Java code has the following effects:

[0088] Improve development efficiency: Developers can see the effects of code modifications in real time without having to manually restart the application server, which greatly improves development efficiency.

[0089] Reduce development costs: Reduce the time and resource consumption caused by frequent restarts of application servers.

[0090] Enhanced user experience: During the development process, developers can respond to changes in requirements and fix problems more quickly, thereby improving the quality of the software and the user experience.

[0091] Flexibility and scalability: This approach does not rely on a specific framework or library and can be easily integrated into various Java application projects and expanded and optimized as needed.

[0092] In an exemplary embodiment, the monitoring module includes a first creation submodule and a monitoring submodule. The first creation submodule is used to create a file listener, and the file listener is Java's WatchService API or a third-party library; the monitoring submodule is used to use the file listener to monitor in real time whether the source code file in the specified directory of the Java application project changes.

[0093] In the above embodiments, different file listeners can implement file monitoring. For example, Java's java.nio.file.WatchService API or a third-party library (such as Apache Commons IO's FileAlterationObserver) is used to implement the file monitoring function, so as to timely discover the modified source code files for hot update.

[0094] In an exemplary embodiment, the monitoring submodule includes a generating subunit and a monitoring subunit, wherein the generating subunit is used to generate a monitoring instruction when a scheduled monitoring task or a manual monitoring task is triggered, the scheduled monitoring task is a task triggered once at a predetermined interval, and the manual monitoring task is a task triggered in response to a predetermined operation; the monitoring subunit is used to respond to the monitoring instruction and use the file listener to monitor whether the source code file in the specified directory of the Java application project changes.

[0095] In the above embodiment, a timed monitoring task is set to trigger the generation of a monitoring instruction once at a predetermined time interval, so that in response to the above monitoring instruction, the above file listener is used to monitor whether the above source code file in the specified directory of the above Java application project has changed, without the need for real-time monitoring, thus saving resources. In addition, a manual monitoring task can also be set, which will trigger the generation of the monitoring instruction only after the developer performs a predetermined operation, so that the developer can modify the source code and then perform a predetermined operation to trigger the generation of the monitoring instruction, so as to timely discover the modified source code file, thereby improving the flexibility and usage experience of file monitoring.

[0096] In an exemplary embodiment, the update module includes a determination submodule, a positioning submodule, a compilation submodule, a second creation submodule and a loading submodule, wherein the determination submodule is used to determine the changed source code file as the target source code file and obtain the file path of the target source code file; the positioning submodule is used to locate the class file corresponding to the target source code file according to the file path of the target source code file and obtain the path of the class file; the compilation submodule is used to compile the target source code file to obtain a new class file; the second creation submodule is used to create a custom class loader, and the custom class loader has a dynamic class loading function; the loading submodule is used to load the new class file using the custom class loader to replace the class file corresponding to the target source code file according to the path of the class file.

[0097] In the above embodiment, when the file listener detects that the source code file has changed, the corresponding class file is located according to the changed file path. The changed source code file is compiled using a Java compiler (such as javac) to generate a new class file (.class). A custom class loader (such as HotUpdateClassLoader) is created to load the compiled new class file and replace the original class. Since the custom class loader has a dynamic class loading function, there is no need to wait for restart to reload the new class file, and the original class can be directly dynamically loaded to replace the original class to achieve hot update.

[0098] In an exemplary embodiment, the first calling module includes a third creating submodule, wherein the third creating submodule is used to adopt a reflection mechanism to dynamically create an object corresponding to the loaded new class file, so as to call the method of the object corresponding to the new class file to realize hot update of the source code file.

[0099] In the above embodiment, since the reflection mechanism can obtain the variable and method information of a class at runtime, and then create an object based on the obtained information, the object corresponding to the loaded new class file can be dynamically created, so that the method of the object corresponding to the new class file can be called to realize the hot update of the source code file.

[0100] In an exemplary embodiment, the device further includes an integration module, an initialization module, a first modification module, a second modification module, a repetition module, a second calling module and a determination module, wherein the integration module is used to integrate the file listener and the custom class loader into the Java application project after creating the object corresponding to the new class file to call the method of the object corresponding to the new class file, and the custom class loader has a dynamic class loading function; the initialization module is used to initialize the file listener and the custom class loader during the startup process of the Java application project; the first modification module is used to modify the class loading policy of the Java application project so that the custom class loader is used preferentially when loading the class file; the second modification module is used to modify the source code file; the repetition module is used to call the Java application project to sequentially execute the monitoring step, the updating step and the calling step; the second calling module is used to call the test interface or page of the Java application project to output the output result of calling the method of the object; the determination module is used to determine that the modification of the source code file is effective when all the methods in the output result are successfully called.

[0101] In the above embodiment, the above file listening and class reloading mechanism are integrated into the Java application. During the startup process of the Java application, the file listener and the custom class loader are initialized. The class loading strategy of the Java application is modified so that the custom class loader is used preferentially when the class needs to be loaded, so as to facilitate the subsequent modification of the source code at any time to perform hot updates of the source code files. A test interface or page is provided to verify whether the hot update is successful. After modifying the code, the developer checks whether the modification has taken effect by accessing the test interface or page, that is, the test interface or page outputs the output results of calling the methods of the above objects. If all the above methods in the output results are called successfully, the modification of the above source code file takes effect, otherwise it does not take effect.

[0102] In an exemplary embodiment, the above-mentioned device also includes an error reporting module, which is used to locate the above-mentioned source code file corresponding to the above-mentioned method that failed to call the output result of calling the test interface or page of the above-mentioned Java application project, and issue an error reminder to remind to check whether the corresponding above-mentioned source code file has been modified incorrectly if there is a case in the above-mentioned output result that all the above-mentioned methods failed to call.

[0103] In the above embodiment, if the above output results show that all the above methods fail to be called, it means that at least one method cannot be called and a bug occurs. In this case, it is necessary to locate the corresponding source code file and issue an error reminder to remind the user to check whether the corresponding source code file is modified incorrectly, so as to timely check the modification errors of the source code, fix the bug in time and improve development efficiency.

[0104] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.

[0105] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when running.

[0106] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.

[0107] An embodiment of the present application further provides a Java code development system, comprising a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.

[0108] In an exemplary embodiment, the Java code development system may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.

[0109] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.

[0110] Obviously, those skilled in the art should understand that the above modules or steps of the present application can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order from that herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.

[0111] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the principles of the present application shall be included in the protection scope of the present application.

Claims

1. A Java code hot update method, characterized in that: include: A monitoring step, monitoring in real time whether source code files in a specified directory of the Java application project change, wherein the specified directory is a directory storing the source code files; An updating step of determining the changed source code file as a target source code file, compiling the target source code file to obtain a new class file, and dynamically loading the new class file to replace the class file corresponding to the target source code file, wherein the class file is a bytecode file generated by compiling the source code file before the change; The calling step is to create an object corresponding to the new class file to call the method of the object corresponding to the new class file to realize the hot update of the source code file.

2. The method according to claim 1, characterized in that: Real-time monitoring of source code files in a specified directory in a Java application project to see if they have changed, including: Create a file listener, which is Java's WatchService API or a third-party library; The file listener is used to monitor in real time whether the source code file in the specified directory of the Java application project changes.

3. The method according to claim 2, characterized in that The file listener is used to monitor in real time whether the source code file in the specified directory of the Java application project changes, including: Generate a monitoring instruction when a scheduled monitoring task or a manual monitoring task is triggered, wherein the scheduled monitoring task is a task triggered once at a predetermined interval, and the manual monitoring task is a task triggered in response to a predetermined operation; In response to the monitoring instruction, the file monitor is used to monitor whether the source code file in the specified directory of the Java application project changes.

4. The method according to claim 1, characterized in that: Determining the changed source code file as a target source code file, compiling the target source code file to obtain a new class file, and dynamically loading the new class file to replace the class file corresponding to the target source code file, including: Determine the changed source code file as a target source code file, and obtain a file path of the target source code file; Locate the class file corresponding to the target source code file according to the file path of the target source code file to obtain the path of the class file; Compiling the target source code file to obtain a new class file; Creating a custom class loader, wherein the custom class loader has a dynamic class loading function; The custom class loader is used to load the new class file to replace the class file corresponding to the target source code file according to the path of the class file.

5. The method according to claim 4, characterized in that Creating an object corresponding to the new class file to call a method of the object corresponding to the new class file to implement hot update of the source code file includes: The reflection mechanism is adopted to dynamically create an object corresponding to the loaded new class file, so as to call the method of the object corresponding to the new class file and realize the hot update of the source code file.

6. The method according to any one of claims 1 to 5, characterized in that After creating an object corresponding to the new class file to call a method of the object corresponding to the new class file to implement hot update of the source code file, the method further includes: Integrate a file listener and a custom class loader into the Java application project, wherein the custom class loader has a dynamic class loading function; During the startup of the Java application project, initializing the file listener and the custom class loader; Modify the class loading strategy of the Java application project so that the custom class loader is used preferentially when loading the class file; Modifying the source code file; Calling the Java application project to sequentially execute the monitoring step, the updating step and the calling step; Call the test interface or page of the Java application project to output the output result of calling the method of the object; When all the methods in the output result are called successfully, it is determined that the modification of the source code file is effective.

7. The method according to claim 6, characterized in that After calling the test interface or page of the Java application project to output the output result of calling the method of the object, the method further includes: In the case that all the methods fail to be called in the output result, the source code file corresponding to the method that fails to be called is located, and an error reminder is issued to remind the user to check whether the corresponding source code file is modified incorrectly.

8. A Java code hot update device, characterized in that: include: A monitoring module, used to execute the monitoring step, and monitor in real time whether the source code files in a specified directory of the Java application project are changed, wherein the specified directory is a directory storing the source code files; An update module, used to execute the update step, determine the changed source code file as a target source code file, compile the target source code file to obtain a new class file, dynamically load the new class file to replace the class file corresponding to the target source code file, and the class file is a bytecode file generated by compiling the source code file before the change; The first calling module is used to execute the calling step, create an object corresponding to the new class file to call the method of the object corresponding to the new class file, and realize the hot update of the source code file.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program implements the steps of the method described in any one of claims 1 to 7 when executed by a processor.

10. A Java code development system, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method described in any one of claims 1 to 7 are implemented.