Generate executable jar packages based on IDEA
1. Write class code, note that there must be main () method to generate the jar package, main () method can be no content.
Example:
public class testFunction { public static void main(String[] args) { } public static void send() { ("i use message"); } }
2. Click file->project structure
3. Click to select the class you want to generate jar package, here I choose.
Note that the location of Directory for meta-inf/ should not be selected by default, it is better to select the project root directory.
I choose the default will not find the location of the generated jar package.
4. Click Build->Build Artifacts to select the corresponding Artifact, and click Build to generate success.
5. generated jar package
How Python calls jar
1. you need to download jpype, windows + R enter cmd to enter the command prompt, the path to the directory where jpype, the implementation of pip install jpype file name.
2. Please search for the specific operation method, you can find the detailed steps.
The code is as follows
def sendMessage(): # 1. load jar package jarpath = (("."),"(Fill in yourjarpacketfilepath)\\wit_test.jar") # 2. Get the path to the file jvmPath = () # 3. Turn on jvm (jvmPath, "-ea", "-=%s" % (jarpath)) # 4. load java class (argument is long java class name) javaClass = ("testFunction") # 5. Instantiate java objects javaInstance = javaClass() # 6. call java methods, since I wrote static methods, I can call the methods directly using the class name () # 7. Shut down jvm () pass
The above code works if the jar package is called only once.
If you need to call it multiple times there will be a restart failure problem, then the solution is to put the code in the main() function, and after the first 5 steps of the code have been initialized, only the 6th step of the code will be looped through each time.
Step 7 code removed.
summarize
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.