Solved

VI/Notepad from java and interact with it

Posted on 2014-11-03
3
252 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 144
backtracking recursion  code 19 53
ArrayIndexOutOfBoundException 9 66
diffSum example 4 23
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now