I have a requirement to export mysql table data to be downloaded by user in CSV format.
To achieve this I am trying java runtime exec function and executing "mysql -e". Unfortunately, I am stuck half way. I am only able to display sql output to console but not able to route it to a file.
WORKING CODE
(I am able to see records in eclipse console)
try {
Process p = Runtime.getRuntime().exec(new String[]
{
"mysql","-h","localhost","-u","admin","-pxyz","myDB","-e", "\"select concat(billing_amount,',') as 'Billing Amount,', concat(amount_paid ,',') as 'Amount Paid,' from invoice\""
}
);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
//process.waitFor();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But when I am trying to export data to a file using "> abc.txt ", the file is not created, instead I see the mySQL --help option in eclipse console. What could be wrong here?
CODE NOT WORKING
try {
Process p = Runtime.getRuntime().exec(new String[]
{
"mysql","-h","localhost","-u","admin","-pxyz","myDB","-e", "\"select concat(billing_amount,',') as 'Billing Amount,', concat(amount_paid ,',') as 'Amount Paid,' from invoice\"", "> abc.txt"
}
);
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
//process.waitFor();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I/O redirection is handled by the shell, not by each program. Hence, mysql does not know how to handle > abc.txt. Executing a shell and passing the full command as a single argument like this would work in an Unix system:
Process p = Runtime.getRuntime().exec(new String[] {
"sh", "-c", "mysql -h localhost -u admin -pxyz myDB -e \"select concat(billing_amount,',') as 'Billing Amount,', concat(amount_paid ,',') as 'Amount Paid,' from invoice\" > abc.txt"
});
But the better option, that does not assume an Unix shell, is to use a ProcessBuilder and the redirectOutput(File) method.
Related
i am running jdk on windows 7. I try to run a external software (pocketsphinx_continous.exe) within my java application. The software runs permanently (pocketsphinx_continous.exe) and prints some output to the console which i like to read by my java application.
if i run "pocketsphinx_continous.exe" with some params from the commandline all works well and i see the output from the software. After killing the process, i try to run it within my java application. But java print no output to the console.
This is my code:
public void start(){
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"D:/cmusphinx/pocketsphinx/bin/Release/x64/pocketsphinx_continuous.exe", "-hmm", "d:/cmusphinx/pocketsphinx/model/en-us/en-us", "-lm", "d:/cmusphinx/pocketsphinx/model/en-us/en-us.lm.bin", "-dict", "d:/cmusphinx/pocketsphinx/model/en-us/cmudict-en-us.dict", "-samprate", "16000/8000/48000", "-inmic", "yes"};
Process proc = rt.exec(commands);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Java will only print "Here is the standard output of the command:" and nothing more. But it wont crash, its still running without any errors. It seems to me java will wait until the executed command is finished until it prints anything. But the software will run permanently and print some times new results ...
Any ideas?
Best regards
Mike
I suggest you do the following:
Process p = null;
ProcessBuilder b = new ProcessBuilder("D:/cmusphinx/pocketsphinx/bin/Release/x64/pocketsphinx_continuous.exe -hmm d:/cmusphinx/pocketsphinx/model/en-us/en-us -lm d:/cmusphinx/pocketsphinx/model/en-us/en-us.lm.bin -dict d:/cmusphinx/pocketsphinx/model/en-us/cmudict-en-us.dict -samprate 16000/8000/48000 -inmic yes");
try {
p = b.start();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader output = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ( (line = output.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Using ProcessBuilder you don't have to separate parameters. Just copy the whole command in a String.
I'm trying to execute a network testing application via my Java application. Below is the code:
try {
String file = new File("iperf3.exe").getCanonicalPath();
String cmd[] = {file,"-c ping.online.net -P 20 -w 710000 -t"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
lblConsole.setText(line);
//System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I got an error saying that the application could not connect to the server. However, when I execute the command in my command prompt, there is no error.
May I know what could I have missed out here?
Managed to solve this issue. It is mainly on how I executed the code in the cmd. Just updated to as below:
String file = new File("iperf3.exe").getCanonicalPath(); String cmd1[] = {file,"-c","ping.online.net","-P","10","-w","710000"};
I'm using UBUNTU/LINUX
I'm trying to build swing application for myself(this app need to work other platforms too) and i can not execute some commands on java.I tried to execute "java -version"
Here is my code:
Runtime run = Runtime.getRuntime();
Process p = run.exec("java -version");
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
for (String output = br.readLine(); output != null; output = br.readLine()) {
System.out.println(output);
}
} catch (IOException e) {
System.out.println(e);
}
It returns blank page no output!
However i can execute run.exec("ls") / or ("gedit") and so on... and I GET DATA
Also i can execute internal programs that in my computer.
Why i cant execute .sh files or built-in java commands and getting blank page?
You should use ProcessBuilder to mix standard and error outputs:
ProcessBuilder run = new ProcessBuilder("java", "-version");
run.redirectErrorStream(true);
Process p = run.start();
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
for (String output = br.readLine(); output != null; output = br.readLine()) {
System.out.println(output);
}
} catch (IOException e) {
System.out.println(e);
}
Check that java parent directory is in your PATH variable (output System.getenv("PATH")) or use a full path to the binary.
For script, use /bin/sh like that ProcessBuilder run = new ProcessBuilder("/bin/sh", "/path/to/your/script");
All path can be absolute starting with /, or relative (without /) to System.getProperty("user.dir").
The jvm itself directs output to the error stream
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
I'm developing an application that works with rooted devices too.
I have two questions:
When I launch the application, it checks for root, the SuperUser dialog appears, I click on Accept then 'Remember my Choice' later, I run a command:
Process process;
try {
process = Runtime.getRuntime().exec(new String[]
{"su", "-c", "rm -r /data/data"});
prefs = this.getSharedPreferences("Prefs",
Context.MODE_WORLD_WRITEABLE);
prefsEditor = prefs.edit();
stopSelf();
then here again the SuperUser dialog appears. Why is it appearing more than once for the same application? I checked "Remember My Choice".
I'm using
process = Runtime.getRuntime().exec(new String[]
{"su", "-c", "rm -r /data/data"});
Is there any way to add exceptions, e.g. Do not delete "com.My.App"?
You are removing /data/data and all subdirectories of it. This is the place where applications are storing the app private data, and for sure the SuperUser isstoring in here the list of authorized applications.
I believe you already guess what's happenig ... you are removing your own authorization.
You need to add an exception to the superUser.
To add an exception, I couldn't find a straightforward solution, as only limited shell commands are available. If you install busybox it would give you opportunity to use the grep command to parse the input and exclude the lines you want.
Alternatively, you can do it programmatically using the following approach:
process = Runtime.getRuntime().exec(new String[] {"su", "-c", "ls /data/data"});
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
ArrayList<String> files = new ArrayList<String>();
files.add("su");
files.add("-c");
files.add("rm -r");
while ((line = bufferedReader.readLine()) != null){
//test if you want to exclude the file before you add it
files.add("/data/data/" + line);
}
//issue a new command to remove the directories
process = Runtime.getRuntime().exec(files.toArray(new String[0])); //changed this line
Hope it helped.
--EDITED--
The code bellow is working fine on a rooted device. The final command being issued is also a ls, as I don't want to remove my files, but you can just replace it by anything else (see comments in file).
private void execCmd(){
Process process;
try {
process = Runtime.getRuntime().exec(new String[] {"su", "-c", "ls /data/data"});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
ArrayList<String> files = new ArrayList<String>();
files.add("su");
files.add("-c");
// files.add("rm -r"); //Uncomment this line and comment the line bellow for real delete
files.add("ls");
try {
while ((line = bufferedReader.readLine()) != null){
//test if you want to exclude the file before you add it
files.add("/data/data/" + line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//comment lines bellow to stop logging the command being sent
Log.d(TAG, "Command size: " + files.size());
for(int i=0; i< files.size(); i++)
Log.d(TAG, "Cmd[" + i + "]: " + files.get(i));
try {
process = Runtime.getRuntime().exec(files.toArray(new String[0]));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //changed this line
}
Regards
I call a class which is located somewhere in a jar file (using java -classpath path/file.jar classname) within my java code.
This work well but only if the command is well formed. If I make a mistake the getRuntime().exect(command) just doesn't say anything. Bellow I have the working command invocation. I would like to get the error message when the command doesn't work. If I make a mistake in a cmd (windows) I get a proper error and I can fix it. But not within my java application.
I left a 'if(input.ready())' since if I don't the program freezes when the command line is incorrect. This happens when executing 'input.readLine()'.
// Execute a command with an argument that contains a space
String[] genKOSCommand = new String[] {
"java",
"-classpath",
Config.XDSI_TEST_KIT_HOME + "/xdsitest/lib/xdsitest.jar;"
+ Config.XDSI_TEST_KIT_HOME + "/xdsitest/classes",
"ca.etsmtl.ihe.xdsitest.docsource.SimplePublisher", "-k",
"C:/Softmedical/Viewer_Test/xdsi-testkit-2.0.4/xdsihome/usr/data/image14.dcm" };
Process child = Runtime.getRuntime().exec(genKOSCommand);
BufferedReader input = new BufferedReader(new InputStreamReader(
child.getInputStream()), 13107200);
String line = null;
if (input.ready()) {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
try {
child.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Have any advice on how to get an error from the executed command?
Thank you
By using getErrorStream:
BufferedReader errinput = new BufferedReader(new InputStreamReader(
child.getErrorStream()));
When processing the input from the different streams, it is better to do it in a different thread (since those calls (readLine etc.) are blocking calls.
Here's a bit more complete piece of code to print out errors received upon running some command via Process/Runtime:
final String command = "/bin/bash -c cat foo.txt | some.app";
Process p;
try {
p = Runtime.getRuntime().exec(command);
} catch (final IOException e) {
e.printStackTrace();
}
//Wait to get exit value
try {
p.waitFor();
final int exitValue = p.waitFor();
if (exitValue == 0)
System.out.println("Successfully executed the command: " + command);
else {
System.out.println("Failed to execute the following command: " + command + " due to the following error(s):");
try (final BufferedReader b = new BufferedReader(new InputStreamReader(p.getErrorStream()))) {
String line;
if ((line = b.readLine()) != null)
System.out.println(line);
} catch (final IOException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Isn't Process.getErrorStream what you want?