简体   繁体   中英

How to invoke a java program with classpath from Python 3.x

I am trying to execute an external java program from a python 3.7 program using the java command with classpath. I am using subprocess.Popen module in Python. Somehow I am not able to get it working! Appreciate any assistance!

cmd = ['java',
           '-classpath', 'C:/Users/Documents/MqTransfer.jar', 'C:/Users/Documents/com.ibm.mq.commonservices.jar',
           'C:/Users/Documents/com.ibm.mq.headers.jar', 'C:/Users/Documents/com.ibm.mq.jar',
           'C:/Users/Documents/com.ibm.mq.jmqi.jar', 'C:/Users/Documents/com.ibm.mq.pcf.jar',
           'C:/Users/Documents/connector.jar', 'C:/Users/Documents/xerces.jar',
           'MyMqTransfer', 'C:/Users/Documents/queueTransfer.properties']
    jproc = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
    output, errors = jproc.communicate()
    print(output, errors)

I am getting the below error

b'' b'Error: Could not find or load main class C:.Users.Documents.com.ibm.mq.commonservices.jar\\r\\n'

When I try to run the java program from my batch script it runs fine! This is the command I use in my batch script. The issue is with my python code!

java -classpath MqTransfer.jar;com.ibm.mq.commonservices.jar;com.ibm.mq.headers.jar;com.ibm.mq.jar;com.ibm.mq.jmqi.jar;com.ibm.mq.pcf.jar;connector.jar;xerces.jar  com.ibm.my.mq.MyMqTransfer C:\Users\Documents\queueTransfer.properties

Based on the error, I believe the process being executed is something like 'java -classpath C:/Users/Documents/MqTransfer.jar c:/Users/Documents/com.ibm.mq.commonServices.jar [followed by the rest of the arguments you are passing to process]' such that java is passed MqTransfer.jar as the entire classpath argument and thinks 'C:.Users.Documents.com.ibm.mq.commonservices.jar' is your class to launch. Try combining your entire intended classpath into the 3rd argument of your launch and I think you will be good. It would look something like this:

cmd = ['java',
           '-classpath', 'C:/Users/Documents/MqTransfer.jar;C:/Users/Documents/com.ibm.mq.commonservices.jar;C:/Users/Documents/com.ibm.mq.headers.jar;C:/Users/Documents/com.ibm.mq.jar;C:/Users/Documents/com.ibm.mq.jmqi.jar;C:/Users/Documents/com.ibm.mq.pcf.jar;C:/Users/Documents/connector.jar;C:/Users/Documents/xerces.jar',
           'MyMqTransfer', 'C:/Users/Documents/queueTransfer.properties']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM