2

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 know 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.

Dave Lewis
  • 126
  • 3
  • 3
  • 12
  • 3
    You shouldn't use backslashes in paths. Forward slashes work on all systems while backslashes only work on windows. Besides that, `subprocess` has some nice functions taking command and arguments through an array so you don't have to take are of quoting etc. – ThiefMaster Jul 06 '11 at 12:06
  • 2
    I highly doubt that your two paths are identical. Here is a similar question: http://stackoverflow.com/questions/204017/how-do-i-execute-a-program-from-python-os-system-fails-due-to-spaces-in-path – Jacob Jul 06 '11 at 12:08
  • Do you mean you actually renamed "Documents and Settings" on your system? Or did you just put _ instead of space in the call? I suggest trying to put the name of the jar in escaped quotes: "java -jar \"path to jar\" %s". – Nodebody Jul 06 '11 at 12:10
  • @ThiefMaster; the program is only going to be used on Windows, and for some reason it doesn't like forward slashes. Don't ask me why, I don't know, but another part of my program gives me error messages if I use forward slashes. Also, `subprocess` isn't the best solution, for this program, I did try it and it couldn't send the variable fileNames properly. – Dave Lewis Jul 06 '11 at 12:13
  • @Cularis; saw that, tried some of the solutions, still gives me the "Unable to access" error, which is different to any of those anyway. Maybe it isn't the filepath, but if it isn't, I have no idea what is causing it. @Nodebody; The second one. Your solution is similar to cularis' and still doesn't work. – Dave Lewis Jul 06 '11 at 12:15
  • This http://stackoverflow.com/questions/5569591/unable-to-access-jarfile-in-linux-land problem seems more like my problem, but for linux rather than windows. Though, as I've said, I do have full access rights to it so... Hmm. – Dave Lewis Jul 06 '11 at 12:57

4 Answers4

1

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.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
0

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.
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • Its definitely not a typo. I'm beginning to think it isn't actually the filepath causing the error, but then I don't know what it is. – Dave Lewis Jul 06 '11 at 12:19
0

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)

S.Lott
  • 384,516
  • 81
  • 508
  • 779
slaphappy
  • 6,894
  • 3
  • 34
  • 59
0

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.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164