4

I am trying to make a terminal emulator in Java. The java program will accept the commands from user, and show its output to them. I can emulate simple commands like 'ls', but I don't know how to handle commands like 'cd'. This is because, I am using exec() method for executing terminal commands. So, all the commands are executed at current directory. The commands like 'cd ..' are executed, but then they have no persistent effect, because each command is separately executed by exec().
Any Ideas How I can emulate a whole session??

4
  • You can give exec() a working directory. docs.oracle.com/javase/1.4.2/docs/api/java/lang/…, java.lang.String[], java.io.File) Commented Jan 8, 2013 at 17:24
  • but how to know that the user has changed the directory and then giving exec() that new directory? Commented Jan 8, 2013 at 17:26
  • Isn't the working directory part of your session? Make cd a "builtin", just like it is for classical shells Commented Jan 8, 2013 at 17:26
  • means whenever user will enter something starting with cd, I would rather modify my 'directory' Java variable instead of actually executing that command?? Commented Jan 8, 2013 at 17:31

3 Answers 3

5

If you are executing commands with exec(), you are not writing a terminal emulator; you are writing a shell. In that case, you will need to keep track of things the shell keeps track of, like environment variables and working directory.

If you really want to write a terminal emulator, you would be talking to a shell process through a pseudo-terminal. Then your program would just be keeping track of the things a terminal keeps track of, like the line state and what appears on the screen.

Working with a pseudo-terminal from Java will be a little tricky, because most of the documentation assumes you are using a C api. man pty should get you started. Your Java process will have to open the master side of the pseudo-terminal with FileStream objects. I'm not sure there is a way within Java to get a child process to open the slave side of the pseudo-terminal; you might have to invoke a shell command with exec() that starts another shell command with standard input/output/error redirected to the slave side of the pseudo terminal.

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

2 Comments

Thank you, please help me about how can i proceed to write a terminal emulator. The talking to shell process is what I really want to do i guess.. :)
@Shaarad Dalvi-- I've added more info
2

JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.

http://www.jcraft.com/jsch/

1 Comment

Yes, I am a student and I want to code the similar thing in a simple and small scale.. :)
0

You should really give a try to Ganymed.

Ganymed SSH-2 for Java is a library which implements the SSH-2 protocol in pure Java (tested on J2SE 1.4.2 and 5.0). It allows one to connect to SSH servers from within Java programs. It supports SSH sessions (remote command execution and shell access), local and remote port forwarding, local stream forwarding, X11 forwarding, SCP and SFTP.

http://www.ganymed.ethz.ch/ssh2/

Ganymed along with apache FTP client you can also download and upload files.

Also there is a inbuilt example code for terminal emulation in Ganymed.

The following is a link to a project which is did using Ganymed along with apache FTP client.

GITHUB

Happy Coding!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.