Backup mysql database from Java using mysqldump failed - java

I am using mysqldump like this:
Runtime.getRuntime().exec("mysqldump -u USERNAME -pPASSWORD DBNAME > /path/to/location/backup.sql");
in order to dump it into my local files, my java program is deployed using kubernetes.
Here is my code:
#RequestMapping(value = "/testDumping", method = {RequestMethod.POST, RequestMethod.GET})
public Object test(#RequestBody Map<String,Object> params) throws IOException {
String runStatement = (String)params.get("runStatement");
Runtime runtime = Runtime.getRuntime();
Process exec = runtime.exec(runStatement);
return exec;
}
And I finally got this exception "java.io.IOException: Cannot run program "mysqldump": error=2, No such file or directory". What is the problem here?

The error message is telling you exactly what the problem is:
java.io.IOException: Cannot run program "mysqldump": error=2, No such file or directory
This means that the shell is unable to find the executable file for "mysqldump". Try giving the entire absolute path to mysqldump inside your command like i.e. (assuming linux-like system):
Runtime.getRuntime().exec("/usr/bin/mysqldump -u USERNAME -p PASSWORD DBNAME > /path/to/location/backup.sql");
Exact path on Linux can usually be found using command which mysqldump
You could as well add this path to you PATH environment variable and keep your command as is (not 100% sure of this one as I'm usually working on Windows and am far from a Linux expert).

Related

Getting No such File or Directory Exception on Ubuntu when trying compile a java project from java.lang.Process

Recently, I have switched to Ubuntu v20.04 from Windows 10 Pro v2004 because of performance purposes. When, I was on Windows I can freely compile a java project from another java program by writing:
String pathToCompiler = "\"C:/Program Files/Java/jdk-14/bin/javac\"";
Process compileProcess = Runtime.getRuntime().exec(pathToCompiler+" -d bin #.sources", null, new File("ProjectPath"))
Where the sources file is a file containing the list of classes of the project
The code above works successfully on Windows 10.
But On Linux(Ubuntu):
if I substitute the value of variable pathToCompiler as
pathToCompiler = "\"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac\""
the below exception is raised up and the program executing the command exits:
"/usr/lib/jvm/java-11-openjdk-amd64/bin/javac" -d bin #.sources
java.io.IOException: Cannot run program ""/usr/lib/jvm/java-11-openjdk-amd64/bin/javac"" (in directory "/home/arham/Documents/Omega Projects/Project0"): error=2, No such file or directory
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at java.base/java.lang.Runtime.exec(Runtime.java:592)
at java.base/java.lang.Runtime.exec(Runtime.java:416)
at ide.utils.systems.BuildView.lambda$3(BuildView.java:267)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.IOException: error=2, No such file or directory
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:340)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:271)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
The problem is that the file actually exists but it says No Such File or Directory
Actually, The program which is compiling the project is a Java IDE that I am creatiing.
Someone please tell if he/she knows how to fix this bug
The Runtime.exec method has several problems that make it difficult to use, and this is one of them. Use the newer ProcessBuilder class instead.
String pathToCompiler = "C:/Program Files/Java/jdk-14/bin/javac";
Process compileProcess = new ProcessBuilder(pathToCompiler, "-d", "bin", "#.sources")
.directory(new File("ProjectPath"))
.start();
The differences are:
Remove the extra quotes from around the path to the executable. If quoting is needed, the system takes care of it.
Pass the each command line arguments as a separate string. This way you don't have to worry about quoting.
Update the path to the following:
String pathToCompiler = "/usr/lib/jvm/java-11-openjdk-amd64/bin/javac/";

java shell execute for php command

I wrote a shell execute program in java for php localhost:8080 with root directory /src/MyForm/ and file name is index.php. Directory Im passing from a string.
my code is like this.
String root = "/src/MyForm";
String[] command = {"php", "-S", "localhost:80 -t "+ root};
Process p = Runtime.getRuntime().exec(command);
im getting output like this
Not Found
The requested resource / was not found on this server.
enter image description here
What i'm missing ?

Unable to get Eclipse to recognize binaries using ProcessBuilder located on $PATH on mac os x

I have just switched over to working on a mac and I am trying to determine why I am unable to get Eclipse to recognize the binary I am trying to run via a ProcessBuilder.
I have tried to run it both as a Java Application in Eclipse and as a TestNG test.
If I compile the class with java and run it directly from the command line it will work but not through Eclipse which leads me to believe the configuration for the $PATH is not setup correctly in my TestNG configuration.
Question
I am sure this is a configuration issue within Eclipse but after searching for a day and coming up short I wanted to post for some help. I have tried to set $PATH on the configuration but it does not seem to work.
Thank you
Update /Answer
It turned out that the PATH I had set on the shell shown below was not the same that Java had which I checked using the code below. After verifying that I then added the proper path to my environment on the ProcessBuilder and executed the script as shown in the answer.
Map<String, String> env = processBuilder.environment();
for (String key : env.keySet())
System.out.println(key + ": " + env.get(key));
Map<String, String> envs = processBuilder.environment();
System.out.println("Path " + envs.get("PATH"));
envs.put("PATH", "/usr/local/bin");
System.out.println("PATH " + envs.get("PATH"));
Code
File logsDir = new File(logDirectory);
if (!logsDir.exists()) {
logsDir.mkdirs();
}
// run process directly
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("appium");
processBuilder.redirectError(new File(logsDir, "appiumError.txt"));
processBuilder.redirectOutput(new File(logsDir, "appiumOutput.txt"));
process = processBuilder.start();
Output (it cannot find node to run appium hence the No such file or directory)
Caused by: java.io.IOException: Cannot run program "appium": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at AppiumService.startAppium(AppiumService.java:77)
Path (The bin for node and appium is in /usr/local/bin)
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:
/usr/local/opt/ant/bin:/usr/local/opt/maven/bin:
/usr/local/opt/gradle/bin
The PATH variable of the java-process might be explicitly set by Eclipse not containing the paths you need. You can call the command using the absolute path to the corresponding directory or you might try using a shell to start the process by creating the process with
processBuilder.command("/bin/sh", "-c", "appium");

Running yarn job from java program using ProcessBuilder gives file does not exist error

I am trying to run a yarn job from a java wrapper program. The mapreduce jar takes two inputs:
A header file: I dont know the name of the file but the location and file extension and there's only one file at that location
A Input files directory
Apart from these I have an Output directory.
the processbuilder code looks like:
HEADER_PATH = INPUT_DIRECTORY+"/HEADER/*.tsv";
INPUT_FILES = INPUT_DIRECTORY+"/DATA/";
OUTPUT_DIRECTORY = OUTPUT_DIRECTORY+"/";
ProcessBuilder mapRProcessBuilder = new ProcessBuilder("yarn","jar",JAR_LOCATION,"-Dmapred.job.queue.name=name","-Dmapred.reduce.tasks=500",HEADER_PATH,INPUT_DIRECTORY,OUTPUT_DIRECTORY);
System.out.println(mapRProcessBuilder.command().toString());
Process mapRProcess = mapRProcessBuilder.start();
On run, I get the following error:
Exception in thread "main" java.io.FileNotFoundException: Requested
file /input/path/dir1/HEADER/*.tsv does not exist.
But when I run the same command as :
yarn jar jarfile.jar -Dmapred.job.queue.name=name -Dmapred.reduce.tasks=500 /input/path/dir1/HEADER/*.tsv /input/Dir /output/Dir/
It works all fine.
what can be the issue when running the command from java is causing this issue?
The * is being treated as part of the literal string in this case rather than a wildcard. Therefore globbing isn't expanding to your desired path name.
If there is only one file in the directory, why don't you find what its path is and pass that as the argument instead
eg.
File dir = new File(INPUT_DIRECTORY+"/HEADER);
if (dir.list().length > 0)
String HEADER_PATH = dir.list()[0].getAbsolutePath();

Running external executable file from Java program

I'm trying to write a Java program which gets a executable file to run under Linux. This executable file receives two parameters and performs a nmap -sP operating with the two given parameters.
I've called this file as file.exe and its content is the one below:
nmap -sP $segment1-$segment1
I already did a chmod +x file.exe and it's in same directory where the .class is present.
The Java code is the following:
import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
import java.lang.InterruptedException;
public class runFile {
public static void main (String args[]) throws IOException, InterruptedException {
Runtime r = Runtime.getRuntime();
Process p = r.exec("file.exe "+args[0]+" "+args[1]);
p.waitFor();
}
}
After compiling, Whenever I try to run it (from where the file.exe is) by
java runFile
I'm getting the following exception and errors log:
Exception in thread "main" java.io.IOException: Cannot run program
"file.exe": error=2, No such file or directory at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) at
java.lang.Runtime.exec(Runtime.java:617) at
java.lang.Runtime.exec(Runtime.java:450) at
java.lang.Runtime.exec(Runtime.java:347) at
runFile.main(runFile.java:12) Caused by: java.io.IOException: error=2,
No such file or directory at java.lang.UNIXProcess.forkAndExec(Native
Method) at java.lang.UNIXProcess.(UNIXProcess.java:135) at
java.lang.ProcessImpl.start(ProcessImpl.java:130) at
java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) ... 4 more
What am I missing?
The error is telling you that the executable can not be found in the current directory or the OS's search path.
Try including the Pathans part of the command
Process p = r.exec("/path/to/file.exe "+args[0]+" "+args[1]);
You should also consider separating each command/argument as a separate parameter
Process p = r.exec(new String[]{"/path/to/file.exe ", args[0], args[1]});
This will help with parameters that contain spaces.
You should also consider using ProcessBuilder, this will allow you to change the directory context that the command should be executed
Your java program looks for file.exe in the directory where you started your java program. It does not look inside the directory with your class files.

Categories

Resources