本文整理汇总了Java中org.zeroturnaround.exec.InvalidExitValueException类的典型用法代码示例。如果您正苦于以下问题:Java InvalidExitValueException类的具体用法?Java InvalidExitValueException怎么用?Java InvalidExitValueException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
InvalidExitValueException类属于org.zeroturnaround.exec包,在下文中一共展示了InvalidExitValueException类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
/**
* Executes this command.
* @param timeout The allowed timeout
* @param unit A time unit
* @return This command
* @throws TimeoutException See {@link ProcessExecutor#execute()}
* @throws InvalidExitValueException See {@link ProcessExecutor#execute()}
* @throws IOException See {@link ProcessExecutor#execute()}
* @throws InterruptedException See {@link ProcessExecutor#execute()}
*/
public Command execute(long timeout, TimeUnit unit)
throws TimeoutException, InvalidExitValueException, IOException,
InterruptedException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
final int exitCode = new ProcessExecutor()
.environment(System.getenv())
.directory(this.directory)
.command(this.parts)
.timeout(timeout, unit)
.redirectOutput(outputStream)
.redirectError(errorStream)
.readOutput(true)
.stopper((process) -> {
process.destroyForcibly();
})
.execute()
.getExitValue();
this.result = new Result(
exitCode,
outputStream,
errorStream
);
return this;
}
示例2: taskkill
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
/**
* Sends the destroy signal to this process.
*
* @param forceful <code>true</code> if this process should be destroyed forcefully.
* @return <code>true</code> if this process got the signal, <code>false</code> if the process was not found (any more).
*
* @throws IOException on IO error.
* @throws InterruptedException if interrupted.
*/
public boolean taskkill(boolean forceful) throws IOException, InterruptedException {
try {
new ProcessExecutor()
.commandSplit(String.format("taskkill%s%s /PID %d", includeChildren ? " /T" : "", forceful ? " /F" : "", pid))
.redirectOutput(Slf4jStream.ofCaller().asDebug()).exitValueNormal().executeNoTimeout();
return true;
}
catch (InvalidExitValueException e) {
if (e.getExitValue() == EXIT_CODE_COULD_NOT_BE_TERMINATED) {
// Process could be either alive or not, if it's not alive we don't want to throw an exception
if (isAlive()) {
throw e; // process is still alive
}
return false; // process is stopped but but not because of us
}
if (e.getExitValue() == EXIT_CODE_NO_SUCH_PROCESS) {
return false;
}
throw e;
}
}
示例3: isAlive
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
public boolean isAlive() throws IOException, InterruptedException {
try {
new ProcessExecutor()
.commandSplit(String.format("kill -0 %d", pid)).readOutput(true)
.redirectOutput(Slf4jStream.ofCaller().asTrace())
.setMessageLogger(MessageLoggers.TRACE)
.exitValueNormal()
.executeNoTimeout();
return true;
}
catch (InvalidExitValueException e) {
if (isNoSuchProcess(e)) {
return false;
}
throw e;
}
}
示例4: runShellCommand
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
/**
* Run a shell command synchronously.
*
* @param command command to run and arguments
* @return the stdout output of the command
*/
public static String runShellCommand(String... command) {
String joinedCommand = String.join(" ", command);
LOGGER.debug("Executing shell command: `{}`", joinedCommand);
try {
ProcessResult result;
result = new ProcessExecutor()
.command(command)
.readOutput(true)
.exitValueNormal()
.execute();
return result.outputUTF8().trim();
} catch (IOException | InterruptedException | TimeoutException | InvalidExitValueException e) {
throw new ShellCommandException("Exception when executing " + joinedCommand, e);
}
}
示例5: kill
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
/**
* Sends a signal to this process.
*
* @param signal name of the signal.
* @return <code>true</code> if this process received the signal, <code>false</code> if this process was not found (any more).
*
* @throws IOException on IO error.
* @throws InterruptedException if interrupted.
*/
public boolean kill(String signal) throws IOException, InterruptedException {
try {
new ProcessExecutor()
.commandSplit(String.format("kill -%s %d", signal, pid))
.redirectOutput(Slf4jStream.ofCaller().asDebug()).exitValueNormal().executeNoTimeout();
return true;
}
catch (InvalidExitValueException e) {
if (isNoSuchProcess(e)) {
return false;
}
throw e;
}
}
示例6: isNoSuchProcess
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
@Override
protected boolean isNoSuchProcess(InvalidExitValueException e) {
return e.getExitValue() == EXIT_CODE_NO_SUCH_PROCESS;
}
示例7: testJavaVersionExitValueCheck
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
@Test(expected=InvalidExitValueException.class)
public void testJavaVersionExitValueCheck() throws Exception {
new ProcessExecutor().command("java", "-version").exitValues(3).execute();
}
示例8: testJavaVersionExitValueCheckTimeout
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
@Test(expected=InvalidExitValueException.class)
public void testJavaVersionExitValueCheckTimeout() throws Exception {
new ProcessExecutor().command("java", "-version").exitValues(3).timeout(60, TimeUnit.SECONDS).execute();
}
示例9: testCustomExitValueInvalid
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
@Test(expected=InvalidExitValueException.class)
public void testCustomExitValueInvalid() throws Exception {
new ProcessExecutor(exitLikeABoss(17)).exitValues(15).execute();
}
示例10: isNoSuchProcess
import org.zeroturnaround.exec.InvalidExitValueException; //导入依赖的package包/类
/**
* @param e process exited with an error code.
* @return <code>true</code> if this exception indicates that the process was not found (any more).
*/
protected boolean isNoSuchProcess(InvalidExitValueException e) {
return e.getExitValue() == EXIT_CODE_NO_SUCH_PROCESS;
}