Python: How to Execute a Jar File Through a Python Script

Python: How can I execute a jar file through a python script

I would use subprocess this way:

import subprocess
subprocess.call(['java', '-jar', 'Blender.jar'])

But, if you have a properly configured /proc/sys/fs/binfmt_misc/jar you should be able to run the jar directly, as you wrote.

So, which is exactly the error you are getting?
Please post somewhere all the output you are getting from the failed execution.

Attempting to execute .jar file using python

One thing that frequently works is using shell=True option on the subprocess, this executes the command as if it was done in a shell (batch file or command prompt)

So using:

import subprocess
subprocess.call(['java', '-jar', 'StatsCalc.jar'],shell=True)

Should work for you, but please read the Security Considerations section of the subprocess's documentation before relying too heavily on this.

Usually the reason it does not work without shell is because it cannot find the command java without using the full path, so if you know the full path of the java executable that would be preferable to using shell=True

import subprocess
subprocess.call(['path/to/java', '-jar', 'StatsCalc.jar'])

I don't know the exact details on the difference between these two but

Running python script from jar file

The problem had multiple layers and solutions:
1. I didn't put .py file in jar build config
2. After putting it I always got an exception that it is null because of a typo in code
3. After trying many ways to run it this one worked Cannot run python script from java jar
. The important thing is to check if you added py file to the build config and to run it in a proper way since python cannot runt files from the zip and compressed states.

Calling a python script from a Java jar file

In this solution, we run the script if the file exists. The script could be on a full or relative path. The script is not in the jar file.

TestPython.java

import java.lang.*;
import java.io.*;

public class TestPython {
public static void main(String[] args) {

System.out.println("I will run a Python script!");
Runtime r = Runtime.getRuntime();
String pyScript = "py/test.py";

File f = new File(pyScript);
if (f.exists() && !f.isDirectory()) {
try {
Process p = r.exec("python " + pyScript);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in .readLine()) != null) {
System.out.println(line);
}
System.out.println("Python script ran!!");
} catch (Exception ex) {
System.out.println("Something bad happened!!");
ex.printStackTrace();
}
} else {
System.out.println("Unexistent file!" + pyScript);
}
}
}

py/test.py

print("I'm a Python script!!!")

Output:

I will run a Python script!

I'm a Python script!!!

Python script ran!!

How can I get my python (version 2.5) script to run a jar file inside a folder instead of from command line?

Here is a small script to get you started. There are ways to make it "better", but not knowing the full scope of what you are trying to accomplish this should be sufficient.

import os

if __name__ == "__main__":
startingDir = os.getcwd() # save our current directory
testDir = "\\test" # note that \ is windows specific, and we have to escape it
os.chdir(testDir) # change to our test directory
os.system("java -jar run_this.jar required_paramter.ext")
os.chdir(startingDir) # change back to where we started

How to call Python script from a Java JAR file

  • Part1:(import .jar file as library in Eclipse)

You make a new project to Eclipse(name:Project1)
When you open it you see JRE System Library[java version something]

1.right click on JRE System Library

2.Go->Build Path->Configure Build Path

3.You can see (Up right Corner the button[add jars or add external jars]

*Here i advise you to choose the first(add jars) but..

*First copy(or move) the Python.jar inside the project((example):Project 1)

*Now you can add it with the button(add jars).

*In this way when you finish your project the Python.jar will be
imported inside the project(If you export it as a .jar from Eclipse)

..Now you can call any method of Python.jar just(import it into the class
you want to use and call it)

Let me know if that works for you...

Part 2:

  • Running a file which is inside the eclipse Project(warning! before export it to jar):

1.Way(Following the path of file)

 try {
Runtime.getRuntime().exec("cmd /c start C:/NameOfEclipseProject/Project/src/pack(Package)/script.py");

} catch (IOException e) { e.printStackTrace(); }

2.i will add them soon..(it has been time since i used them try to find)

  • Running the script after export project to jar(warning! after export to jar)

Check this question link



Related Topics



Leave a reply



Submit