I want to implement a command line interface within the Java GUI window. Is there a good way of doing this or an existing project/example I could use?
My application visualises a graph in the GUI and I would like to enter verbal commands for editing it, instead of developing a point-and-click interface for every possible command. It would be similar to a normal command line but I would have a custom parser that analyses the entered command strings.
You can write your command and press enter, upon which the string is sent to a listener for parsing and execution.
Show a scrollable history of previous commands.
After parsing I might want to print something onto the screen (which would go to the history area).
Perhaps a command-line-style prefix (">").
Ability to edit the current command (backspace) before submitting it (enter). Not being able to delete the prefix or previous commands in the history.
Up and down keys would allow navigating and repeating previous commands.
You have the Groovy Console. See the paragraph Embedding the Console for your app swing.
Related
I'm working on a project in which I intend to make a Java GUI application that connects to a ssh server and executes remote commands on the server. I'm willing to use JSch Library. My aim is to make buttons and textfields those will provide the user the ability of sending commands and getting replies easily. I mean, instead of opening xShell and prompting "grep "hi" /usr/file.txt", the user will choose the path from the list and will enter "hi" into the textfield and will press the button for grep.
Problem is, I couldn't find a solution to execute multiple linux commands in one session (I don't want shell if i cannot redirect its input and output streams) (also I don't want the solution "cd.. \n dir \n ls -l" which works fine but not solving my problem) send the arguments those shall be taken from related GUI components.
Since I have not made so much modifications on the JSch's example code, yet, you can see the code here: http://www.jcraft.com/jsch/examples/Exec.java.html
Thanks from now on.
If using exec type of channel you can combine commands with && :
channel.setCommand(". ./.profile && env");
Help me understand what is being asked here. The question instructs me to use the command window to display the following statement about comments:
"Program comments are nonexecuting statements you add to a file for the purpose of documentation."
Also include the same statement in three different comments in the class; each comment should use one of the three different methods of including comments in a Java class.
What is the "command window"? Is it the window that appears when I press run? If so wouldn't comments not display at all.
Yes, depending on your IDE, the command window is the prompt which you can start (or stop the program with), receive output and enter input into the program. If you are using the Windows CMD software, you generally will use javac [filename].java to compile and java [filename] to run the code.
Inside your Java code, each line is run as a meaningful statement causing the program to do something. However comments are used to ignore lines in your program, such as messages for yourself or documentation to note to others who may view the program, how to use it.
Examples of comments are (single line): \\ This line will NOT be run
and (block):
/*
Lots of text can go here
This will not be run at all.
Not even the third line.
*/
However you are asked to display a line and that can be done with the print (or println) command from the out subclass of System class.
This would look like:
System.out.print("Program comments are nonexecuting statements you add to a file for the purpose of documentation.");
And that way the string of text,
Program comments are nonexecuting statements you add to a file for the purpose of documentation.
is printed into the commandline.
Microsoft Windows Command Prompt Window is what is meant there.
http://windows.microsoft.com/en-us/windows-vista/open-a-command-prompt-window
I tried to find a solution for the following use case (on Linux):
Start the program, show some information to the stdout, input some information such as username/password.
The program validate the username/password, then goes to background and run as a daemon.
I did not find a way to do this in Java. There are several sulotions to daemonize java program (such as jsvc, or this: http://barelyenough.org/blog/2005/03/java-daemon/ ). But seems they all do not work for this situation, because the program just goes to background from the beginning, there is no chance to input information before it goes to background.
I don't believe that there's a way to do this purely in java. You could make it work by writing an init script which accepted the command line parameters before spawning the java process in the background. You could use -D command line arguments to pass the user input to the java process.
I'm trying to build a simple text editor using Scala's swing library and I have to support two side by side windows. I was wondering if it was possible to have the second window be the terminal (bash, Unix). I haven't been able to find any information on the subject. Thank you for any information.
The question is: What is "the terminal"? bash is a shell but you need an implementation of a terminal which runs a certain shell. In general, I would say there are two possibilities:
Find a terminal implementation, which can be used directly in Swing. Maybe you this or that may help.
Implement your own terminal. You may want to start to wrap the shell with a ProcessBuilder. Now you can redirect standard input and output of this process so that you can control it programmatically (more information: here and there). Then you have to create the UI part which (1) reads input from the user and (2) displays the shell output in your window.
I wrote a Java application which has a while(input.readLine()) which continues reading input until the user types a quit command. I have a .bat script to launch it in Windows and a .sh to launch it in Linux. The application works fine in both.
While in Windows, if you type a command like "check email" and hit enter, it will perform the command. Then, at the next prompt (that is: the next time the Java application reads a line of input) you can hit the up arrow and it recalls your "check email" command. You could press the left arrow key to move your cursor left, etc. This is all exactly how I want it.
On Linux, however, pressing the up key causes ^[[D to appear. The left and right arrow keys produce similar ASCII output, like ^[[C.
I've heard of stty and viewed the man pages, but cannot figure out what I need to change in the .sh script with launches my Java application. Can anyone help me?
The simplest solution is to use an external wrapper that reads a line with edition capabilities, then sends it to your program. A good wrapper is rlwrap.
Using a separate tool is in keeping with the unix philosophy of using separate tools for separate purposes (line edition and whatever your program does). It's also simple: just make your shell wrapper run rlwrap myprogram instead of myprogram.
The underlying readline library (no relation to any readLine method in Java; you'll want a Java wrapper such as Java-Readline) is more work to integrate, and constrains your program to be GPL. Go for that only if you want more than what a wrapper can provide — primarily application-specific completions.
The readline function is used for this stuff on Unices, but it's a C function.
Here you could find a Java wrapper for it. (I never used it)