Solved

VI/Notepad from java and interact with it

Posted on 2014-11-03
3
253 Views
Last Modified: 2014-11-09
Hi there,
I have a console program built using the deadline clone, JLine and need to call a full editor from it to make editing easier.  The java program will have to call 'vi' or VIM or notepad on windows and allow the user to interact with it. Then when it is closed, the process would end and control return to the java console program.

Currently, most of the research I see is based around Runtime.exec and calling Process.waitfor for it to return.  
Now, this runs, but the terminal window is not showing vi and is blocking.

Has anyone called an external program from java and have it take control of the console like this?  I want to be able to do this from a prompt.  Assume 'prompt>' is my command line running jline.

prompt> edit

here I can capture the command edit from JLine and process it to call
    Process p = Runtime.getruntime().exec("vi blah.txt");

then I can call

   p.waitFor()

which should block until my vi session comes back.  And, while in vi, I can do all the normal things, before saving and coming back to my java console session.

 Now, my question is this, Does Runtime work in this situation? Can I control the terminal like this?  If not, I'm open to suggestions to something that will work.
0
Comment
Question by:BarryMcGillin
  • 2
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40420176
I think it's going to be very difficult to pass control to a console editor (like basic vi) and then back to your app. A gui editor (like Notepad) would of course be easier
0
 
LVL 2

Accepted Solution

by:
BarryMcGillin earned 0 total points
ID: 40421267
Leave it long enough and you figure it out.  Redirecting the input and output sorted it out.  Simply put, this will do it.

    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
 
LVL 2

Author Closing Comment

by:BarryMcGillin
ID: 40431056
Didn't get a solution and ended up finding something that worked for me in javadoc released on 1.7 and 1.8
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question