Java applet performs method and apparatus
Technical field
The present invention relates to computer realm, more particularly to a kind of java applet performs method and apparatus.
Background technology
Java is a kind of object oriented program language that can write cross-platform software, is by Sun Microsystems
The java applet design language of release and the general name of Java platform.Java has remarkable versatility, high efficiency, platform transplantation
And security, it is widely used in every field.
Java applet on windows platforms when running, it is often necessary to Windows Powershell scripts are called, so that
Reach corresponding business purpose.And for operating in the java application on windows platform, it can only be answered by Java
Reached with JavalangRuntime exec (String command) methods provided in programming interface, the method can worked as
An independent Powershell script process is restarted outside the java application process of preceding operation, so as to perform this
Powershell scripts in java applet, script terminate the process again after being finished.
In the implementation of the present invention, inventor has found that the prior art has at least the following problems:
Java applet, which often performs a Powershell script, will create an independent Powershell script process,
If multiple processes will be created at the same time by there are multiple threads concurrently to perform Powershell scripts in java applet, cause to take
More system resource, it is big to produce expense.
The content of the invention
In order to solve java applet in the prior art often perform a Powershell script will create one it is independent
Process, the problem of causing to take excessive system resource, an embodiment of the present invention provides a kind of java applet to perform method and dress
Put.The technical solution is as follows:
On the one hand, the embodiment of the present invention provides a kind of java applet and performs method, the described method includes:
Dynamic link library file is loaded, the dynamic link library file includes:Performed for creating Powershell scripts
The realization of first nation method in environment pond and the realization of the second nation method for performing Powershell scripts;
First nation method is called, creates Powershell script execution environments pond, the Powershell scripts
Performing environment pond includes multiple Powershell script execution environments;
Second nation method is called, is performed in the Powershell script execution environments in the java applet
Powershell scripts;
Return to Powershell script implementing results.
In a kind of implementation of the embodiment of the present invention, the dynamic link library file is further included for pre-loaded
Powershell scripts rely on the realization of the 3rd nation method of class libraries, and the method further includes:
Before second nation method is called, the 3rd nation method is called, in each Powershell feet
Pre-loaded Powershell scripts rely on class libraries in this performing environment.
In another implementation of the embodiment of the present invention, the loading dynamic link library file, including:
The class files for being used for performing Powershell scripts are loaded, the class files include called dynamic chain
Connect the statement of the title in storehouse, and the sound of first nation method, second nation method and the 3rd nation method
It is bright;
According to the title of the dynamic link library of the class file states, the dynamic link library file is loaded.
In another implementation of the embodiment of the present invention, the method further includes:
Generate the dynamic link library file and the class files.
In another implementation of the embodiment of the present invention, the generation dynamic link library file and described
Class files, including:
Java files are obtained, the Java files include the statement of the title of called dynamic link library, Yi Jisuo
State the statement of the first nation method, second nation method and the 3rd nation method;
The Java files are compiled, obtain the class files;
According to the class file generateds Java local interface header files;
Obtain and compile C# codes, generate CS files, the C# codes include drawing for the Java local interfaces header file
Enter, and first nation method, second nation method and the 3rd nation method stated in the header file
Realization;
The CS files are compiled as the dynamic link library file.
In another implementation of the embodiment of the present invention, the generation dynamic link library file and described
Class files, including:
Obtain and compile C# codes, generate CS files, the C# codes include first nation method, second described
The realization of ground method and the 3rd nation method;
The CS files are compiled as the dynamic link library file;
The dynamic link library file is compiled using JNI4NET compilation tools, obtains the class files.
On the other hand, the embodiment of the present invention additionally provides a kind of java applet executive device, and described device includes:
Input module, for loading dynamic link library file, the dynamic link library file includes:For creating
The realization of first nation method in Powershell script execution environments pond and second for performing Powershell scripts
The realization of nation method;
First processing module, for calling first nation method, creates Powershell script execution environments pond, institute
Stating Powershell script execution environments pond includes multiple Powershell script execution environments;
Second processing module, for calling second nation method, in the Powershell script execution environments
Perform the Powershell scripts in the java applet;
Output module, for returning to Powershell script implementing results.
In a kind of implementation of the embodiment of the present invention, the dynamic link library file is further included for pre-loaded
Powershell scripts rely on the realization of the 3rd nation method of class libraries, and described device further includes:
3rd processing module, for before second nation method is called, the 3rd nation method being called, each
Pre-loaded Powershell scripts rely on class libraries in the Powershell script execution environments.
In another implementation of the embodiment of the present invention, the input module, including:
Loading unit, for loading the class files for being used for performing Powershell scripts, the class files include
The statement of the title of the dynamic link library called, and first nation method, second nation method and described
The statement of three nation methods;
Execution unit, for the title of the dynamic link library according to the class file states, loads the dynamic link
Library file.
In another implementation of the embodiment of the present invention, described device further includes:
Generation module, for generating the dynamic link library file and the class files.
In another implementation of the embodiment of the present invention, the generation module, including:
Acquiring unit, for obtaining Java files, the Java files include the title of called dynamic link library
Statement, and the statement of first nation method, second nation method and the 3rd nation method;
First compilation unit, for compiling the Java files, obtains the class files;
Second compilation unit, for according to the class file generateds Java local interface header files;
3rd compilation unit, for obtaining and compiling C# codes, generates CS files, the C# codes include the Java sheets
The introducing of ground interface header file, and stated in the header file first nation method, second nation method and
The realization of 3rd nation method;
4th compilation unit, for the CS files to be compiled as the dynamic link library file.
In another implementation of the embodiment of the present invention, the generation module, including:
5th compilation unit, for obtaining and compiling C# codes, generates CS files, the C# codes include the first
The realization of ground method, second nation method and the 3rd nation method;
6th compilation unit, for the CS files to be compiled as the dynamic link library file;
7th compilation unit, for compiling the dynamic link library file using JNI4NET compilation tools, obtains described
Class files.
The beneficial effect that technical solution provided in an embodiment of the present invention is brought is:
By loading dll file, and the first nation method in the DLL is called, create Powershell scripts and perform ring
Border pond, Powershell script execution environments pond include multiple Powershell script execution environments, call second in DLL
Ground method, performs the Powershell scripts in java applet;On the one hand, performed as a result of nation method
Powershell scripts, therefore new process will not be created when performing Powershell scripts, meanwhile, in Powershell feet
This performing environment pond includes multiple Powershell script execution environments so that may be performed simultaneously multiple Powershell feet
This, avoid it is every execution one Powershell script will create an independent process, caused by the wasting of resources the problem of;
On the other hand, the 3rd nation method is further included in dll file, Powershell scripts can created by the 3rd nation method
Behind performing environment pond, the loading dependence class libraries in Powershell script execution environments, shortens java applet execution
The time of Powershell scripts.
Brief description of the drawings
To describe the technical solutions in the embodiments of the present invention more clearly, make required in being described below to embodiment
Attached drawing is briefly described, it should be apparent that, drawings in the following description are only some embodiments of the present invention, for
For those of ordinary skill in the art, without creative efforts, other can also be obtained according to these attached drawings
Attached drawing.
Fig. 1 is that the java applet that the embodiment of the present invention one provides performs method flow diagram;
Fig. 2 is that java applet provided by Embodiment 2 of the present invention performs method flow diagram;
Fig. 3 is that the java applet that the embodiment of the present invention three provides performs method flow diagram;
Fig. 4 is the structure diagram for the java applet executive device that the embodiment of the present invention four provides;
Fig. 5 is the structure diagram for the java applet executive device that the embodiment of the present invention five provides.
Embodiment
To make the object, technical solutions and advantages of the present invention clearer, below in conjunction with attached drawing to embodiment party of the present invention
Formula is described in further detail.
Embodiment one
An embodiment of the present invention provides a kind of java applet to perform method, and referring to Fig. 1, this method includes:
Step 101:DLL (Dynamic Link Library, dynamic link library) file is loaded, which includes:With
In the realization of the first Native (local) method for creating Powershell script execution environments pond and for performing
The realization of the 2nd Native methods of Powershell scripts.
The dll file is used to perform the Powershell scripts in java applet.
Step 102:The first Native methods are called, create Powershell script execution environments pond, Powershell feet
This performing environment pond includes multiple Powershell script execution environments.
Step 103:The 2nd Native methods are called, are performed in Powershell script execution environments in java applet
Powershell scripts.
Step 104:Return to Powershell script implementing results.
The embodiment of the present invention calls the first Native methods in the DLL by loading dll file, creates
Powershell script execution environments pond, Powershell script execution environments pond include multiple Powershell scripts and perform ring
Border, calls the 2nd Native methods in DLL, performs the Powershell scripts in java applet;On the one hand, as a result of
Native methods perform Powershell scripts, therefore will not create new process when performing Powershell scripts, together
When, include multiple Powershell script execution environments in Powershell script execution environments pond so that can hold at the same time
The multiple Powershell scripts of row, an independent process will be created by avoiding one Powershell script of every execution, be made
Into the wasting of resources the problem of.
Embodiment two
An embodiment of the present invention provides a kind of java applet to perform method, and referring to Fig. 2, this method includes:
Step 201:Java files are obtained, which includes the statement of the title of called DLL, and first
The statement of Native methods, the 2nd Native methods and the 3rd Native methods.
Java files are compiled by Java code.Wherein, the first Native methods are used to create Powershell
Script execution environment pond, includes some script execution environments in pond, the quantity of script execution environment can specify in code.The
Three Native methods are used for the pre-loaded Powershell scripts in above-mentioned script execution environment and rely on class libraries, are performed for script
When use.2nd Native methods are used to, when needing to perform Powershell scripts, obtain from script execution environment pond empty
Not busy script execution environment, and then perform Powershell scripts.Wherein, script execution environment pond refers to carry in multiple C# class libraries
For the set of object, it is a Powershell script executor which, which can regard, i.e., each object can be used as one
A Powershell script execution environments.Java applet needs only to need to obtain idle script when performing Powershell scripts
Performing environment, discharges the script execution environment again again after having performed, can thus reuse script execution environment, forms one
A script execution environment pond.
Preferably, the script that the 3rd Native methods are loaded, which relies on class libraries, to be included:What is included in the java applet is all
Required class libraries when Powershell scripts perform.
The Java files in step 201 are illustrated below:
Wherein, the Demo in System.LoadLibrary (" Demo ") is exactly the title of called DLL.
Step 202:Java files are compiled, obtain class files, which is used to perform Powershell scripts,
The statement of the title of the class files including called DLL, and the first Native methods, the 2nd Native methods and the
The statement of three Native methods.
The step can be completed in Java compiler.
Step 203:According to class file generateds JNI (Java Native Interface, Java local interface) head text
Part.
Specifically, the class files obtained in Javah compilation steps 202 can be used in command-line tool, so that raw
Into the header file for including three native methods.
Step 204:Obtain and compile C# codes, generate CS files, C# codes include the introducing of JNI header files, Yi Jitou
The realization of the first Native methods, the 2nd Native methods and the 3rd Native methods stated in file.
Step 205:CS files are compiled as dll file.
Above-mentioned steps 201~205 are the process of generation dll file.The dll file ultimately produced is in step 201
The DLL called stated in Java files.
Step 206:The dll file is loaded, which includes the first Native methods, the 2nd Native methods and the
The realization of three Native methods.
Specifically, step 206 can include:
Step 1: when java applet starts or performs Powershell scripts, the class produced in step 202 is loaded
File.
The basic component units of java applet are classes, and class includes method.When java applet performs, the class in java applet
The class files in Lib (storehouse) are loaded, to realize method that class includes, above-mentioned execution Powershell scripts can certain
Method in a class.
Step 2: according to the title of the DLL stated in the class files, dll file is loaded.
Step 207:The first Native methods are called, create Powershell script execution environments pond, Powershell feet
This performing environment pond includes multiple Powershell script execution environments.
Step 208:The 3rd Native methods are called, it is pre-loaded in each Powershell script execution environments
Powershell scripts rely on class libraries.
Step 209:The 2nd Native methods are called, are performed in Powershell script execution environments in java applet
Powershell scripts.
Step 210:Return to Powershell script implementing results.
The embodiment of the present invention calls the first Native methods in the DLL by loading dll file, creates
Powershell script execution environments pond, Powershell script execution environments pond include multiple Powershell scripts and perform ring
Border, calls the 2nd Native methods in DLL, performs the Powershell scripts in java applet;On the one hand, as a result of
Native methods perform Powershell scripts, therefore will not create new process when performing Powershell scripts, together
When, include multiple Powershell script execution environments in Powershell script execution environments pond so that can hold at the same time
The multiple Powershell scripts of row, an independent process will be created by avoiding one Powershell script of every execution, be made
Into the wasting of resources the problem of;On the other hand, the 3rd Native methods are further included in dll file, pass through the 3rd Native methods
Can be after Powershell script execution environments pond be created, the loading dependence class libraries in Powershell script execution environments,
Shorten the time that java applet performs Powershell scripts.
Embodiment three
An embodiment of the present invention provides a kind of java applet to perform method, the difference between the present embodiment and the second embodiment lies in that adopting
Compiling is realized with JNI4NET, and referring to Fig. 3, this method includes:
Step 301:Obtain and compile C# codes, generate CS files, C# codes include the first Native methods, second
The realization of Native methods and the 3rd Native methods.
Wherein, the first Native methods are used to create Powershell script execution environments pond, include some scripts in pond
Performing environment, the quantity of script execution environment can specify in code.3rd Native methods are used to perform in above-mentioned script
Pre-loaded Powershell scripts rely on class libraries in environment, are used when being performed for script.2nd Native methods, which are used to work as, to be needed
When performing Powershell scripts, the script execution environment of free time is obtained from script execution environment pond, and then perform
Powershell scripts.Wherein, script execution environment pond refers to the set that object is provided in multiple C# class libraries, which can
As being a Powershell script executor, i.e., each object can be used as a Powershell script execution environment.
Program needs only to need to obtain idle script execution environment when performing Powershell scripts, discharges the foot after having performed again again
This performing environment, can thus reuse script execution environment, form a script execution environment pond.
Preferably, the script that the 3rd Native methods are loaded, which relies on class libraries, to be included:What is included in the java applet is all
Required class libraries when Powershell scripts perform.
Step 302:CS files are compiled as dll file.
Step 303:Dll file is compiled using JNI4NET compilation tools, obtains class files, which is used for
Execution Powershell scripts, the statement of title of the class files including called DLL, and the first Native methods,
The statement of 2nd Native methods and the 3rd Native methods.
Step 304:Dll file is loaded, which includes the first Native methods, the 2nd Native methods and the 3rd
The realization of Native methods.
Specifically, step 304 can include:
Step 1: when java applet starts or performs Powershell scripts, the class generated in step 303 is loaded
File.
Step 2: according to the title of the DLL stated in the class files, dll file is loaded.
Step 305:The first Native methods are called, create Powershell script execution environments pond, Powershell feet
This performing environment pond includes multiple Powershell script execution environments.
Step 306:The 3rd Native methods are called, it is pre-loaded in each Powershell script execution environments
Powershell scripts rely on class libraries.
Step 307:The 2nd Native methods are called, are performed in Powershell script execution environments in java applet
Powershell scripts.
Step 308:Return to Powershell script implementing results.
The embodiment of the present invention calls the first Native methods in the DLL by loading dll file, creates
Powershell script execution environments pond, Powershell script execution environments pond include multiple Powershell scripts and perform ring
Border, calls the 2nd Native methods in DLL, performs the Powershell scripts in java applet;On the one hand, as a result of
Native methods perform Powershell scripts, therefore will not create new process when performing Powershell scripts, together
When, include multiple Powershell script execution environments in Powershell script execution environments pond so that can hold at the same time
The multiple Powershell scripts of row, an independent process will be created by avoiding one Powershell script of every execution, be made
Into the wasting of resources the problem of;On the other hand, the 3rd Native methods are further included in dll file, pass through the 3rd Native methods
Can be after Powershell script execution environments pond be created, the loading dependence class libraries in Powershell script execution environments,
Shorten the time that java applet performs Powershell scripts.
Example IV
An embodiment of the present invention provides a kind of java applet executive device, and referring to Fig. 4, which includes:
Input module 401, for loading dll file, dll file includes:Ring is performed for creating Powershell scripts
The realization of the first Native methods in border pond and the realization for performing the 2nd Native methods of Powershell scripts;
First processing module 402, for calling the first Native methods, creates Powershell script execution environments pond,
Powershell script execution environments pond includes multiple Powershell script execution environments;
Second processing module 403, for calling the 2nd Native methods, performs in Powershell script execution environments
Powershell scripts in java applet;
Output module 404, for returning to Powershell script implementing results.
The embodiment of the present invention calls the first Native methods in the DLL by loading dll file, creates
Powershell script execution environments pond, Powershell script execution environments pond include multiple Powershell scripts and perform ring
Border, calls the 2nd Native methods in DLL, performs the Powershell scripts in java applet;On the one hand, as a result of
Native methods perform Powershell scripts, therefore will not create new process when performing Powershell scripts, together
When, include multiple Powershell script execution environments in Powershell script execution environments pond so that can hold at the same time
The multiple Powershell scripts of row, an independent process will be created by avoiding one Powershell script of every execution, be made
Into the wasting of resources the problem of.
Embodiment five
An embodiment of the present invention provides a kind of java applet executive device, and referring to Fig. 5, which includes:Input module
501st, first processing module 502, Second processing module 503 and output module 504.
Input module 501, for loading dll file, dll file includes:Ring is performed for creating Powershell scripts
The realization of the first Native methods in border pond and the realization for performing the 2nd Native methods of Powershell scripts;
First processing module 502, for calling the first Native methods, creates Powershell script execution environments pond,
Powershell script execution environments pond includes multiple Powershell script execution environments;
Second processing module 503, for calling the 2nd Native methods, performs in Powershell script execution environments
Powershell scripts in java applet;
Output module 504, for returning to Powershell script implementing results.
Further, above-mentioned dll file, which is further included, relies on the 3rd of class libraries for pre-loaded Powershell scripts
The realization of Native methods, the device further include:
3rd processing module 505, for before the 2nd Native methods are called, the 3rd Native methods being called, each
Pre-loaded Powershell scripts rely on class libraries in Powershell script execution environments.
Wherein, the first Native methods are used to create Powershell script execution environments pond, include some scripts in pond
Performing environment, the quantity of script execution environment can specify in code.3rd Native methods are used to perform in above-mentioned script
Pre-loaded Powershell scripts rely on class libraries in environment, are used when being performed for script.2nd Native methods, which are used to work as, to be needed
When performing Powershell scripts, the script execution environment of free time is obtained from script execution environment pond, and then perform
Powershell scripts.Wherein, script execution environment pond refers to the set that object is provided in multiple C# class libraries, which can
As being a Powershell script executor, i.e., each object can be used as a Powershell script execution environment.
Program needs only to need to obtain idle script execution environment when performing Powershell scripts, discharges the foot after having performed again again
This performing environment, can thus reuse script execution environment, form a script execution environment pond.
Preferably, the script that the 3rd Native methods are loaded, which relies on class libraries, to be included:What is included in the java applet is all
Required class libraries when Powershell scripts perform.
In a kind of implementation of the embodiment of the present invention, input module 501 can include:
Loading unit, for when java applet starts or performs Powershell scripts, loading and being used to perform
The class files of Powershell scripts, class files include the statement of the title of called DLL, and the first Native
The statement of method, the 2nd Native methods and the 3rd Native methods;
Execution unit, for the title of the DLL according to class file states, loads dll file.
Further, device further includes:
Generation module 506, for generating dll file and class files.
In a kind of implementation of the embodiment of the present invention, generation module 506 includes:
Acquiring unit, for obtaining Java files, Java files include the statement of the title of called DLL, and
The statement of first Native methods, the 2nd Native methods and the 3rd Native methods;
First compilation unit, for compiling Java files, obtains class files;
Second compilation unit, for according to class file generated JNI header files;
3rd compilation unit, for obtaining and compiling C# codes, generates CS files, C# codes include drawing for JNI header files
Enter, and the realization for the first Native methods, the 2nd Native methods and the 3rd Native methods stated in header file;
4th compilation unit, for CS files to be compiled as dll file.
In another implementation of the embodiment of the present invention, generation module 506 includes:
5th compilation unit, for obtaining and compiling C# codes, generates CS files, C# codes include the first Native side
The realization of method, the 2nd Native methods and the 3rd Native methods;
6th compilation unit, for CS files to be compiled as dll file;
7th compilation unit, for using JNI4NET compilation tools compiling dll file, obtaining class files.
The embodiment of the present invention calls the first Native methods in the DLL by loading dll file, creates
Powershell script execution environments pond, Powershell script execution environments pond include multiple Powershell scripts and perform ring
Border, calls the 2nd Native methods in DLL, performs the Powershell scripts in java applet;On the one hand, as a result of
Native methods perform Powershell scripts, therefore will not create new process when performing Powershell scripts, together
When, include multiple Powershell script execution environments in Powershell script execution environments pond so that can hold at the same time
The multiple Powershell scripts of row, an independent process will be created by avoiding one Powershell script of every execution, be made
Into the wasting of resources the problem of;On the other hand, the 3rd Native methods are further included in dll file, pass through the 3rd Native methods
Can be after Powershell script execution environments pond be created, the loading dependence class libraries in Powershell script execution environments,
Shorten the time that java applet performs Powershell scripts.
It should be noted that:Above-described embodiment provide java applet executive device when performing Powershell scripts,
, can be as needed and by above-mentioned function distribution only with the division progress of above-mentioned each function module for example, in practical application
Completed by different function modules, i.e., the internal structure of device is divided into different function modules, it is described above to complete
All or part of function.In addition, the java applet executive device that above-described embodiment provides performs method with java applet and implements
Example belongs to same design, its specific implementation process refers to embodiment of the method, and which is not described herein again.
The embodiments of the present invention are for illustration only, do not represent the quality of embodiment.
One of ordinary skill in the art will appreciate that hardware can be passed through by realizing all or part of step of above-described embodiment
To complete, relevant hardware can also be instructed to complete by program, the program can be stored in a kind of computer-readable
In storage medium, storage medium mentioned above can be read-only storage, disk or CD etc..
The foregoing is merely presently preferred embodiments of the present invention, is not intended to limit the invention, it is all the present invention spirit and
Within principle, any modification, equivalent replacement, improvement and so on, should all be included in the protection scope of the present invention.