Expiration of an interactive process

How can a Java application create a new interactive application (like a command line editor) from Java / Scala?

When I use Runtime.getRuntime().exec("vim test")

, I get an instance of the process, whereas it vim

will run in the background; and then it will appear to the user.

+2


a source to share


5 answers


You will have to write the input and output records using System.console()



You will need to manually redirect every input to the spawned process and every output to the user.

+4


a source


Assuming you are on Linux, you really need to run the command in a new terminal window



Runtime.getRuntime().exec(new String[]{"xterm", "-e", "vim test"});

      

0


a source


mmmmm. Try the following:

scala.sys.Process("vim test").run(true)

      

Oh, that only on the Scala trunk - maybe Scala 2.9.

0


a source


Note. Use this method only when you absolutely need a terminal / console, such as vim. For all other uses, use Runtime.exec ().

I had the same problem. To work, I used Java Native Interface to call a small c function that makes a system call:

SystemProcess.java:

class SystemProcess {
  public native int systemCall(String cmd);
  public boolean execute() { return systemCall("vim test") == 0; }
  static { System.loadLibrary("SystemProcess"); }
}

      

SystemProcess.c

#include [javah-generated-header.h]
[javah-generated-function-prototype] (JNIEnv *env, jobject obj, jstring jcmd) {
  jboolean iscopy;
  const char *cmd = (*env)->GetStringUTFChars(env, jcmd, &iscopy);
  jint ec = system(cmd);
  (*env)->ReleaseStringUTFChars(env, jcmd, cmd);
  return ec;
}

      

I'll leave it all to actually do all the header generation and c compilation into a shared library. Feel free to replace the fork / exec or posix_spawn system if you get better. But for the most part it should work like on unix / linux.

0


a source


In 1.7 it's pretty straight forward. (Of course everything when you've solved it before)

ProcessBuilder builder = new ProcessBuilder("/usr/bin/vim");
Map<String, String> environ = builder.environment();
builder.redirectInput(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
final Process process = builder.start();
process.waitFor();

      

0


a source







All Articles