1

I am creating a python program that runs a jar file. The jar file and some support files are placed in a different location than the python program's directory. I tried adding jar file path to sys.path but it's unable to access the file from there, however the path is added to sys.path correctly. How can I get this working?

jar file location: E:\data

python file location: C:\Users\user\Desktop

I am using subprocess to call the jar file, the code looks like:

import os
import sys
import subprocess as sp

class abc():
    def __init__(self):
        sys.path.append(r'E:\data')

    def run(self):
        print sys.path
        env = dict(os.environ)
        env['JAVA_OPTS'] = '-Xms256m -Xmx256m -Xss1024k'
        sp.call(['java', '-jar', 'file.jar'], env=env)

if __name__ == '__main__':
    o = abc()
    o.run()

After running above code, I get an error saying:

Error: Unable to access jarfile file.jar

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Ayush
  • 909
  • 1
  • 13
  • 32
  • I know it's not a good way to include absolute path but what happens when you reference absolute path in place of 'file.jar'? – kmario23 Dec 08 '14 at 06:38
  • possible duplicate of [Python subprocess/Popen with a modified environment](http://stackoverflow.com/questions/2231227/python-subprocess-popen-with-a-modified-environment) – Peter Wood Dec 08 '14 at 06:41
  • @mario23 I can use absolute path in place of 'file.jar' but there are some support files as well which are required in order to execute file.jar That's why I need to add the path to some env variable. – Ayush Dec 08 '14 at 10:07

2 Answers2

4

What if you just change your working directory:

import os
cwd = os.getcwd() #current directory
os.chdir('path/to/jar')
... # run file
...
os.chdir(cwd)
Dashing Adam Hughes
  • 1,522
  • 1
  • 13
  • 12
  • That works fine! But I don't want to use it that way because there might be a case when it has to share some data from/at both locations. I wanted to add jar file path to python path as a permanent path, so that I would be able to access any of those paths. – Ayush Dec 08 '14 at 06:11
2

sys.path and PYTHONPATH are used when importing python modules

When executing commands, the operating system lookup the command in its system path (%PATH% on Windows).

There is no lookup path for data / filenames passed as argument.

When using sp.call() the system path lookup uses whatever directory the script has been launched from. So you need to either change dir to E:\DATA or use the absolute path:

sp.call(['java', '-jar', 'E:\DATA\file.jar'], env=env)

There are plenty of env variable on Windows: https://en.wikipedia.org/wiki/Environment_variable#Default_values

  • I can use absolute path in place of 'file.jar' but there are some support files as well which are required in order to execute file.jar That's why I need to add the path to some env variable. Providing complete path to just jar doesn't work. – Ayush Dec 08 '14 at 10:08
  • 1
    Then that E:\DATA\ can be a class attribute which you reuse everywhere. Or you could `os.chdir('E:\DATA\')` as suggested by Dashing Adam Hughes, thus file operations will be relative to E:\DATA\. – Antoine 'hashar' Musso Dec 09 '14 at 11:21
  • yeah, had to use os.chdir() – Ayush Dec 10 '14 at 06:13