java python中的子进程调用以使用JAVA_OPTS调用java jar文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14762048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:19:33  来源:igfitidea点击:

subprocess call in python to invoke java jar files with JAVA_OPTS

javapython

提问by user1164061

Example code:

示例代码:

import subprocess
subprocess.call(['java', '-jar', 'temp.jar'])

How to specify the JAVA_OPTS in the above command? I am getting a 'java.lang.OutOfMemoryError: unable to create new native thread' when I use the above command and I think specifying JAVA_OPTS in the command would solve the problem.

如何在上面的命令中指定JAVA_OPTS?当我使用上述命令时,出现“java.lang.OutOfMemoryError:无法创建新的本机线程”,我认为在命令中指定 JAVA_OPTS 可以解决问题。

I did specify the JAVA_OPTS in .bashrc file and it had no effect.

我确实在 .bashrc 文件中指定了 JAVA_OPTS 并且它没有效果。

回答by andrewdotn

You can do this, but finding how to do it in the documentation is kind of a wild goose chase.

你可以这样做,但在文档中找到如何做是一种疯狂的追逐。

The subprocess.call()documentationsays,

subprocess.call()文件说,

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

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)上面显示的参数只是最常见的参数,在下面的常用参数中进行了描述(因此缩写签名中的符号略有奇怪)。

Then the Frequently Used Argumentssection, says, at the very end after describing a bunch of other arguments:

然后是常用参数部分,在描述了一堆其他参数后的最后说:

These options, along with all of the other options, are described in more detail in the Popenconstructor documentation.

这些选项以及所有其他选项在Popen构造函数文档中进行了更详细的描述。

Well then! The Popendocumentation gives the full signature:

好吧!该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)

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)

envis the one you want! However, if you just pass env={'JAVA_OPTS': 'foo'}, then that will override allenvironment variables, including stuff like CLASSPATH, which could break other things. So you probably want to use code like this to add a JAVA_OPTSenvironment variable for the new process execution, without setting it in the current process:

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)

回答by andersschuller

There is no need to use JAVA_OPTS - just pass in some more arguments to call(). For example:

无需使用 JAVA_OPTS - 只需将更多参数传递给call(). 例如:

import subprocess
subprocess.call(['java', '-jar', 'temp.jar', '-Xmx1024m', '-Xms256m'])