I am trying to get a program to automatically send text at a certain time to another program. I created a short Java code that simulates the program I am trying to use for the actual program. The real-life scenario is I am running a .jar file with a batch file and I want a program that will send keyboard inputs to this cmd window and command the .jar from cmd automatically based on the system time. Any help? I am familiar with AutoIT, Java, and basic batch functions. Thanks!
play with this:
While 1
If #HOUR == 22 And #MIN == 31 And #SEC == 15 Then
ShellExecute(#WorkingDir & "\example.jar")
;Sleep(2000)
Send("examplecommand {ENTER}")
EndIf
WEnd
Related
I'm working on a project in which I intend to make a Java GUI application that connects to a ssh server and executes remote commands on the server. I'm willing to use JSch Library. My aim is to make buttons and textfields those will provide the user the ability of sending commands and getting replies easily. I mean, instead of opening xShell and prompting "grep "hi" /usr/file.txt", the user will choose the path from the list and will enter "hi" into the textfield and will press the button for grep.
Problem is, I couldn't find a solution to execute multiple linux commands in one session (I don't want shell if i cannot redirect its input and output streams) (also I don't want the solution "cd.. \n dir \n ls -l" which works fine but not solving my problem) send the arguments those shall be taken from related GUI components.
Since I have not made so much modifications on the JSch's example code, yet, you can see the code here: http://www.jcraft.com/jsch/examples/Exec.java.html
Thanks from now on.
If using exec type of channel you can combine commands with && :
channel.setCommand(". ./.profile && env");
The program I'm using runs through the command prompt from java using a batch file (following the solutions from Run .exe file from Java from file location, and at one point of time, it will prompt the user for an input which is either a 'y' or 'n' in order to continue the program.
How do I program using java codes such that the value 'y' or 'n' is automatically entered when prompted, without requiring users to enter it manually? I have tried using pipe (following the solutions from How do you enter something at a DOS prompt Programmatically?) but it doesn't work. Any ideas?
Grab the InputStream from the Process and write the y into it.
Look at this questions for example code:
Java Process with Input/Output Stream
Reading streams from java Runtime.exec
You can do it the old DOS way.
Create a file, say yes.txt. All it should have is y (as per your app expectations). The file can contain responses to multiple prompts each on their own lines
Now you can execute your exe file something like this
myApp.exe < yes.txt
When exe prompts, yes.txt will supply the prompt text
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 have a CentOS server which is currently running a java jar application. I need to write a php script to communicate with this running program via its input stream. The java program outputs its output to a log file so I don't need access to the output stream.
I don't want to restart the program, just access the running process and interact with it.
Can someone point me in the right direction?
If portability is not a big matter for you, why not creating your own pipe(s)? I don't know much about the java application but have a look at the "mkfifo" function/command.
First, find the ProcessID of the application. You may do it using:
ps -Af | grep java
Since you are using java, you may feel more convenient with the jps command for finding the PID.
I'll assume PID of the running application is 12345. It suffices to issue the command:
cat >/proc/12345/fd/0
And whatever you type in will be put in the standard input of that application. Note that fd contains the file descriptors used by the application, and I suppose the 0-th file descriptor would always be stdin.
Using PHP for writing into the file (and thus being consumed by the application as input) is possible as well.
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.