Completed demo.

This commit is contained in:
Christian P. MOMON 2017-05-10 09:56:41 +02:00
parent d0d0837441
commit 6595d1eb30
2 changed files with 71 additions and 16 deletions

View file

@ -18,7 +18,18 @@
*/ */
package fr.devinsy.util.cmdexec.demo; package fr.devinsy.util.cmdexec.demo;
import fr.devinsy.util.strings.StringList; import java.io.File;
import java.io.IOException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.util.cmdexec.CmdExec;
import fr.devinsy.util.cmdexec.CmdExecException;
import fr.devinsy.util.cmdexec.CmdExecUtils;
import fr.devinsy.util.cmdexec.StreamGobbler;
/** /**
* The Class Demo. * The Class Demo.
@ -30,27 +41,71 @@ public class CmdExecDemo
* *
* @param args * @param args
* the arguments * the arguments
* @throws CmdExecException
* @throws IOException
*/ */
public static void main(final String[] args) public static void main(final String[] args) throws CmdExecException, IOException
{ {
// #1 //
BasicConfigurator.configure();
org.apache.log4j.Logger.getRootLogger().setLevel(Level.ERROR);
Logger logger = LoggerFactory.getLogger(CmdExecDemo.class);
//
File tmpFile = null;
try
{ {
StringList strings = new StringList(); tmpFile = File.createTempFile("cmdexec", ".tmp");
strings.appendln("========== DEMO #1;"); // #0
strings.appendln(); {
System.out.println("========== DEMO #0;");
System.out.println(strings.toString()); System.out.println("Launch…");
String result = CmdExecUtils.run("/bin/bash", "-c", "echo '10\n5\n7\n1' > " + tmpFile.getAbsolutePath());
System.out.println("result=[" + result + "]");
}
// #1
{
System.out.println("========== DEMO #1;");
System.out.println("Launch…");
String[] command = { "/bin/cat", tmpFile.getAbsolutePath() };
CmdExec cmdexec = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
System.out.println("command=[" + cmdexec.toString() + "]");
System.out.println("exitVal=[" + cmdexec.getExitValue() + "]");
System.out.println("out=[" + cmdexec.getOutStream() + "]");
System.out.println("err=[" + cmdexec.getErrStream() + "]");
}
// #2
{
System.out.println("========== DEMO #2;");
System.out.println("Launch…");
// String command = "/bin/sort -r /etc/passwd";
String[] command = { "/usr/bin/sort", "-n", "-r", tmpFile.getAbsolutePath() };
CmdExec cmdexec = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
System.out.println("command=[" + cmdexec.toString() + "]");
System.out.println("exitVal=[" + cmdexec.getExitValue() + "]");
System.out.println("out=[" + cmdexec.getOutStream() + "]");
System.out.println("err=[" + cmdexec.getErrStream() + "]");
}
} }
finally
// #2
{ {
StringList strings = new StringList(); if (tmpFile != null)
{
strings.appendln("========== DEMO #2;"); boolean status = tmpFile.delete();
strings.appendln(); logger.debug("file delete status={}", status);
}
System.out.println(strings.toString());
} }
} }
} }

View file

@ -100,7 +100,7 @@ class CmdExecSandbox
{ {
try try
{ {
System.out.println("Launch…s"); System.out.println("Launch…");
// String command = "/bin/sort -r /etc/passwd"; // String command = "/bin/sort -r /etc/passwd";
String[] command = { "/usr/bin/sort", "-r", "/etc/passwd" }; String[] command = { "/usr/bin/sort", "-r", "/etc/passwd" };