Using os.execvp in Python

I have a question about using os.execvp in Python. I have the following bit of code that's used to create a list of arguments:

args = [ "java"
       , classpath
       , "-Djava.library.path=" + lib_path()
       , ea
       , "-Xmx1000m"
       , "-server"
       , "code_swarm"
       , params
       ]

When I output a string using " ".join(args) and paste that into my shell prompt, the JVM launches fine, and everything works. Everything works if I use os.system(" ".join(args)) in my Python script, too.

But the following bit of code does not work:

os.execvp("java", args)

I get the following error:

Unrecognized option: -classpath [and then the classpath I created, which looks okay]
Could not create the Java virtual machine.

So what gives? Why does copying/pasting into the shell or using os.system() work, but not os.execvp()?


Asked by: Maria672 | Posted: 05-10-2021






Answer 1

If your "classpath" variable contains for instance "-classpath foo.jar", it will not work, since it is thinking the option name is "-classpath foo.jar". Split it in two arguments: [..., "-classpath", classpath, ...].

The other ways (copy and paste and system()) work because the shell splits the command line at the spaces (unless they are escaped or quoted). The command line is in fact passed down to the called program as an array (unlike on Windows), and the JVM is expecting to find an element with only "-classpath" followed by another element with the classpath.

You can see the difference for yourself by calling the following small Python script instead of the JVM:

#!/usr/bin/python
import sys
print sys.argv

Answered by: Julian731 | Posted: 06-11-2021



Answer 2

Make sure you aren't relying on shell expansion in your classpath. E.g. "~/my.jar" will get expanded by the shell in an os.system call, but not, I believe in an os.execvp call.

Answered by: Sawyer505 | Posted: 06-11-2021



Similar questions

python - Why does os.execvp block on apache but not on the shell?

I'm looking for a way, in plain python (no extra libraries) to kick off a background process that will run after the web page returns. In the shell, this script below exits, and then prints "Hello World". However, in the browser, it blocks until two.py is finished. #!/usr/bin/python import os import sys def run(): program = "python" pid = os.fork() if not pid: os.execvp(program,...


python - Infinite while not working with os.execvp

I am programming in python which involves me implementing a shell in Python in Linux. I am trying to run standard unix commands by using os.execvp(). I need to keep asking the user for commands so I have used an infinite while loop. However, the infinite while loop doesn't work. I have tried searching online but they're isn't much available for Python. Any help would be appreciated. Thanks This is the code I have w...


python - Why does os.execvp block on apache but not on the shell?

I'm looking for a way, in plain python (no extra libraries) to kick off a background process that will run after the web page returns. In the shell, this script below exits, and then prints "Hello World". However, in the browser, it blocks until two.py is finished. #!/usr/bin/python import os import sys def run(): program = "python" pid = os.fork() if not pid: os.execvp(program,...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top