Console IO for Java applet - java

I have a console based (System.in and System.out based) Java standalone application. I am trying to convert it into an applet. But the problem is that there is no console in Java applet! In other words, when I write to system output it is not displayed to the user and similarly I am unable to get input from user in absence of a console.
I know that a console lookalike can be done using TextArea and KeyListener, but for that I would need two different components, one for input and one for output. I want the input and output to go into the same component. Any ideas?

But there is a standard console for Java applets :)
http://www.java.com/en/download/help/javaconsole.xml
http://download.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/console.html
Joking aside, there does seem to be some console code on this site: http://math.hws.edu/eck/cs124/javanotes2/source/index.html. Apparently you can just extend this class and it'll give you a console in your applet. However, you may need to redirect System.out (standard output), System.err (error output) and System.in (input) into the console. That doesn't seem to be implemented but could be blocked in applets by the SecurityManager.

If you make your JTextArea editable, you can then listen for DocumentEvents that indicate when a user has entered text.
That said, and not knowing anything about your applet, it's generally nicer from a UI perspective if the user does not have to type into a console. Consider breaking the input into distinct fields, with a clear transition from input to execution to output.

The way I always run a console based Java program without downloading anything is running over to the trust command prompt terminal.
Click windows button + r (or open file explorer or program search)
Next, type "cmd" into the search box and hit enter.
Now, in the command prompt terminal, type java -jar xx.jar without quotes and replacing xx with the program name.
That's it! It's as simple as that! If you would like to make a file that you click on that automatically runs your Java application, continue with these steps:
Make a new text document where you want your start program file to be.
In the text document, type:
#echo off
start java -jar xx.jar
exit
Save the file as a .bat file.
That is how you make a batch file to run your Java application!

Related

Running emacs remotely fails when no X11 forwarding

I have been writing java program that I start from the terminal. One of the tasks my program must perform is to open a text file in my favorite text editor. I have accomplished this fairly easily with the following command, but it only works in certain scenarios.
Runtime.getRuntime().exec("emacs "+p.fullName);
This works great when I am on my local Linux, and when I ssh in to a computer using the -X flag (for X11). At these times, the editor pops up in a separate window. However, if I ssh without -X, my beloved text editor never appears. It is the times that I am trying to open the editor in the same terminal as the Java program.
The reason for this seems to make sense, the java program is currently occupying the terminal, so the editor either gets created in a detached state or not at all. Either way, what I'd like to do is somehow put my program in the background and set up my editor as my foreground process. And is there a better term for this than context switching?
Edit: Emacs gives me this error at the moment: emacs: standard input is not a tty
Edit: Removed mentions of Lanterna, because bug is reproducible without it.

How to check if a key was pressed in do-while Loop [in Java] [duplicate]

I have a console program written in Java that should respond to single key presses, but the user does not press enter.
I'm making pong.. so need the up and down keys to move the bat thing.
Alternative approaches welcome! (apart from making a GUI instead)
-- Edit:
I'm only going to run my program on UNIX systems (OSX and Linux), so I think I can put the terminal into "raw" mode with this: stty raw
When I type that into the console before running the program it works! But I need Java to do it automatically, so I tried this:
Runtime.getRuntime().exec("stty raw");
and it does nothing... probably because the JVM is just running it as a separate process and not as a process within this terminal.
I think you can't without native code and JNI. Take a look at Java Curses library:
http://sourceforge.net/projects/javacurses/

How to read/copy command prompt data using JAVA

I got an application that prints bunch of lines on the command prompt, I want to read that information and write it into a file to track my application output.
I can not invoke my application using Process or ProcessBuldier and read output because the output it prints uses a different process.
What I want to do is copy data from the prompt and paste it into a file, I just do not know how to access that process.
for example, open cmd prompt and type dir manually know I want to read that info and write it into a file(remember I am not using getruntime.exec('cmd','\c',dir) to get my cmd output but instead I am doing dir part manually by opening cmd and typing all I am doing is reading the output.
basically what you want amounts to a keylogger. This has security implications (you might try to intercept someone's password) so I think it's not possible in Java.
If you are looking for manual steps to copy visible text from cmd, then following steps would help.
Right click on cmd.
Select "mark" from context menu.
Drag over the text that you want to copy.
Right click again to copy.
Paste anywhere you want.
Else please explain your question in more detail.

Control a Command Prompt Window Like a Swing Window using Java [duplicate]

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.

DOS batch file to enter commands in proprietary java app and receive feedback?

I'm working on a project in which I'd like to be able to turn lights on and off in the Duke Smart Home via a high frequency chirp. The lighting system is called Clipsal Square-D and the program that gives a user access to the lighting controls is called CGate. I was planning on doing some signal processing in Matlab, then create a batch file from Matlab to interact with Cgate. Cgate is a proprietary Java app that, if run from a DOS command line, opens up another window that physically looks like the DOS command prompt. I have a batch file that can check to see if Cgate is running and if not, open it.
But what I can't figure out how to do is actually run commands in the Cgate program from the batch file and likewise, take the response from Cgate. An example of such a command is "noop," which should return "200 OK."
Any help would be much appreciated! Thank you very much in advance :)
(here's my existing batch file by the way)
#ECHO off
goto checkIfOpen
:checkIfOpen
REM pv finds all open processes and puts it in result.txt
%SystemRoot%\pv\pv.exe
%SystemRoot%\pv\pv.exe > result.txt
REM if result has the word notepad in it then notepad is running
REM if not then it opens notepad
FIND "notepad.exe" result.txt
IF ERRORLEVEL 1 START %SystemRoot%\system32\Clipsal\C-Gate2\cgate.exe
goto end
:end
I don't know how to do this on Windows, but on UNIX, there is a program called expect that is designed for such a task. If you install Cygwin, you should be able to use the expect utility on Windows.
You're calling start cgate.exe, which will cause cgate.exe to be launched in a new window. First off, you probably want to run cgate in the same window, which means you should drop the start.
Secondly, you can use shell redirection to pass commands to the STDIN of cgate from a text file, like so:
cgate.exe < commands.txt
This will probably work, but it might not, depending on how cgate.exe is actually expected to receive its data.
If you want to have two-way communication, where you send in data, get the response, then send in more data depending on what the response was, you'll have to use something other than a batch file. Most scripting languages (perl, python) could be used for this purpose, or C or anything else.

Categories