All,
Our server is running Java 1.5 and I am having difficulty trying to mask user input from the command line. I am executing a jar file (java -jar my.jar) and am working through command line prompts via printlns. I cannot use Java Console.
Thanks
The best approach would be to use Java 6's Console readPassword() method. Since you mentioned that you are using Java 5, that is not an option. A lot of Java 6 utilities have been backported to Java 5. I have not found anyone who has done it for this class though.
This site has a good article on how to do it using Java 5. http://www.devdaily.com/java/edu/pj/pj010005/. Basically they wrap System.in with an InputStreamReader and read a line.
On a Unix system? You could try running the system program stty with argument -echo before reading the program and the program stty echo afterwards.
ProcessBuilder pb = new ProcessBuilder("stty", "-echo");
pb.start().waitFor();
// Read the password here
pb = new ProcessBuilder("stty", "echo");
pb.start().waitFor();
Note that I have not tried this! Some stty programs are happy with it and some aren't (those that aren't need their stdin to come from /dev/tty to work right...)
Here is an interesting article: http://java.sun.com/developer/technicalArticles/Security/pwordmask/
edit now that I re-read that, I think their command-line "solution" is really stupid. What we've got in our application is an auxiliary program to do that, one that understands how to mask input according to the OS (Linux, Windows, whatever). The Java code listens for commands on a socket, and the front-end password reader gets the password and anything else needed, then sends commands to the Java code.
Related
I built a GUI in JavaFX with FXML for running a bunch of different Python scripts. The Python scripts continuously collect data from a device and print it to the console as it's collected in a loop at anywhere from around 10 to 70 Hz depending on which script was being run, and they don't stop on their own.
I want the end-user to be able to click a button on my GUI which launches the scripts and lets them see the output. Currently, the best I have done was using Runtime.exec() with the command "cmd /c start cmd /k python some_script.py" which opens the windows command prompt, runs python some_script.py in it, and keeps the command prompt open so that you can see the output. The problem with this is that it only works on Windows (my OS) but I need to have universal OS support and that it relies on Java starting an external program which I hear is not very elegant.
I then tried to remedy this by executing the python some_script.py command in Java, capturing the process output with BufferedReader, creating a new JavaFX scene with just a TextArea in an AnchorPane to be a psuedo-Java-console and then calling .setText() on that TextArea to put the script output in it.
This kinda worked, but I ran into many problems in that the writing to the JavaFX console would jump in big chunks of several dozens of lines instead of writing to it line by line as the Python code was making Print() calls. Also, I got a bunch of NullPointerException and ArrayIndexOutOfBoundsException somewhat randomly in that Java would write a couple of hundred lines correctly but then throw those errors and freeze the program. I'm pretty sure both of these issues were due to having so much data at such high data rates which overflowed the BufferedReader buffer and/or the TextArea.setText() cache or something similar.
What I want to know is what approach I should take at this. I cannot migrate the Python code to Java since it relies on someone else's Python library to collect its data. Should I try to keep with the pseudo-Java-console idea and see if I can make that work? Should I go back to opening a command prompt window from Java and running the Python scripts and then add support for doing the same with Terminal in Mac and Linux? Is there a better approach I haven't thought of? Is the idea of having Java code call Python code and handle its output just disgusting and a horrible idea?
Please let me know if you would like to see any code (there is quite a lot) or if I can clarify anything, and I will try my best to respond quickly. Thank you!
My solution was to still call the Python code from the Java Processbuilder, but use the -u option like python -u scriptname.py to specify unbuffered Python output.
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.
The Java program has to launch few prorams that are launched using a command promt(one of them is nginx). How could I handle and send commands to the program from my Java application?
I found this library http://commons.apache.org/proper/commons-cli/usage.html But I'm not sure how it helps..
I do NOT need code. I need an explanation on how things like these work.
Well keep in mind its never a nice solution.
You act like you would be on a command line so you execute Commands like you would on the shell. And does always depend on your platform.
You said you don't want code, I will give it to you anyway ;)
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("service nginx start");
That is done with plan java.
I highly advise you to use a script language for that. Thats just not Java.
additional info:
One thing to remember is to use the streams on process to send input and check output (from Process class)
abstract InputStream getErrorStream()
Returns the input stream connected to the error output of the subprocess.
abstract InputStream getInputStream()
Returns the input stream connected to the normal output of the subprocess.
abstract OutputStream getOutputStream()
Returns the output stream connected to the normal input of the subprocess.
If you need to execute shell commands, this can be achieved like so (This example uses bash as the executing process)
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","my_script.sh"});
You'll have to write your own script for this, but you don't even have to put it in a file. If you wanted to give user-like input to a command, keep in mind that you can pipe in the result of an echo to a command and it will act as a user typing that command. e.g
echo 1234 | pinTaker.sh;
This will effectively "type in" 1234 to the pinTaker script. This can also be used for things like typing in a password for ssh (Though this is not a good idea, it's a good example..)
I am looking for a way to check the state of a windows service through Java. From some basic search through Google and here it sounds like Java has no api to query the Windows Services.
On the Windows command prompt running: sc \some_host_name query "serviceName"
gets me the info i want. Now i want to be able to run that in a Java program and be able to parse the output.
Any one know of a way to run a Windows command through Java?
Sounds like you need the Java 5+ ProcessBuilder.
A quick example (based the above documentation)
To start a process running:
Process p = new ProcessBuilder("sc", "\\some_host_name", "query", "serviceName").start();
The Process class provides methods to get the output (and error) stream from the process - it's standard stream handling from there.
The pre-Java 5 way of doing this was Runtime.exec(). I haven't actually used ProcessBuilder on Windows myself, drop a comment if you have problems?
Hey I have run into the following problem when attempting to build a program in java which executes commands on a remote linux server and returns the output for processing...
Basically I have installed Cygwin with an SSH client and want to do the following:
Open Cygwin,
Send command "user#ip";
Return output;
Send command "password";
Return output;
Send multiple other commands,
Return output;
...etc...
So far:
Process proc = Runtime.getRuntime().exec("C:/Power Apps/Cygwin/Cygwin.bat");
Works nicely except I am at a loss as to how to attempt the next steps.
Any help?
The quick way: Don't go through cygwin. Pass your login info and commands as arguments to ssh.
A better way: Install and use the open source and very mature Sun Grid Engine and use its DRMAA binding for Java to exec your commands. You might also consider switching to a scripting language (yours is a very script like task). If you do DRMAA has Perl, Ruby and other bindings as well.
You could also use Plink:
Download here
There is a good set of instructions link here
You can use a command like:
plink root#myserver -pw passw /etc/backups/do-backup.sh
Use a ssh implementation in java. I used Ganymede a couple of years ago, there are perhaps better alternatives now. (?)
Using Ganymede, you will get an input stream to read from, and an output stream to write to.
You can create a LineInputReader on the input stream and use that to read Strings representing the output from the remote server. Then use a regexp Pattern/Matcher to parse responses.
Create a PrintWriter on the output stream and use println() to send your commands.
Its simple and actually quite powerful (if you know regexp... It might require some trial and error to get it right...)