As part of my python-based program, I have to call a Java program - solved that perfectly with os.system(). It worked well, until I moved the entire thing from one directory to the other. I changed all the filepaths in the python code, and the java program - as best I can tell, I don't really understand java - relies on relative file paths, which weren't changed during the move. But now, the java program won't open. The command line appears then vanishes almost instantly, so I know that os.system() is working. It must be to do with the filepath I'm using in os.system, as when I change that back to the original filepath it works perfectly fine again. Code below:
os.system("java -jar C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
Where fileNames is a variable thingie passed to the java program as an argument, which I'm fairly sure isn't the problem. If I call the python program directly from cmd.exe, then it gives me back the error message "Unable to access Jarfile C:\Documents". I thought this might have to do with the spaces in the filepath, so I put underscores in:
os.system("java -jar C:\\Documents_and_Settings\\enginx_mgr.ISIS\\My_Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
And it gave me the same "Unable to access Jarfile" message, but this time with the full filepath. Trying os.path.exists()
on the filepath returns true, so python knows its a real filepath; I guess it must be the command line disagreeing with it, but I don't kno开发者_C百科w why. Any ideas?
Edit: Original filepath, if its of interest, was C:\Inetpub\ftproot\JPivSource\jpivc.jar
Edit 2: Its almost certainly not the filepath, going by the answers below and the fact that none of them work (and that the original filepath works). Checked the security options out of a hunch, and I have full control over the .jar file, as does the system, so its not that it can't access it for security reasons. Still at square zero.
Not a direct answer but ...
I think it is better to call .bat
file instead of direct call to java with many command line option. This way you will not need to change Python program to add some other options (like -Xms2048m
or -Dfile.encoding=utf8
).
Such .bat
file is also much easier to debug.
Your problem looks to be caused because of a typo somewhere. This should fix it:
- Open Windows Explorer
- right click on the file
- click "Properties"
- copy the location
- paste that location into your script, with directories escaped.
You have to put quotes around your path.
os.system('java -jar "C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar" %s' % fileNames)
I'm sorry for offering up another file path solution, but this wasn't mentioned, and it seems likely that the changing of paths would be causing the issue. So, if you wouldn't mind humouring me?
os.system("java -jar C:\\Documents\ and\ Settings\\enginx_mgr.ISIS\\My\ Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar %s"%fileNames)
All I've done differently, is escape the spaces with a backslash.
You said that os.path.exists
is returning True
, and that's fine, but you're trying to execute a command line program by passing it a number of arguments. The program reading the arguments will interpret the string as several strings because of the spaces.
You could also try altering the quotes that you are using:
os.system('java -jar "C:\\Documents and Settings\\enginx_mgr.ISIS\\My Documents\\ISAProgramFiles\\JPivSource\\jpivc.jar" %s' % fileNames)
That file path you're using does look quite strange.
精彩评论