Hide Putty terminal while running from Java - java

I am running the following code in Java for running a shell script in Ubuntu.
But when it is running, y=the putty terminal will be displayed. But i dont want this. How will I hide this terminal.
ProcessBuilder pb = new ProcessBuilder(winBasePath + "putty.exe", "-ssh", "-m", winBasePath + "runHiveCmd.txt", linuxSystem, "-pw", linuxPwd);
Process p = pb.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}

Try this
ProcessBuilder pb =
new ProcessBuilder("cmd.exe", "/C", "START", "/MIN", winBasePath + "putty.exe", ...);

If performing a remote command via ssh, consider jsch. This provides cleaner integration does not require ProcessBuilder. Here is an example you could start with

Related

How do I get the process id of shell script process build from Java ProcessBuilder

How can I get the process id of the shell script started with ProcessBuilder?
String cmd[] = { "sh", "-c", "ls -l" };
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectOutput(new File(request.getParameter("output_file_name")));
Process p = pb.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
p.destroy();
See: How can a Java program get its own process ID?
Java 9 now has support for this. Otherwise call a script out of your application to save the PID which you will read somewhere

Running command from java produces empty output

I need to run the below comand using java but it is running fine in terminal as
svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k > output.txt
But when running the same via process builder it is returning empty result.
My code:
ProcessBuilder pb = new ProcessBuilder("cmd", "C:\\Users\\dev112\\output", "svn", "list", "http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs", "--username", "prasadh", "--password", "prasadh2k", ">", "output.txt");
pb.redirectErrorStream(true);
try {
Process p = pb.start();
new Thread(new InputConsumerforImageMagick.InputConsumer(p.getInputStream())).start();
try {
System.err.println("Exited with: " + p.getErrorStream());
} catch (Exception ex) {
Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (IOException ex) {
Logger.getLogger(AddImage.class.getName()).log(Level.SEVERE, null, ex);
}
I/O redirection doesn't work well with ProcessBuilder. You should either call cmd.exe with
new ProcessBuilder("cmd", "/c", "svn ... > output.txt");
(i.e. you have to call cmd with exactly two arguments)
or you must redirect yourself, that is you need to start a background thread which reads stdout from the process and writes it to output.txt. In that case, you should use:
new ProcessBuilder("svn", "list", ...);
The former is brittle when you have spaces in arguments. So I suggest the latter even though the Java code is much more complex.
You should also have a look at Commons Exec which makes it much easier to deal with external processes.
Or with Java 7, you can use pb.redirectOutput();
Don't go through cmd. Just run the command directly:
final Path cwd = Paths.get("c:\\Users\\dev112\\output");
Files.createDirectories(cwd);
final Path outfile = cwd.resolve("output.txt");
final ProcessBuilder pb = new ProcessBuilder("svn", "list",
"http://192.168.0.19/svn/cadgraf/Branch/Jobs/T0003SATHYABAMAT/Completedjobs",
"--username", "prasadh", "--password", "prasadh2k");
pb.directory(cwd.toFile());
pb.redirectOutput(outfile.toFile());
final int retcode = pb.start().waitFor();
What is more, why do you get the process' standard output if you output to a file? Do one or the other, not both. If you output to a file then read the contents of that file after the command is executed.
The sample above outputs to a file; just open a stream to that file afterwards using Files.newInputStream(outfile) (well, that is, if retcode is 0; if it isn't, your command has ended with an error; which also means that you should redirect stderr somewhere, too)
This works for me:
String command = "svn list http://192.168.0.19/svn/cc/Branch/Jobs/tt/jobs/ --username prasadh --password prasadh2k";
ProcessBuilder processBuilder = new ProcessBuilder(command.split());
processBuilder.redirectOutput(new File("C:/Users/dev112/output/", "output.txt"));
processBuilder.start();

Running simple CECopy in CMD using Java

I am trying to run a simple "cecopy" in java. I call "cmd.exe" and pass the command through. It creates the directories but doesnt carry out the copy.
Below is the command I am using, set as a string in java:
String cmd = "mkdir \"C:\\\\Dominos\\\\DATFiles\" >> log.txt\n"
+ "\n" +
"cecopy \"dev:\\Application\\\\MCL\\\\Projects\\\\Default\\\\aa.dat\" \"C:\\\\Dominos\\\\DATFiles\");
Below is how I am calling command prompt to execute the DOS statement:
Runtime rt = Runtime.getRuntime();
try {
Process p = rt.exec("cmd.exe /c" + cmd); // Call CMD
p.waitFor(); // Wait till CMD finishes
} catch (InterruptedException | IOException ex) {
Logger.getLogger(readData.class.getName()).log(Level.SEVERE, null, ex);
}
Any help?
Thanks in advance!
You can use process builder. It handles commands with arguments neatly.
ProcessBuilder processBuilder = new ProcessBuilder();
p.command("cmd_to_run", "args_if_any");
p.start();

Set the root directory in cmd.exe using java

This is one way to run cmd.exe using java:
String command="cmd /c start cmd.exe";
Process p = Runtime.getRuntime().exec(command);
How to enforce command to run cmd.exe from the root directory C:\ ?
As suggested by others, consider using ProcessBuilder.
Code:
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "start");
processBuilder.directory(new File("C:\\"));
try {
processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}

Running a batch file by java

I just wanted to run a batch file using java code in win7. I can run .exe files with the code but u know it doesn't work with a batch. Where is the problem? You know even cmd.exe doesn't start with that command. But I can run other exe files, I've tried some. The code is this (with try and catch is that): none of them worked!
Runtime.getRuntime().exec("cmd.exe /c demo.bat");
Runtime.getRuntime().exec("demo.bat");
i tried to do work with process and i wrote the code below. it retuened
java.lang.IllegalThreadStateException:process has not exited
at java.lang.ProcessImpl.exitValue(Native Method)
at Test.Asli.main(Asli.java:38)
this is the code:
try{
Runtime rt = Runtime.getRuntime();
Process proc= rt.exec("C:\\Windows\\System32\\cmd.exe");
int b = proc.exitValue();
// int exitVal = proc.exitValue();
//System.out.println("Process exitValue: " + exitVal);}
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Try the following:
String[] cmd = {"cmd.exe", "/c", "demo.bat");
Runtime.getRuntime().exec(cmd);
I always prefer splitting the command and the parameters myself. Otherwise it is done by splitting on space which might not be what you want.
Try this:
Runtime.getRuntime().exec("cmd.exe /c start demo.bat");
Use this:
try {
Process p = Runtime.getRuntime().exec("C:PATH/TO/FILE/yourbatchfile.bat");
} catch(Exception e) {
e.printStackTrace();
}
It even hides the annoying prompt window (if you want that)

Categories

Resources