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.
Related
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 want to create one standalone executable file which accepts some user input and it should use those inputs as arguments to run a command at CMD as background process. For GUI i thought of using swings and to run cmd batch file . Is it possible to merge java files and batch file to form an exe.? or i should go for someother scripting languages to execute command at background process and someother programming languages for UI design ? Thanks in advance.
Theoretically you could do this all in Java. As you mentioned, you could use Swing to read in the user input. Instead of trying to get ahold of the input data from a separate batch file, you could just use Java's Runtime class to execute a command using the inputs as arguments.
And, if you need this all to run from an executable, you could use a utility like Launch4J or JSmooth to wrap the Java application.
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.
I would just like to know whether it is possible to make a command prompt in Java.
My friend asked to make it, I wanted to know if it was possible or not. If it is possible, can someone suggest me some api or something? Thank you.
EDIT: I want to make it similar to windows command prompt
EDIT 2: I would like to make a SWING GUI application and put a command prompt inside of it.
Yes. Use the Process API.
You can run commands in Java using the Process API. You can also get the output and write input to the runned process. For more info, see this tutorial.
But if you want to make a terminal emulator (such as those in Linux) in Java,
I recommend having a look at JCTerm or JTA.
You must be careful how you start it.
If you start your program with java.exe then the console (input/output) is shown. With System.out.println("mymessage"); you can print (output) text to the console. With System.in you can read from the console. This delegates to the java.io.Console class (available throug System.console()).
If you start your program with javaw.exe, then you don't see the console. You must then create your own screen to allow input/output. This is the default on Windows.
Java can do console I/O and it can launch processes, so yes, it's possible. You'd use methods of System.in and System.out to display a prompt and read commands, and a ProcessBuilder to execute programs.
yes it's possible in java
your have to do some research on Java.lang & IO
Check the class java.lang.Runtime. It provides a couple of exec() methods.
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)