seaders
seaders

Reputation: 4106

Exec format error when calling Java from python with sh.Command

Calling

import subprocess
print subprocess.Popen(['java', '-version'])

Gives the error

OSError: [Errno 8] Exec format error

But that can be fixed by changing the above to,

print subprocess.Popen(['java', '-version'], executable='/bin/sh')

How can I fix sh.Command the same way?

import sh
print sh.Command('java').bake('-version')()

Because it then gives the exact same error,

OSError: [Errno 8] Exec format error

Upvotes: 0

Views: 1159

Answers (2)

seaders
seaders

Reputation: 4106

@nosklo's answer definitely does do what I asked, and I'll keep it as the correct answer, but that wasn't what my problem was.

Basically, as @nosklo hinted at on irc, this error comes when there's confusion between files, treating a binary like a script, or a script like a binary. So that points definitely to java on the build machine.

which java
/usr/bin/java
ls -la /usr/bin/java
/usr/bin/java -> java.new
cat /usr/bin/java
' java.old -Djava.awt.headless=true "$@" '
ls -la /usr/bin/java.old
/usr/bin/java.old -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

My Java, /usr/bin/java, was a symlink to an executable script, java.new file without a shebang, which forwarded everything to java.old, which was a symlink to the main Java...

Talk about convoluted, but hopefully this might help someone who can't take the easy out the was available to me.

Upvotes: 0

nosklo
nosklo

Reputation: 223142

Try this

if sys.platform == 'darwin':
    java = sh.Command('/bin/sh').bake('java')
else:
    java = sh.Command('java')

print java.bake('-version')()

Upvotes: 1

Related Questions