jmap -histo:live output is empty - java

Sometimes when I execute a command:
"$JAVA_HOME/bin/jmap -histo:live " + pid + " > " + fileName
I get an empty(blank) file.
Why this may happen?
P.S. This is using in autotest (test on memory leaks).
With option -F (forced) I get histogram, but it includes non-live objects.
Thx, for any help.

Related

/bin/sh -c not working with process builder

I am trying to run the following command via process builder:
ls -lrt my-directory | tail -1
I am using bin/sh -c when passing it in but cannot get it to work any way I try.(my custom directory depends on the function and is passed in as a string)
process builder("/bin/sh", "-c", "ls -lrt ", customDir, " | tail -1");
This gives the contents of the directory I am running the program out of. It's as if I simply typed ls (not even ls -lrt).
Both of
String temp = "ls -lrt " + customDir + " | tail -1";
process builder("/bin/sh ", "-c " temp);
String temp = "ls -lrt " + customDir;
process builder("/bin/sh ", "-c " temp, " | tail -1");
say that the directory doesn't exist even though I know it does. Removing /bin/sh -c simply gives me an error saying it couldn't understand the ls -lrt command which I understand. But I simply have no idea how to get it to produce the actual results I would get if I typed this command in seperately.
edit: the problem is not because of how I am calling process builder. I send the content to another function which calls process builder properly and relays its output.For simplicity, I just said process builder (...) in the examples.

Execute command from java which might include spaces

I am trying to execute the following command from a java program:
java -jar /opt/plasma/fr.inria.plasmalab.plasmalab-1.3.4.jar -t -a montecarlo -A"Total samples"=1000 -m models/translated/plasma/NaCl2.rml:rml --format csv -r models/translated/plasma/NaCl2.bltl:bltl
with the following code:
String totalSample = "-A\"Total samples\"=1000";
String mcCommand = "java -jar " + MChecker.getAppPath() + " -t "
+ "-a " + "montecarlo " + totalSample
+ " -m " + mcModelRelPath + ":rml " + "--format " + "csv "
+ "-r " + customQueryRelPath + ":bltl";
Process process = Runtime.getRuntime().exec(mcCommand);
int errCode = process.waitFor();
//then get the output, and error
But it results in the following error:
Wrong parameter description : Dynamic parameter expected a value of the form a=b but got:"Total
I ran the same command in a terminal and it worked without any problem. But when I create the command in Java and try to invoke the tool it does not work.
I think it's confused because of the totalSample parameter which includes a space. What I did next was to put "\ " space escape in paramater(String totalSample = "-A\"Total\\ samples\"=1000";), but it still refused to accept it. It gave the following error:
Wrong parameter description : Dynamic parameter expected a value of the form a=b but got:"Total\
Then I run the same parameters with the ProcessBuilder object, like the following:
String[] mcCommand = {"java", "-jar", MChecker.getAppPath(), "-t",
"-a", "montecarlo",totalSample, "-m",
mcModelRelPath + ":rml", "--format", "csv", "-r",
customQueryRelPath + ":bltl" };
ProcessBuilder pb = new ProcessBuilder(mcCommand);
Process process = pb.start();
process.waitFor();
But it still did not work and threw some custom exceptions.
I am currently out of options -- do you have any idea why this command does not work with Java, when it works just fine from the terminal interface?
BTW: I ran the same code on Windows it worked perfectly, but I have to run this code on Ubuntu OS.
Many Thanks
It was weird that, Java Process and ProcessBuilder classes could not pass the parameters properly. I don't know why, but since I was able to execute the command from terminal. I decide to call the terminal first and then execute the command. Therefore, I changed my command as following
String mcCommand[] = {
"/bin/sh",
"-c",
"java -jar /opt/plasma/fr.inria.plasmalab.plasmalab-1.3.4.jar -t -a montecarlo "+totalSample+" -m models/translated/plasma/NaCl2.rml:rml --format csv -r models/translated/plasma/NaCl2.bltl:bltl" };
Despite it is platform dependant solution, currently it is ok for me.
Don't add extra quotes for Total samples when using ProcessBuilder:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", MChecker.getAppPath(), "-t",
"-a", "montecarlo", "-ATotal samples=1000",
"-m", mcModelRelPath + ":rml", "--format", "csv",
"-r", customQueryRelPath + ":bltl");
//...

Can't execute Shellscript successfully using getruntime.exec()

I have written a code to execute a script from java :
String wrapper_script=homedir+"/blast_distribute.sh "+" --seqs="+seqs+" --i="+formobj.getUpFile().getFileName()+" "+formobj.getSelected_program();
script_exec=Runtime.getRuntime().exec(wrapper_script);
This works perfect for me as command is executed successfully. Now I need to run this command as other user so I need to execute a command in a format like :
su username -c 'command'
SO I have edited above script_exec string as :
String wrapper_script1="su - "+username+" -c "+"'"+wrapper_script+"'";
I have printed wrapper_script1 which diplays:
su - abhijeet -c '/home/abhijeet//blast_distribute.sh --seqs=1562
--i=mPS_0.contigs.fasta'
If I run same command directly on Linux,It works exactly as I need.But When I am running through :
script_exec=Runtime.getRuntime().exec(wrapper_script1);
It does't work properly ,In my error stream I get error as
su: unrecognized option '--seqs=1562' Try `su --help' for more
information.
I have tried a lot but could't resolve the issue.What can be the reason for this issue?
You should use multiple parameters run for this:
Runtime.getRuntime().exec(new String[] {"su", "-", username, "-c",
homedir + "/blast_distribute.sh " + " --seqs=" + seqs + " --i=" + formobj.getUpFile().getFileName() + " " + formobj.getSelected_program()
});
You're getting the error, because ' is not treated here as a enclosing characters, but as a parameters for su command.
Let's look into details. When you type into console some command like su - jsmith -c 'aba --cabga', what you're doing is: "run command su with such an arguments: -, jsmith, -c, aba --caba (one argument for multiple words)".
The same thing you should do in your Java code. There's special command in Java API for running the concrete command with concrete arguments instead of parsing line as shell do: Runtime.exec(String[]): the first argument is command, the next are the arguments. That's what we do in this code block.

How is it possible to restart Tomcat in Linux from a webapp inside Tomcat?

I tried this:
Runtime.getRuntime().exec("/bin/bash -c " + System.getProperty("catalina.home") + "/logs/shutdown.sh && " + System.getProperty("catalina.home") + "/bin/startup.sh");
But only shutdown worked properly, Tomcat didn't startup properly, I think the problem was that the previous instance still used port 8080. I added a sleep 2m between the two commands (seperated with &&), but in this case, shutdown occured, but nothing happened later according to catalina log, no error, startup wasn't executed at all.
(First time I tried without /bin/bash -c, but in that case nothing happened.)
Never use Runtime.getRuntime().exec(String). It doesn't work the way people expect. Use the version that accepts a String[] instead.
Also, run it in the background and redirect stdout/stderr, otherwise it will block waiting for the java process to read its input.
Runtime.getRuntime().exec(new String[] {
"/bin/bash", "-c",
"(" +
System.getProperty("catalina.home") + "/bin/shutdown.sh && " +
System.getProperty("catalina.home") + "/bin/startup.sh" +
") > ~/restart.log 2>&1 &"
});
Here it writes to ~/restart.log, so that if it fails, you can see the error messages. You can replace this with /dev/null when it works.
I would strongly recommend creating a new script restart_tomcat that does all this backgrounding and redirection, so that you can reduce this to
Runtime.getRuntime().exec(new String[] { "/path/to/restart_tomcat" });
It's easier and more flexible to work with a script on the file system than a script embedded in Java code.

Using Java's ProcessBuilder to run SoX

I am running SoX from java using a ProcessBuilder to trim a wav file. I am sure I should be able to run SoX, cause in the other JUnit tests, I manage to successfully run the following commands:
sox/sox --version
sox/sox --i -r test/test.wav
sox/sox --i -D test/test.wav
sox/sox --i -b test/test.wav
sox/sox --i -c test/test.wav
but when I try to trim a file as in the following:
sox/sox -V3 "/Users/username/workspace/Thesis Corpus Integrator/test/test.wav" -b 16 "/Users/username/workspace/Thesis Corpus Integrator/test/newWaveFile.wav" channels 1 trim 0:00:00.000 =0:00:30.000
it throws an IOException with the error: error=2, No such file or directory. I tried running the command on a terminal, and it worked without a problem. If it matters, I ran it through a JUnit test from eclipse, on a macbook.
Here's the code I used to build it in ProcessBuilder:
StringBuilder command = new StringBuilder(soxCommand) // soxCommand resolves to sox/sox, and is used in all the other tests without any problems
if (WavCutter.getMetadata(srcFile.getAbsolutePath(),
MetadataField.SAMPLE_RATE) != 16000) {
command.append(" -V3");
command.append(" -G");
command.append(" \"" + srcFile.getAbsolutePath() + '\"');
command.append(" -b 16");
command.append(" \"" + destFile.getAbsolutePath() + '\"');
command.append(" channels 1");
command.append(" gain -h");
command.append(" rate 16000");
command.append(" trim");
command.append(" " + startTime.toString());
command.append(" " + '=' + endTime.toString());
Process soxProcess = new ProcessBuilder(command.toString())
.start();
I also tried the same thing, but using an ArrayList.
Found the answer myself, with lots of help from bramp's comment. The problem is easily resolved by first using a List of Strings, and then by separating the non-dash prefixed arguments that require spaces, like sox's effects.
So from something like:
StringBuilder s = new StringBuilder("sox/sox"); // command itself is 'sox'
// everything after this is an argument
s.add(srcFile.getPath());
s.add("-b 16");
s.add(destFile.getPath());
s.add("rate 16000");
s.add("channels 1");
you get:
ArrayList<String> s = new ArrayList<String>();
s.add("sox/sox"); // command/process
// everything after this is an argument
s.add(srcFile.getPath());
s.add("-b 16");
s.add(destFile.getPath());
s.add("rate"); // notice how I had to split up the rate argument
s.add("16000");
s.add("channels"); // same applies here
s.add("1");
I think it may have something to do with how Java sends the arguments, or how SoX receives arguments. I was able to replicate my problem in the terminal by using the following command:
sox/sox test/test.wav -b 16 test/newtest.wav "rate 16000" "channels 1"

Categories

Resources