I have a software, its a .jar file and is installed in my linux system. I want use that program through python. How can i import that program so that it works in python script. I want to use that program in loop and I want to use python to create that loop.
Asked
Active
Viewed 31 times
-1
-
Possible duplicate of [Python: How can I execute a jar file through a python script](https://stackoverflow.com/questions/7372592/python-how-can-i-execute-a-jar-file-through-a-python-script) – Ronan Boiteau Apr 05 '18 at 05:48
-
you want to use the `subprocess` module to spawn processes. You can't import jar files directly in python. – avigil Apr 05 '18 at 05:49
-
please be specific with what program you want to run and share the approach that you are taking in order to achieve that. No one can write full code for you. Refer https://stackoverflow.com/help/how-to-ask – arctic_Oak Apr 05 '18 at 05:53
1 Answers
0
You need to import a connection between python and the JVM for running java commands. Then you can create classes and call methods and get the returns like normal.
from py4j.java_gateway import JavaGateway # Import JVM-Connector
java_gateway = JavaGateway() # Connect to the JVM
java_TestClass = gateway.jvm.<Package>.Test() # Initalize a Class
result = java_TestClass.doSomethingWithReturn() # Call a method with return
java_TestClass.doSomethingElse("hello world") # Call void-Methhod
# Invoke static methods
java_gateway.jvm.java.lang.System.out.println('Hello World!')
For using a compiled .jar you need to import this, too.
import os
os.system("java -jar MyJar.jar")

LenglBoy
- 1,451
- 1
- 10
- 24
-
You´re welcome - but like @arcticOak2 told you - please specify you tried things more and post some code. It´s not for "general questions" I just worked on something like this so lucky – LenglBoy Apr 05 '18 at 06:08
-