1

I am working on decryption a PGP file which I could accomplish using Python as I absolutely could not find the resources/example for it. I did raise the problem here with no luck!

However, I did develop some code in Java to decrypt the file.

Now, I want to invoke this jar file from my python code since I can find people using the os.system or subprocess.call to invoke external programmes in a similar fashion.

Below is the sample code which is supposed to do the work:

 os.system("java -jar PGPEncryption.jar BC.csv.pgp X.csv <password>")

 x = subprocess.call(["java -jar PGPEncryption.jar BC.csv.pgp X.csv <password>"], shell=True)
 print(x)

I have no luck with printing a result.

Any Suggestions will be really appreciated!!

EDIT: I have also tried below code:

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

it's not working and gives below error:

 Exception in thread "main" java.lang.UnsupportedClassVersionError: 
        pgpencryption/PGPExampleUtil : Unsupported major.minor version 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: pgpencryption.PGPExampleUtil. Program will exit.
Parth S Rawal
  • 27
  • 1
  • 10
  • AFAIK, running with `shell=True` should print the results to `stdout`, i.e you don't need to print `x`. Maybe a better solution is to generate a CompletedProcess object, which has `stdout` as an attribute. See [this documentation](https://docs.python.org/3/library/subprocess.html), the python page explaining subprocess. – avery_laird Oct 12 '17 at 10:17
  • @Parth S Rawal, Please see my answer below. That might help you. – Abhijit Pritam Dutta Oct 12 '17 at 11:02

2 Answers2

1

Here your first problem is with your java version with which you have compiled the code and java version with which you are running the code.

For example, if you have compiled the code with java version 8 and you are running the java application with java version 7 or 6 (lower than compiled one) you will get a Unsupported major.minor version 52.0 error. Hence compile the code with a lower or same version than your server one.

Check the version in your server : java --version

Check the version in your development tool with which you have compiled the code

In the below code, provide full path to your jar file as well.

os.system("java -jar \fullpath\PGPEncryption.jar BC.csv.pgp X.csv <password>")

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
0

If you want to use modules in JAR file in python code, the you need to run that py file using JYTHON.

java -jar jython.jar demo.py

Jython download

Check here for tutorial

Subprocess with Popen

import subprocess

x = subprocess.Popen("java -jar PGPEncryption.jar BC.csv.pgp X.csv <password>", stderr=subprocess.PIPE, stdout=subprocess.PIPE)

out,err = x.communicate()

print "Error is - ",err
print "Output is -",out
Dinesh Pundkar
  • 4,160
  • 1
  • 23
  • 37