I'm trying to run gcc via ProcessBuilder, but it says:
'gcc' is not recognized as an internal or external command,
operable program or batch file.
But running gcc via cmd works.
Here is code:
public static void main(String[] args) {
String command = "gcc C:\\Users\\pawel\\Desktop\\CFG-master\\test.c";
System.out.println(executeCommand(command));
}
public static String executeCommand(String command) {
String line;
String result = "";
try {
ProcessBuilder builder;
builder = new ProcessBuilder("cmd.exe", "/c", command);
builder.directory(new File("C:\\Users\\pawel"));
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (true) {
line = r.readLine();
if (line == null) {
break;
}
result += line + "\n";
}
} catch (IOException e) {
System.out.println("Exception = " + e.getMessage());
}
return result;
}
cmd screen
Related
A very simple code running in the debugging mode perfectly but not working after installation of exe, giving no response/result in return. even no errors to trace.
After building a .exe and installing on my PC its happening, very strange.
tried process builder but the same thing, anyway to check/trace it. maybe paths ?
StringBuilder b = new StringBuilder();
Process p = Runtime.getRuntime().exec("wmic diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
Please note: CMD /c before commands also return an empty response in actual env.
An internal windows command with arguments, like "wmic diskdrive ..."
can be executed easily by wrapping it up inside a cmd window.
Here is the working code snippet for running the wmic command encapsulated in a cmd window:
import java.io.*;
public class Wmic {
public static void main(String[] args) {
StringBuilder b = new StringBuilder();
try {
// Initialize a process object to run
// wmic command and its parameters
// inside a cmd window
Process process = Runtime.getRuntime()
.exec("cmd /c C:\\Windows\\System32\\wbem\\WMIC.exe diskdrive get signature");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
b.append(line);
}
} catch (Exception ex) {
b.append(ex.toString());
}
System.out.println("Output: \n" + b.toString());
}
}
Output:
>javac Wmic.java
>java Wmic
Output:
Signature
More information:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
The ProcessBuilder constructor takes a list of strings. When using ProcessBuilder to run a command, I separate all the words in the command into separate strings.
I read the output of the process in a separate thread. And I always wait for the command, that I launched via ProcessBuilder, to terminate.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
public class PrcBldTs {
public static void main(String[] args) {
ProcessBuilder pb = new ProcessBuilder("wmic","diskdrive","get","signature");
try {
Process proc = pb.start();
StreamGobbler error = new StreamGobbler(proc.getErrorStream());
StreamGobbler output = new StreamGobbler(proc.getInputStream());
Thread stdout = new Thread(output);
Thread stderr = new Thread(error);
stdout.start();
stderr.start();
int result = proc.waitFor();
stdout.join();
stderr.join();
System.out.println("Exit status = " + result);
if (result != 0) {
System.out.println(error.getContents());
}
else {
System.out.println(output.getContents());
}
}
catch (IOException | InterruptedException x) {
x.printStackTrace();
}
}
}
class StreamGobbler implements Runnable {
private BufferedReader reader;
private StringWriter sw;
public StreamGobbler(InputStream is) {
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
sw = new StringWriter();
}
public String getContents() {
return sw.toString();
}
public void run() {
try {
String line = reader.readLine();
while (line != null) {
sw.append(line);
sw.append(System.lineSeparator());
line = reader.readLine();
}
}
catch (IOException xIo) {
throw new RuntimeException(xIo);
}
}
}
Running the above code gave me the following output.
Exit status = 0
Signature
1145609371
I try to send email using mutt in linux and java
if I execute the mutt command from linux command line the email send great
echo "test" | mutt -s "subject" -- "jojo#foo.com
now I have this simple java app that I try to execute the same command and I get nothing, not even error:
java -cp runtime-SNAPSHOT.jar MyApp "echo \"test\" | mutt -s \"subject\" \"jojo#foo.com\""
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class MyApp {
public static void main(String[] args) throws InterruptedException, IOException {
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}
try
{
String[] cmd = new String[3];
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + args[0] );
Process proc = rt.exec(args[0]);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
what is wrong here?
You get no error as echo seems to be available on your system(usually as "/bin/echo") . The Stringtokenizer in the Runtime exec method passes the rest of your line as parameters to /bin/echo like this:
/bin/echo "\"test\"" "|" "mutt" "-s" "\"subject\"" "--" "\"jojo#foo.com\""
Well this is a valid comand as it calls /bin/echo and /bin/echo outputs all the parameters but never calls mutt. (btw. /bin/echo is a different echo than the one used in a Bash shell which is a builtin and behaves a little different...)
That they(Java) tokenize the command in the exec method may be convenient sometimes but leads to quite irritating effects like this because it makes one assume that something should work, that actually doesn't as in this case...
What you probably want is a shell executing your command line. So you have to actually execute a shell(I marked the change in the file):
public class MyApp {
static class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(type + ">" + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException, IOException {
/*if (args.length < 1) {
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}*/
args = new String[]{"echo \"test\" | grep -i \"s\" " };
try {
String[] cmd = new String[3];
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + args[0]);
//Change here: execute a shell with the command line instead of echo:
Process proc = rt.exec(new String[]{"/bin/sh","-c", args[0]});
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Sidenote. For a better minimal testcase:
I replaced your mutt command with some grep as I don't wan't to send mails ;)
I faked the java command line by creating the array("args") programatically.
made your StreamGobbler static in order to have it one file.
All that shouldn't change your testcase. What does make a difference is the rt.exec call that executes a shell instead of /bin/echo
example run:
Execing echo "test" | grep -i "s"
ExitValue: 0
OUTPUT>test
I'm using the following script to execute commands and get output:
Runtime rt = Runtime.getRuntime();
static Runtime rt;
static Process proc;
public static void main(String[] Args) throws IOException, InterruptedException {
rt = Runtime.getRuntime();
String[] commands = {"ssh.exe","-o BatchMode=yes", "root#192.168.1.1", "cat /var/a.txt"};
// String[] commands = {"ssh.exe", "root#192.168.0.1"};
// String[] commands = { "ls", "-la" };
proc = rt.exec(commands);
new Thread("out") {
public void run() {
System.out.println("Thread: " + getName() + " running");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String s = null;
// read the output from the command
System.out.println("StdOut:\n");
try {
while ((s = stdInput.readLine()) != null) {
System.out.println("$ " + s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
new Thread("err") {
public void run() {
System.out.println("Thread: " + getName() + " running");
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String s = null;
// read any errors from the attempted command
System.out.println("StdErr:\n");
try {
while ((s = stdError.readLine()) != null) {
System.out.println("! " + s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
Thread.sleep(10000);
System.out.println("end");
}
If I execute "ls -la" or "ssh" I get the expected output. However, attempting to get a.txt content from remote device (line 1) fails and the operation hangs.
Executing "ssh root#192.168.0.1 cat a.txt" from command line works and retrieves the content.
Why is this happening? How can it be fixed?
Thanks
Because you need to read the two streams in separate threads, or merge them. You can't assume that the process will exit and therefore close its stderr before writing anything to stdtout, or rather without writing so little to stdout that it won't block.
import java.io.*;
public class Clasa {
public static void main(String args[]) {
try {
Runtime rt = Runtime.getRuntime();
String comand = "net start MySQL55";
Process pr = rt.exec(" cmd.exe /C " + "net start MySQL55");
BufferedReader input = new BufferedReader(
new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
I try this and gives me the next error code: Exited with error code 2
I am trying to run an external process in Java and I have no idea why my code isn't working. It works for any other 'cmd' command (for example /c dir). If I replace cmd sc sdshow w32time with cmd /c dir it works.
Here is my code:
public class services2 {
public static void main(String args[]) {
try {
Process p = Runtime.getRuntime().exec("cmd sc sdshow w32time");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (IOException e1) {
} catch (InterruptedException e2) {
}
System.out.println("Done");
}
}
Any ideas?
Your problem seems to be that sc is not an argument for cmd.
What you need is:
sc sdshow w32time