hi I have a JAVA project in which I want to display the MySQL databases.
i write this code:
try {
String []command={"mysql -u root -pmanager","show databases"};
Process p= Runtime.getRuntime().exec("mysql -u root -pmanager");
Process p1= Runtime.getRuntime().exec("show databases");
if (p.waitFor()==0){System.out.println("backup done...");}
else{System.out.println("!!!");}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but it give me this error:
Cannot run program "show": CreateProcess error=2, The system cannot find the file specified
what it should be do?
thanks...
Better to pass String object in exec method like:
Runtime.getRuntime().exec( new String [] {"mysql", "-u", "root", "-pmanager", "-e", "show databases"} )
You need to specify what is 'mysql' to command prompt...
example - C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql
Related
I know this has kind of been asked before but I just don't know how to locate the program so I can run the R script.
I have a java swing program and on it I have a button whose action should be to run an external R script. I keep getting a "No such file or directory error" and I'm sure its because I don't know what command I should be running. I have RStudio installed. My code:
JButton btnSelfassessR = new JButton("SelfAssess R");
btnSelfassessR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec(new String[] { "/usr/bin/Rscript","Rscript /Users/sebastianzeki/EndoHistol.R"});
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
but actually I think the /usr/bin/Rscript is wrong and I don't know how to correct this. What should I be looking to put in here. What is the likely location for a standard install of RStudio on a mac El Capitan
I have a shell script file that i want to run from java. My java work space directory is different than the script's directory.
private final String scriptPath = "/home/kemallin/Desktop/";
public void cleanCSVScript() {
String script = "clean.sh";
try {
Process awk = new ProcessBuilder(scriptPath + script).start();
awk.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
and i get this error:
java.io.IOException: Cannot run program "cat /home/kemallin/Desktop/capture-03.csv | awk -F ',' '{ print $1,",", $2,",", $3,",", $4,",", $6}' > clean.csv": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at ShellScript.cleanCSVScript(ShellScript.java:21)
at Main.main(Main.java:15)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:186)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 2 more
java.io.FileNotFoundException: /home/kemallin/Desktop/clean.csv (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at CSVReader.run(CSVReader.java:25)
at Main.main(Main.java:17)
I have googled it and every solution pretty much indicate that i'm doing the right thing.
I have tried to put the script file in the src and in bin of the Java project but still it says no such file or dir.
What am i doing wrong?
Thanks.
Your program clean.sh is not an executable as Java understands it, even though the underlying system understands it as executable.
You need to tell Java what shell is needed to execute your command. Do (assuming you are using bash and it is installed at /bin/bash):
private final String scriptPath = "/home/kemallin/Desktop/";
public void cleanCSVScript() {
String script = "clean.sh";
try {
Process awk = new ProcessBuilder("/bin/bash", scriptPath + script).start();
awk.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
You should do a chmod 755 /home/kemallin/Desktop/clean.sh and ensure the java process is run under the same userid
I tried to use this code to delete a file located into /data folder but it doesn't work, what's wrong in it? My device has root.
Runtime.getRuntime().exec(new String[]{"su","rm"+" "+"/data/logger"});
SOLVED USING THIS
Process p;
try {
p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("rm /data/logger"+"\n");
os.writeBytes("exit\n");
os.flush();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Use the -f switch to force the delete.
File exists case is taken care by runtime class.
Try this:
Runtime.getRuntime().exec(new String[]{"su","rm","-f","/data/logger"});
or this
Runtime.getRuntime().exec("su")
Runtime.getRuntime().exec(new String[]{"rm","-f","/data/logger"});
I am executing a self-defined command like following code.I know that append a >nul to command will prevent from outputing error or other message to console.But in my code,>nul is treated as a parameter of my own command deletefile.So the command can not execute as expected.How to resolve it?
Runtime runtime = Runtime.getRuntime();
try {
Process p = runtime.exec("deletefile /name:\"ABC\" /owner:\"Wolfman\" >nul");
int status = p.waitFor();
System.out.println(status);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Solution is:
cmd deletefile /name:\"ABC\" /owner:\"Wolfman\" >nul
I have a sample Java application that I registered as a service using Procrun. I am trying to execute Batch file from my application
public class Service {
public static void main(String args[]) throws IOException, InterruptedException {
if(args.length>0){
if(args[0].equals("start")){
ProcessBuilder builder = new
ProcessBuilder("cmd","/c","start","Start.bat");
builder.start();
}else if(args[0].equals("shutdown")){
ProcessBuilder builder = new
ProcessBuilder("cmd","/c","start","Stop.bat");
builder.start();
}
}
}
}
When I am starting the service, it gets started successfully but it does not launch batch file on my Windows 7.
Contents of Batch files are given below
Start.bat
#echo off
echo I am started
pause
Please let me know what am I missing here
Have you tried following
Runtime.getRuntime().exec("cmd /c start Start.bat");
To execute batch file from java application, try this piece of code:
// "D://bin/" is the location of my .bat
File dir = new File("D:/bin/");
try {
// sign.bat if my actual file.
Runtime.getRuntime().exec("cmd.exe /c sign.bat", null, dir);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}