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.
I did specify the JAVA_OPTS in .bashrc file and it had no effect.
You can do this, but finding how to do it in the documentation is kind of a wild goose chase.
The subprocess.call() documentation says,
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).
Then the Frequently Used Arguments section, 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 Popen constructor documentation.
Well then! The Popen documentation gives the full signature:
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 is the one you want! However, if you just pass env={'JAVA_OPTS': 'foo'}, then that will override all environment variables, including stuff like CLASSPATH, which could break other things. So you probably want to use code like this to add a JAVA_OPTS environment variable for the new process execution, without setting it in the current process:
#!/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)
There is no need to use JAVA_OPTS - just pass in some more arguments to call(). For example:
import subprocess
subprocess.call(['java', '-jar', 'temp.jar', '-Xmx1024m', '-Xms256m'])
Related
I had batch script which executes TestRun class by taking jvm arguments as shown below
java -cp "./lib/*;./statoil.jar" -DURI=localhost:8080 -DOWUser=abc -DOWPassword=abc123 -DpipelineName=EDMStatOil -Ddatabase=edm -DproviderName=141Provider -DdestinationName=110EDM -DproviderWellName=Serno Grad com.statoil.rts.test.TestRun
But while running batch script getting error:
Error: Could not find or load main class Grad
I know it is treating Grad as class file. But how we can avoid this error while passing jvm argument with space?
Java doesn't care if there is a space in the JVM argument's value, but the terminal will split -DproviderWellName=Serno Grad into two command line arguments and pass those to the java executable.
You have to put quotes around the whole argument:
java "-DproviderWellName=Serno Grad"
In you batch file try setting the variable first and then pass that parameter to the actual command like these.
set WellName="Serno Grad"
java -cp "./lib/*;./statoil.jar" -DURI=localhost:8080 -DOWUser=abc -DOWPassword=abc123 -DpipelineName=EDMStatOil -Ddatabase=edm -DproviderName=141Provider -DdestinationName=110EDM -DproviderWellName=%WellName% com.statoil.rts.test.TestRun
OR
set WellName="Serno Grad"
java -cp "./lib/*;./statoil.jar" -DURI=localhost:8080 -DOWUser=abc -DOWPassword=abc123 -DpipelineName=EDMStatOil -Ddatabase=edm -DproviderName=141Provider -DdestinationName=110EDM -DproviderWellName="%WellName%" com.statoil.rts.test.TestRun
On my system either of them works fine.
try with escape characters -DproviderWellName="\"Serno Grad\""
I am trying to use a python script to manipulate the input files for my java program. The way I am doing it is I am generating the file name and passing it to subprocess.call() method to execute. Here is my program:
def execJava(self):
self.thisCmd="pause"
call(self.javaCmd,shell=True)
call(self.pauseCmd,shell=True)
Where,
self.javaCmd = 'java -ea -esa -Xfuture -Xss64m -classpath "C:\FVSDK_9_1_1\lib\x86_64\msc_12.0-sse2_crtdll\*" -Djava.library.path="C:\FVSDK_9_1_1\lib\x86_64\msc_12.0-sse2_crtdll;C:\FVSDK_9_1_1\lib\x86_64\share" com.cognitec.jfrsdk.examples.MatchFIRAgainstGallery C:\FVSDK_9_1_1\etc\frsdk.cfg 0 .\tmp\frsdk-scratch\probe_1.fir .\tmp\test\*'
Yes, it's a long complex java instruction but when I run it in the command prompt it works fine. Only when I pass it as a string it doesn't run and returns:
Exception in thread "main" java.lang.Error
After some exploration into the problem, I have discovered that it is due to \x, \t in the instruction, so it is executing
.\tmp\test\*
as
mp est\*
as it replaces \t with tab space while executing. I have looked up quite a bit and didn't find any solution. Any help is much appreciated.
Use forward slashes "/" instead of back slashes "\" for your paths.
I'm running a jarball using
java -classpath myBatch.jar some.package.MyMainClass \
-bloodyArgument "I got the argument!" -Dbloody.prop="I got the prop!"
Then in my main I have:
Properties argsProps = BatchUtils.argsToProperties(args);
System.out.println(argsProps.getProperty("bloodyArgument"));
System.out.println(System.getProperty("bloody.prop"));
And I get output:
I got the argument!
null
I can see the command line bloodyArgument (I added it to see if "something" gets passed to the program), but I'd expect the -D argument to set the system property. Why is the "bloody.prop" null?
PS: BatchUtils.argsToProperties() does what you'd expected it to do: parse -argName "value" from command line into argName=value property pair.
Everything after the class argument of java command gets passed to your main in String[] args and doesn't make it to the JVM.
The solution was to rearrange the properties like this:
java -classpath myBatch.jar -Dbloody.prop="I got the prop!" \
some.package.MyMainClass -bloodyArgument "I got the argument!"
I didn't find this anywhere explicitly stated when waddling the web with http://duckduckgo.com or googling this queation of "null -D defined property". I figured it a lot later when printing all system properties and the whole arguments array, so am posting for others.
This should be a pretty simple question, I can't believe I wasn't able to find anything from googling.
I'm using powershell and I'm trying to run a java app from command line:
$memory = "-Xms128m -Xmx1028m -XX:MaxPermSize=512m"
$ssl = "-Djavax.rmi.ssl.client.enabledProtocols=`"TLSv1`" -Djavax.rmi.ssl.client.enabledCipherSuites=`"_removed_`" -Djavax.net.ssl.trustStorePassword=`"_removed_`" -Djavax.net.ssl.trustStore=`"_removed_`" -Djavax.net.ssl.keyStorePassword=`"_removed_`" -Djavax.net.ssl.keyStore=`"_removed_`" -Djava.endorsed.dirs=`"$($ddmsLoc)tomcat6\endorsed`""
$classpath = getClasspath "manager" $null
$java_opts = "$($memory) $($ssl) -Djavax.net.debug=all"
$cmd = "$($java) $($java_opts) -cp `"$($classpath)`" dss.vector.solutions.manager.server.ServerStatus -g"
Invoke-Expression $cmd
But for some reason it thinks my JAVA_OPTS parameters are the name of the java class I'm running:
Caused by: java.lang.ClassNotFoundException: .rmi.ssl.client.enabledProtocols=TLSv1
I have tried:
The ssl options with/without quotes around the value
Actually setting the JAVA_OPTS environment variable, until I read somewhere else that the JVM doesn't ever read that variable.
wrapping them in more quotes
I'm kind of at a loss here.
Etan Reisner posted a comment that helped me to solve it:
Why does PowerShell split arguments containing hyphens and periods?
Windows is (for some reason) splitting the parameters in half.
echo -Dmy.param=value
returns:
-Dmy
.param=value
If the parameter is wrapped in quotes, like:
echo "-Dmy.param=value"
Then it works just fine.
I apologize for the simplicity of this problem.
I'm trying to use a program called ChromHMM to analyze some biological data. I try and run the program according to the instructions but can't seem to enter the arguments correctly.
Here is an example:
E:\ComputationalAnalysis\ChromHMM>java -mx1600M -jar ChromHMM.jar BinarizeBed
chromosomelengthfile=\CHROMSIZES\hg19.txt inputbeddir=\Donor1 cellmarkfileta
ble=\Donor1\cellmarkfile.txt outputbinarydir=\firstoutput
It returns just this:
usage BinarizeBed [-b binsize][-c controldir][-center][-colfields chromosome,sta
rt,end[,strand]][-e offsetend][-f foldthresh][-n shift][-o outputcontroldir][-p
poissonthresh][-peaks][-s offsetstart][-strictthresh][-t outputsignaldir][-u pse
udocountcontrol][-w flankwidthcontrol] chromosomelengthfile inputbeddir cellmark
filetable outputbinarydir
In the manual it says the 4 arguments at the end are the only ones required to run. Does anyone know what I'm doing wrong? I'm trying to do this from the windows command prompt
try removing the name of the argument and the equals;just pass the value. so in your example you would have the following.
E:\ComputationalAnalysis\ChromHMM>java -mx1600M -jar ChromHMM.jar BinarizeBed
\CHROMSIZES\hg19.txt \Donor1 cellmarkfileta \Donor1\cellmarkfile.txt \firstoutput