1

I'm trying to write a small Java command-line application that will create a new file, and then open it with the systems default editor stored in $EDITOR, and then exit after the editor is closed.

So far, without luck, I've tried the following:

Desktop dt = Desktop.getDesktop();
dt.edit(file);

This method resulted in an UnsupportedOperationException, which sort of makes sense as I'm running my program from the terminal, not as a Java appliacation from the desktop.

Right now, I have this:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(commandString); // "vim newfile"
proc.waitFor();

This is working, but not how I need it to. When I run

ps a | grep vim

I can see that it is indeed running in the background, with the filename I've given it:

1000 pts/1    S+     0:00 vim 2014-07-16.23-02

Any ideas on how to make this run in the foreground?

1 Answer 1

1

vim, like many interactive programs, expects its stdin to be a real terminal that it can send ioctl calls to. But when executing through Runtime.exec() stdin will be redirected to the parent process (see the Javadoc on Process for more information).

In Java 7, you should be able to use ProcessBuilder.inheritIO() to pass along the file handles. (Disclaimer: I haven't tried it, YMMV.)

Sign up to request clarification or add additional context in comments.

2 Comments

I was hoping to find a Java 6 solution, but it looks like Java 7 is necessary to make this work.
@grimetime: if you're stuck on Java 6, you could always use JNI to do a fork() in native code. Of course, then you have to deal with the pain of pulling in a platform-specific JNI dependency.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.