示例代码:
import subprocess
subprocess.call(['java', '-jar', 'temp.jar'])
如何在上面的命令中指定JAVA_OPTS?当我使用上述命令时出现“java.lang.OutOfMemoryError: unable to create new native thread”,我认为在命令中指定 JAVA_OPTS 可以解决问题。
我确实在 .bashrc 文件中指定了 JAVA_OPTS,但没有效果。
最佳答案
您可以做到这一点,但在文档中找到如何做到这一点有点像徒劳的追逐。
subprocess.call()
documentation说,
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
The arguments shown above are merely the most common ones, described below in Frequently Used Arguments (hence the slightly odd notation in the abbreviated signature).
然后 Frequently Used Arguments部分,在描述了一堆其他论点之后最后说:
These options, along with all of the other options, are described in more detail in the
Popen
constructor documentation.
那好吧! Popen
文档给出了完整的签名:
class subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
env
就是你想要的!但是,如果您只是传递 env={'JAVA_OPTS': 'foo'}
,那么这将覆盖所有 环境变量,包括 CLASSPATH
,这可能会破坏其他东西。所以你可能想使用这样的代码为新进程的执行添加一个 JAVA_OPTS
环境变量,而不是在当前进程中设置它:
#!/usr/bin/env python2.7
import os
import subprocess
# Make a copy of the environment
env = dict(os.environ)
env['JAVA_OPTS'] = 'foo'
subprocess.call(['java', '-jar', 'temp.jar'], env=env)
关于java - python中的子进程调用以使用JAVA_OPTS调用java jar文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14762048/