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.
Related
I have created an ImageViewer using javafx for browsing and editing images. This appn provides an open menu to select image files to be browsed. I converted the .jar file into .exe file. In properties of one image file I changed the option "open with" so that the image now by default opens using this ImageViewer. But when I double click the image, appn ImageViewer launches and doea nothing as theres no coding for such things. The only way to view image is to use the "open menu". But I want the functionality that on clicking of the image, the appn ImageViewer launches and displays the image. Is there any way of doing this?
To answer your question: Yes, there is a way to do this since we know other applications can do it - we just have to identify what it is.
I have done something similar before, so allow me to walk you through my thinking regarding how to find the answer and then I think it will be clear.
Consider the problem - you have an operating system trying to talk to an application such that it will tell the app which file to open. Logically we must conclude that the system must send the app a path to that file.
Now, how would the communication take place? Let's think about setting default programs - are there any limitations there or can you ask any program to open any kind of file? If you experiment a bit you'll find that there are no restrictions - you can ask any program to open any file (not saying it will work correctly, but it can be done). So what does that tell us? It means the system has no idea which programs are the "correct" ones to open files and this suggests there is no registration mechanism where you would have to say to the system "Hey, I can open this kind of file".
So, if any program can open any file and the system does not maintain a registry of programs that can open files how would you think it would talk to a program to communicate the file path it should open? Well, the simplest most generic way would seem to be with command line arguments. This way all the system does is invoke the application and pass an argument containing the file path.
When you define your main method: public static void main(String[] args){...} the "args" parameter is where the command line arguments are being passed to your program. If you write some log statements and/or System.out statements to dump the contents of the args I bet you'll find that the file path is being passed this way.
Once you confirm that you can obtain the file path through the args then you just have to write your file reading logic such that is uses the argument passed in by the system to find and read the file contents.
I have googled for a while now, but no matter how I put the question, google just won't give me what I'm looking for (just watch how someone now will find the answer in 1 google search :D ). So I'm getting around to finally being able to convert my java code into .exe files using Launch4j, but one thing I noticed is I'm unable to paste into the console. I assumed since the console uses command prompt as it's base, it would have it's functions, but unfortunately not. This wouldn't be an issue most of the time, but when for example I want to enter a very long string of binary numbers, I'd rather not have to type it in. So if someone would tell me how (if at all possible) I can paste plain text into the console, I'd really appreciate it. Thank you!
update: ok, I've realized if I right click the title bar of the window and hover over "edit", I have access to the right click functions that would normally show up in a regular cmd window, so my original question is basically solved. However is it possible to compile the .exe such that I can just right click anywhere in the window rather than only the title bar? This simplicity is not for me, it's so the program is as user friendly as possible.
I have been learning Java for the past couple of months now and while I have gotten to grips with just about everything, input and output redirection are causing me somewhat of a problem.
The book I am using (Introduction to Java Programming, Ninth Edition) brings up the concept fairly early on and references it on and off throughout the book and while I have managed to get away with not knowing exactly how to do it, I feel it will set me back considerably soon if I don't get to grips with it soon.
So, my question is how do I do it? So far I understand that input redirection is done by
java ClassName < input.txt
And Output by
java ClassName > Output.txt
in the Command Prompt. This is basically all the book gives in respect to the topic. Yet I've had limited success with this. Such that I could get it to work by fully directing it to the folder where both ClassName.class and input.txt is saved. For input redirection to work, do both the class file and .txt file have to be situated in the same folder?
If so what are the commands for use in cmd to change the folders in which it is directed towards?
Then for output redirection i have been unsuccessful in getting this to work, does output redirection create a .txt file in which it will store my output data or do i need to create that .txt file before I can use output redirection?
For reference I have organised my work through the book into chapters so that each program created will be easy to find in the eventuality that I need to. they are stored as followed:
C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming
for Chapter 2 respectively.
This is my first post on StackOverflow so any advice is appreciated and I apologies if I'm unclear in what I'm asking as I am not yet proficient in Java and what everything means as of yet
With the commands you have above, everything will have to be in the same folder, or will be created in the same folder. Think of the second argument, the filename, as a reference to where that file can be found (in the case of input) or where that file will be placed (in the case of output).
If you just specify a simple filename (without a directory structure in front of it), the system will assume that file will be read in or created in the current directory (like your Chapter2ElementaryProgramming).
So if you're in:
C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming
and ClassName.class and input.txt are also in C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming, you can get away by simply doing as you've described above.
If you have ClassName.class somewhere else, but want to read in the input.txt from wherever you call java ClassName, you can do:
java ClassName < C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming\input.txt (assuming you have the input.txt in that directory).
The same applies for output.txt. Except for output, just think of it as capturing what was naturally outputting to the screen to a file instead. So if you just run java ClassName, and it spits out a bunch of output, then if you do java ClassName > output.txt, all it does is save all that output directly to the output.txt instead of showing it to your screen.
Again, if you want output.txt to end up somewhere different than where you have ClassName.class, you can give it the full directory to where you want it to go, like:
java ClassName > C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming\output.txt
I hope that helps, if this was indeed what you were asking.
As for something successfully outputting or inputting, that part, as suggested, might help to see the code to make sure it is programmatically sound.
Good luck.
java ClassName < input.txt
Runs a class called ClassName that has to be available on the CLASSPATH. If you're using the default CLASSPATH of '.', that means that ClassName.class must be in the current directory.
Redirects the input from a file called input.txt in the current directory.
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!
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.