apache-commons-exec

How to execute /bin/sh with commons-exec?

假如想象 提交于 2019-12-24 03:34:06
问题 This is what I'm doing: import org.apache.commons.exec.*; String cmd = "/bin/sh -c \"echo test\""; new DefaultExecutor().execute(CommandLine.parse(cmd)); This is the output: /bin/sh: echo test: command not found What am I doing wrong? 回答1: This one works for me:- CommandLine command = new CommandLine("/bin/sh"); command.addArguments(new String[] { "-c", "echo 'test'" },false); new DefaultExecutor().execute(command); 回答2: According to the FAQ "It is recommended to use CommandLine.addArgument()

How to run interactive python script using apache common exec?

感情迁移 提交于 2019-12-24 02:25:24
问题 I am trying to run a python script using Apache Commons exec.I need to pass some values to python script as python script in an interactive one.How to do it? My attempt was to set values in parent process's input stream.But it's not working for me. My code so far: String line = "python /home/abhijeet/test.py"; CommandLine cmdLine = CommandLine.parse(line); byte buf[]="4".getBytes(); InputStream io=new ByteArrayInputStream(buf); DefaultExecuteResultHandler resultHandler = new

Streaming output with commons-exec?

谁都会走 提交于 2019-12-23 12:26:05
问题 Can anyone give me an example of how to stream the output of an external program executed with DefaultExecutor ? I'm not finding any documentation describing how to do this. My external process will run for several hours, so just grabbing all output data isn't feasible; it must be streamed. 回答1: Note: this solution is synchronous, so it won't stream. You'll need to read is in a separate thread, or use the asynchronous version of the execute command. private InputStream getStream() { String

commons-exec: Executing a program on the system PATH?

↘锁芯ラ 提交于 2019-12-23 11:49:34
问题 I'm trying to execute a program (convert from ImageMagick, to be specific) whose parent folder exists on the path. Ergo, when I run convert from the command line, it runs the command. The following, however, fails: String command = "convert" CommandLine commandLine = CommandLine.parse(command); commandLine.addArgument(...) ... int exitValue = executor.execute(commandLine); If I specify the full path of the convert executable ( C:\Program files\... ) then this code works. If I don't do this, I

Graceful kill of Apache Commons Exec process

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:56:34
问题 I am starting an external process in my Java program (on Linux) and I need the ability to send it a SIGTERM signal rather than the SIGKILL that exec.getWatchdog().destroyProcess() is sending. Is there a way that I can more gracefully stop a unix process started with commons-exec? Or can I get the PID so that I can just run the appropriate kill command myself? 回答1: Well, Commons Exec relies on the Java Process class, which doesn't expose a PID. It's also what is used to kill the process, so it

“Kill a process tree” on windows using Java

倾然丶 夕夏残阳落幕 提交于 2019-12-19 08:02:55
问题 I have a Java webstart process that is part of a windows batch script. I'm using the javaws command in a batch script in this case. This match script ( start.bat) is invoked programatically using the "apache commons exec". Under some conditions the java process invoked by javaws hangs and I'd have to kill the entire process thread starting from the batch script start.bat. Is there a programatic way of doing killing an entire process tree through apache commons exec? I've tried using the

“Kill a process tree” on windows using Java

此生再无相见时 提交于 2019-12-19 08:02:01
问题 I have a Java webstart process that is part of a windows batch script. I'm using the javaws command in a batch script in this case. This match script ( start.bat) is invoked programatically using the "apache commons exec". Under some conditions the java process invoked by javaws hangs and I'd have to kill the entire process thread starting from the batch script start.bat. Is there a programatic way of doing killing an entire process tree through apache commons exec? I've tried using the

How to pipe a string argument to an executable launched with Apache Commons Exec?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 04:02:19
问题 I need to pipe a text argument to the stdin of a command launched with Apache Commons Exec (for the curious, the command is gpg and the argument is the passphrase to the keystore; gpg does not have an argument to provide the passphrase explicitly, only to accept it from stdin). In addition, I need this to support both Linux and Windows. In a shell script I'd do cat mypassphrase|gpg --passphrase-fd or type mypassphrase|gpg --passphrase-fd but type doesn't work on Windows as it's not an

How to pipe a string argument to an executable launched with Apache Commons Exec?

本秂侑毒 提交于 2019-12-18 04:02:00
问题 I need to pipe a text argument to the stdin of a command launched with Apache Commons Exec (for the curious, the command is gpg and the argument is the passphrase to the keystore; gpg does not have an argument to provide the passphrase explicitly, only to accept it from stdin). In addition, I need this to support both Linux and Windows. In a shell script I'd do cat mypassphrase|gpg --passphrase-fd or type mypassphrase|gpg --passphrase-fd but type doesn't work on Windows as it's not an

Thread-safe way to call an external process (and grab its output stream) from inside an EJB

…衆ロ難τιáo~ 提交于 2019-12-13 08:07:52
问题 How do I call an external process in a thread safe way from inside an EJB? ProcessBuilder is not thread safe, as stated in the javadoc. Apache commons exec says nothing about thread-safety and I am not confident in Runtime.exec either. What's the proper way? let me add some code so people won't think I am abusing, this code sometimes works, sometimes not public int startTask(Logger logger, String expectPath, String expectScriptPath, long ticket) throws IOException { Runtime r = Runtime