Run cmd-line program with user input from Java - java

public class Controller {
public String printResults(Process process) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
String container = "";
while ((line = reader.readLine()) != null) {
container = container + line + "\n";
System.out.println(line);
}
return container;
}
public String executeCmd(String firstname, String lastname) throws IOException {
String command = "./myprogram -n " + firstname + " -s " + lastname;
Process p = Runtime.getRuntime().exec(command, null, new File("/home/user/myprogram/build/"));
return "Hi, \n" + printResults(p);
}
}
This allowes me to run a single command with attributes from a specific directory and get back result.
However, I have a larger program, which asks user for inputs from terminal.
user#debian:~/program/build/$./program
...
Enter Value: 5
...
Enter Name: Hanz
...
Enter State: GE
Output..
How can I run that program and enter user input from Java?

First, rewrite your code to use ProcessBuilder; it's pretty much just replacing exec with new ProcessBuilder.
That's because PB gives you some flexibility that may well come up. For example, you can redirect inputs and outputs to files, for example.
Then, run start() on the builder and this gets you a Process object. You can use this to get the OutputStream, and then you can write your string to that: p.getOutputStream().write("5\nHanz\GE".getBytes());
There are a couple of different ways to do the job; Have a look at the APIs of Process and ProcessBuilder, there's lots of interesting stuff in there :)

Related

Get the Infornation from getRuntime().exec hat java app hangs up

My code so far:
public static void getTasklist(Table table, Login login){
for(TableItem tableItem : table.getItems()){
try {
if(tableItem.getChecked()){
String command = ("cmd.exe /k tasklist /s " + tableItem.getText(3) + " /U .\\" + SettingsManager.getSetting(login, "Current-User") + " /p " + SettingsManager.getSetting(login, SettingsManager.getSetting(login, "Current-User")+"-PW"));
Process p = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
}
catch (IOException e) {
}
}
}
What I want to do: get task list information from a computer and print it to console (later print it to a window maybe). The host name from the computer comes from the checked table (tableItem.getText(3)) and the credentials comes from something I called settingsmanager (SettingsManager.getSetting....).
What works ATM: I get the Information and it got printed to console but after the last line the app hangs up.. I assume that the while loop never breaks but I am not sure about that..

Java getting bash root access

I am trying to install missing dependencies on a Linux for a program I am making. I however am failing at getting the root access required to install the missing dependencies. Here is what I have so far:
My logic is as follows:
1) Check if the dependency is installed using pacapt (npm in this case)
2) if so then get the user password using a text prompt
3) then continue further instructions like so: echo [userpass] | sudo -S ...
Right now the 'echo [userpass] | sudo -S ...' command gets printed out to the shell like so; [userpass] | sudo -S ... (where the user password is displayed in place of [userpass]), but does not execute.
And here is my code:
public class LinuxDependencyCheck extends Application{
public static void main (String [] args){
launch(args);
}
#Override
public void start(Stage mainWindow){
String userPass = null;
String terminalOut = null;
terminalOut = runBash("./LinuxScripts/pacapt -Qqe npm");
if (terminalOut.equals("npm")){
userPass = getUserPass();
if (userPass != null){
System.out.println("runing");
runBash("echo " + userPass + " | sudo -S npm install" +
" phantomjs2");
}
}
}
public String runBash(String runCommand){
String result = null;
String returnVal = null;
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(runCommand);
BufferedReader in =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
result += inputLine;
returnVal = inputLine;
}
in.close();
} catch (IOException e) {
System.out.println(e);
}
return returnVal;
}
public String getUserPass(){
TextInputDialog dialog = new TextInputDialog("Password");
dialog.setTitle("Installation helper");
dialog.setHeaderText("It looks like you are missing" +
" dependecies to complete this action" +
" would you like to try to install" +
" them now");
dialog.setContentText("Please enter your password :");
// Traditional way to get the response value.
Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
return result.get().toString();
}
return result.get();
}
}
Your runBash() method is poorly named, as it does nothing to cause the given command to be run via bash. It is therefore also inappropriate for use with a command string such as you are specifying, which relies on the shell's pipe operator to string two separate commands together.
When you do this ...
runBash("echo " + userPass + " | sudo -S npm install" +
" phantomjs2");
... Java splits the string on whitespace, takes the first substring ("echo") as the command, and executes that command with all the other substrings strings as arguments. Needless to say, that will run without error, but also without the effect you intended.
If you really want to execute the command string via bash (as it appears you do), then in your runBash() method you could change this ...
Process p = r.exec(runCommand);
... to this ...
Process p = r.exec("/bin/bash", "-c", runCommand);
. That should at least get you past your first hurdle.
You also should close the Process's OutputStream (by which you could have piped data into the process), and drain the Process's error stream. In general, you need to drain the input and error streams in parallel, because if either one's buffer fills up then the process can block. Perhaps that's not a risk for this particular command, but you'll need to judge. It's also good form to waitFor() the Process; doing so may avoid Java accumulating zombie child processes.

Passing windows credentials programmatically

I have a VB script to which I need to pass username and password.
I want to run this VB script through Java code programmatically.
Is there a way that I can pass the Windows credentials to the VB script in Java programmatically?
You can have the credentials on the OS environment and read them from there:
String credentials = System.getenv().get("encrypted_credentials_or_something");
And then run your command from Java. However, Runtime.exec() won't work in some cases:
When the command is not on the System's PATH
When arguments are involved
When you want to have access to the process output
When you need to be able to kill the process
When you need to check if it terminated successfully or in error (status code != 0 - which is why you write System.exit(int) to terminate a Java application. The System.exit(1), for example, indicates abnormal termination)
That's why I created this utility class to execute external processes with arguments and everything. It works very well for me:
import java.io.*;
import java.util.*;
public class ExternalCommandHelper {
public static final void executeProcess(File directory, String command) throws Exception {
InputStreamReader in = null;
try {
//creates a ProcessBuilder with the command and its arguments
ProcessBuilder builder = new ProcessBuilder(extractCommandWithArguments(command));
//errors will be printed to the standard output
builder.redirectErrorStream(true);
//directory from where the command will be executed
builder.directory(directory);
//starts the process
Process pid = builder.start();
//gets the process output so you can print it if you want
in = new InputStreamReader(pid.getInputStream());
//simply prints the output while the process is being executed
BufferedReader reader = new BufferedReader(in);
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
int status = 0;
//waits for the process to finish. Expects status 0 no error. Throws exception if the status code is anything but 0.
if ((status = pid.waitFor()) != 0) {
throw new IllegalStateException("Error executing " + command + " in " + directory.getAbsolutePath() + ". Error code: " + status);
}
} finally {
if (in != null) {
in.close();
}
}
}
//Splits the command and arguments. A bit more reliable than using String.split()
private static String[] extractCommandWithArguments(String command) {
StringTokenizer st = new StringTokenizer(command);
String[] cmdWithArgs = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++) {
cmdWithArgs[i] = st.nextToken();
}
return cmdWithArgs;
}
}

how to write log file while starting services using java

hello everyone i created a java file,in that java i written a code for starting,stoping and restarring a windows service ,in that i want to create a log file and write output of window service as a console please if any one knows give suggestion
i used code for stoping service
public static void stopService(String serviceName) throws IOException,
InterruptedException {
String executeCmd = "cmd /c net stop \"" + serviceName + "\"";
Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
System.out.println("processComplete: " + processComplete);
if (processComplete == 1) {// if values equal 1 process failed
System.out.println("Service failed");
}
else if (processComplete == 0) {
System.out.println("Service Success");
}
}
Probably the best way to achieve this is Log4J. If you haven't used it before it can seem overbearing, but there are a lot of good tutorials out there to cherry pick from, should you need to. Put the LogFactory in your class definition and then pop lines like log.info.println("service starting"). The other keyword to keep an eye our for is FileAppender. (Sorry no better references, writing from my phone!)
The best solution is to use some logging API have a look at http://www.vogella.com/articles/Logging/article.html tutorial.
Or the other trivial option is to redirect the output of executing the command in some text File. Change the command
String executeCmd = "cmd /c net stop \" + serviceName + "\"+" >> "+ fileName".
In order to get the output of a command you can do this:
String lineSeparator = System.getProperty("line.separator");
stderr = runtimeProcess.getErrorStream();
stdout = runtimeProcess.getInputStream();
// clean up if any output in stdout
BufferedReader brCleanUp =
new BufferedReader(new InputStreamReader(stdout));
StringBuilder output = new StringBuilder();
String line;
while ((line = brCleanUp.readLine()) != null) {
output.append(line).append(lineSeparator);
}
brCleanUp.close();
// clean up if any output in stderr
brCleanUp =
new BufferedReader(new InputStreamReader(stderr));
StringBuilder error = new StringBuilder();
while ((line = brCleanUp.readLine()) != null) {
error.append(line).append(lineSeparator);
}
brCleanUp.close();
And now in output and error String you have the "standard output" and "standard error" of the process you executed.

handling c executable file using java

I want to run a C/C++ program's exe file using java.......and handle its input and output......
my code is
import java.io.*;
class run2 {
public static void main(String[] args) throws java.io.IOException {
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/C";
// command[2] = "java Run1";
command[2] = "start C:\\WE.EXE";
Process p = Runtime.getRuntime().exec(command);
String i = "20";
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedWriter st = new BufferedWriter(new OutputStreamWriter(
p.getOutputStream()));
String s = null;
System.out.println("Here is the standard output of the command:\n");
s = stdInput.readLine();
System.out.println(s);
st.write(i);
st.newLine();
st.flush();
while ((s = stdInput.readLine()) != null) {
System.out.println("Stdout: " + s);
}
try {
System.out.println("Exit status = " + p.waitFor());
}
catch (InterruptedException e) {
}
stdInput.close();
}
}
i am getting an error which says pipes is closed
do help me out.....
Well, first of all, if there isn't a WE.EXE in C:/, that could be an issue. If no process is ever launched, of course you can't do anything with its input/output pipes.
However, presuming you have a WE.EXE, your error is probably at:
st.flush();
Your application is opening up WE.EXE in command prompt, or cmd.exe, who will take care of both standard input and standard output. Your call stdInput.readLine(); will wait until WE.EXE, and therefore cmd.exe, terminates, at which point the output stream will be closed (and you obviously can't write onto a closed pipe).
So if you want to handle input and output yourself, you should launch WE.exe directly, like:
Process p = Runtime.getRuntime().exec("C://WE.EXE");
Additionally, you may consider using ProcessBuilder instead of Runtime.exec.
Small detail, but consider using Java's naming conventions--for example, your class name would be Run2 (or something more descriptive) instead of run2.
You are trying to read from a stream (stdInput) that does not exist yet.
It won't exist until the WE.EXE program writes something to it.
Just wait until you send the commands to the program.
In other words, take out the first input line, and it will work fine.
//s = stdInput.readLine();
System.out.println(s);
st.write(i);
st.newLine();
st.flush();
while ((s = stdInput.readLine()) != null)
{ System.out.println("Stdout: " + s); }

Categories